xref: /dragonfly/sys/dev/netif/iwm/if_iwm_phy_db.c (revision ef2687d4)
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/mutex.h>
116 #include <sys/module.h>
117 #include <sys/proc.h>
118 #include <sys/rman.h>
119 #include <sys/socket.h>
120 #include <sys/sockio.h>
121 #include <sys/sysctl.h>
122 #include <sys/linker.h>
123 
124 #include <machine/endian.h>
125 
126 #include <bus/pci/pcivar.h>
127 #include <bus/pci/pcireg.h>
128 
129 #include <net/bpf.h>
130 
131 #include <net/if.h>
132 #include <net/if_var.h>
133 #include <net/if_arp.h>
134 #include <net/ethernet.h>
135 #include <net/if_dl.h>
136 #include <net/if_media.h>
137 #include <net/if_types.h>
138 #include <net/ifq_var.h>
139 
140 #include <netinet/in.h>
141 #include <netinet/in_systm.h>
142 #include <netinet/if_ether.h>
143 #include <netinet/ip.h>
144 
145 #include <netproto/802_11/ieee80211_var.h>
146 #include <netproto/802_11/ieee80211_regdomain.h>
147 #include <netproto/802_11/ieee80211_ratectl.h>
148 #include <netproto/802_11/ieee80211_radiotap.h>
149 
150 #include "if_iwmreg.h"
151 #include "if_iwmvar.h"
152 #include "if_iwm_debug.h"
153 #include "if_iwm_util.h"
154 #include "if_iwm_phy_db.h"
155 
156 /*
157  * BEGIN iwl-phy-db.c
158  */
159 /*
160  * get phy db section: returns a pointer to a phy db section specified by
161  * type and channel group id.
162  */
163 static struct iwm_phy_db_entry *
164 iwm_phy_db_get_section(struct iwm_softc *sc,
165 	enum iwm_phy_db_section_type type, uint16_t chg_id)
166 {
167 	struct iwm_phy_db *phy_db = &sc->sc_phy_db;
168 
169 	if (type >= IWM_PHY_DB_MAX)
170 		return NULL;
171 
172 	switch (type) {
173 	case IWM_PHY_DB_CFG:
174 		return &phy_db->cfg;
175 	case IWM_PHY_DB_CALIB_NCH:
176 		return &phy_db->calib_nch;
177 	case IWM_PHY_DB_CALIB_CHG_PAPD:
178 		if (chg_id >= IWM_NUM_PAPD_CH_GROUPS)
179 			return NULL;
180 		return &phy_db->calib_ch_group_papd[chg_id];
181 	case IWM_PHY_DB_CALIB_CHG_TXP:
182 		if (chg_id >= IWM_NUM_TXP_CH_GROUPS)
183 			return NULL;
184 		return &phy_db->calib_ch_group_txp[chg_id];
185 	default:
186 		return NULL;
187 	}
188 	return NULL;
189 }
190 
191 int
192 iwm_phy_db_set_section(struct iwm_softc *sc,
193 	struct iwm_calib_res_notif_phy_db *phy_db_notif)
194 {
195 	enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
196 	uint16_t size  = le16toh(phy_db_notif->length);
197 	struct iwm_phy_db_entry *entry;
198 	uint16_t chg_id = 0;
199 
200 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD ||
201 	    type == IWM_PHY_DB_CALIB_CHG_TXP)
202 		chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
203 
204 	entry = iwm_phy_db_get_section(sc, type, chg_id);
205 	if (!entry)
206 		return EINVAL;
207 
208 	if (entry->data)
209 		kfree(entry->data, M_DEVBUF);
210 	entry->data = kmalloc(size, M_DEVBUF, M_INTWAIT);
211 	if (!entry->data) {
212 		entry->size = 0;
213 		return ENOMEM;
214 	}
215 	memcpy(entry->data, phy_db_notif->data, size);
216 	entry->size = size;
217 
218 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
219 	    "%s(%d): [PHYDB]SET: Type %d , Size: %d, data: %p\n",
220 	    __func__, __LINE__, type, size, entry->data);
221 
222 	return 0;
223 }
224 
225 static int
226 iwm_is_valid_channel(uint16_t ch_id)
227 {
228 	if (ch_id <= 14 ||
229 	    (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
230 	    (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
231 	    (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
232 		return 1;
233 	return 0;
234 }
235 
236 static uint8_t
237 iwm_ch_id_to_ch_index(uint16_t ch_id)
238 {
239 	if (!iwm_is_valid_channel(ch_id))
240 		return 0xff;
241 
242 	if (ch_id <= 14)
243 		return ch_id - 1;
244 	if (ch_id <= 64)
245 		return (ch_id + 20) / 4;
246 	if (ch_id <= 140)
247 		return (ch_id - 12) / 4;
248 	return (ch_id - 13) / 4;
249 }
250 
251 
252 static uint16_t
253 iwm_channel_id_to_papd(uint16_t ch_id)
254 {
255 	if (!iwm_is_valid_channel(ch_id))
256 		return 0xff;
257 
258 	if (1 <= ch_id && ch_id <= 14)
259 		return 0;
260 	if (36 <= ch_id && ch_id <= 64)
261 		return 1;
262 	if (100 <= ch_id && ch_id <= 140)
263 		return 2;
264 	return 3;
265 }
266 
267 static uint16_t
268 iwm_channel_id_to_txp(struct iwm_softc *sc, uint16_t ch_id)
269 {
270 	struct iwm_phy_db *phy_db = &sc->sc_phy_db;
271 	struct iwm_phy_db_chg_txp *txp_chg;
272 	int i;
273 	uint8_t ch_index = iwm_ch_id_to_ch_index(ch_id);
274 
275 	if (ch_index == 0xff)
276 		return 0xff;
277 
278 	for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++) {
279 		txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
280 		if (!txp_chg)
281 			return 0xff;
282 		/*
283 		 * Looking for the first channel group that its max channel is
284 		 * higher then wanted channel.
285 		 */
286 		if (le16toh(txp_chg->max_channel_idx) >= ch_index)
287 			return i;
288 	}
289 	return 0xff;
290 }
291 
292 static int
293 iwm_phy_db_get_section_data(struct iwm_softc *sc,
294 	uint32_t type, uint8_t **data, uint16_t *size, uint16_t ch_id)
295 {
296 	struct iwm_phy_db_entry *entry;
297 	uint16_t ch_group_id = 0;
298 
299 	IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__);
300 	/* find wanted channel group */
301 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
302 		ch_group_id = iwm_channel_id_to_papd(ch_id);
303 	else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
304 		ch_group_id = iwm_channel_id_to_txp(sc, ch_id);
305 
306 	entry = iwm_phy_db_get_section(sc, type, ch_group_id);
307 	if (!entry)
308 		return EINVAL;
309 
310 	*data = entry->data;
311 	*size = entry->size;
312 
313 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
314 	    "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
315 	    __func__, __LINE__, type, *size);
316 
317 	return 0;
318 }
319 
320 static int
321 iwm_send_phy_db_cmd(struct iwm_softc *sc, uint16_t type,
322 	uint16_t length, void *data)
323 {
324 	struct iwm_phy_db_cmd phy_db_cmd;
325 	struct iwm_host_cmd cmd = {
326 		.id = IWM_PHY_DB_CMD,
327 		.flags = IWM_CMD_SYNC,
328 	};
329 
330 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
331 	    "Sending PHY-DB hcmd of type %d, of length %d\n",
332 	    type, length);
333 
334 	/* Set phy db cmd variables */
335 	phy_db_cmd.type = le16toh(type);
336 	phy_db_cmd.length = le16toh(length);
337 
338 	/* Set hcmd variables */
339 	cmd.data[0] = &phy_db_cmd;
340 	cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
341 	cmd.data[1] = data;
342 	cmd.len[1] = length;
343 	cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY;
344 
345 	return iwm_send_cmd(sc, &cmd);
346 }
347 
348 static int
349 iwm_phy_db_send_all_channel_groups(struct iwm_softc *sc,
350 	enum iwm_phy_db_section_type type, uint8_t max_ch_groups)
351 {
352 	uint16_t i;
353 	int err;
354 	struct iwm_phy_db_entry *entry;
355 
356 	/* Send all the channel-specific groups to operational fw */
357 	for (i = 0; i < max_ch_groups; i++) {
358 		entry = iwm_phy_db_get_section(sc, type, i);
359 		if (!entry)
360 			return EINVAL;
361 
362 		if (!entry->size)
363 			continue;
364 
365 		/* Send the requested PHY DB section */
366 		err = iwm_send_phy_db_cmd(sc, type, entry->size, entry->data);
367 		if (err) {
368 			IWM_DPRINTF(sc, IWM_DEBUG_CMD,
369 			    "%s: Can't SEND phy_db section %d (%d), "
370 			    "err %d\n", __func__, type, i, err);
371 			return err;
372 		}
373 
374 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
375 		    "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
376 	}
377 
378 	return 0;
379 }
380 
381 int
382 iwm_send_phy_db_data(struct iwm_softc *sc)
383 {
384 	uint8_t *data = NULL;
385 	uint16_t size = 0;
386 	int err;
387 
388 	IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
389 	    "%s: Sending phy db data and configuration to runtime image\n",
390 	    __func__);
391 
392 	/* Send PHY DB CFG section */
393 	err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CFG, &data, &size, 0);
394 	if (err) {
395 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
396 		    "%s: Cannot get Phy DB cfg section, %d\n",
397 		    __func__, err);
398 		return err;
399 	}
400 
401 	err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CFG, size, data);
402 	if (err) {
403 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
404 		    "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
405 		    __func__, err);
406 		return err;
407 	}
408 
409 	err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CALIB_NCH,
410 	    &data, &size, 0);
411 	if (err) {
412 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
413 		    "%s: Cannot get Phy DB non specific channel section, "
414 		    "%d\n", __func__, err);
415 		return err;
416 	}
417 
418 	err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CALIB_NCH, size, data);
419 	if (err) {
420 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
421 		    "%s: Cannot send HCMD of Phy DB non specific channel "
422 		    "sect, %d\n", __func__, err);
423 		return err;
424 	}
425 
426 	/* Send all the TXP channel specific data */
427 	err = iwm_phy_db_send_all_channel_groups(sc,
428 	    IWM_PHY_DB_CALIB_CHG_PAPD, IWM_NUM_PAPD_CH_GROUPS);
429 	if (err) {
430 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
431 		    "%s: Cannot send channel specific PAPD groups, %d\n",
432 		    __func__, err);
433 		return err;
434 	}
435 
436 	/* Send all the TXP channel specific data */
437 	err = iwm_phy_db_send_all_channel_groups(sc,
438 	    IWM_PHY_DB_CALIB_CHG_TXP, IWM_NUM_TXP_CH_GROUPS);
439 	if (err) {
440 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
441 		    "%s: Cannot send channel specific TX power groups, "
442 		    "%d\n", __func__, err);
443 		return err;
444 	}
445 
446 	IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
447 	    "%s: Finished sending phy db non channel data\n",
448 	    __func__);
449 	return 0;
450 }
451