xref: /freebsd/sys/dev/iwm/if_iwm_phy_db.c (revision 02e9120893770924227138ba49df1edb3896112a)
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 "if_iwmreg.h"
153 #include "if_iwmvar.h"
154 #include "if_iwm_debug.h"
155 #include "if_iwm_util.h"
156 #include "if_iwm_phy_db.h"
157 
158 #define CHANNEL_NUM_SIZE	4	/* num of channels in calib_ch size */
159 
160 struct iwm_phy_db_entry {
161 	uint16_t	size;
162 	uint8_t		*data;
163 };
164 
165 /**
166  * struct iwm_phy_db - stores phy configuration and calibration data.
167  *
168  * @cfg: phy configuration.
169  * @calib_nch: non channel specific calibration data.
170  * @calib_ch: channel specific calibration data.
171  * @n_group_papd: number of entries in papd channel group.
172  * @calib_ch_group_papd: calibration data related to papd channel group.
173  * @n_group_txp: number of entries in tx power channel group.
174  * @calib_ch_group_txp: calibration data related to tx power chanel group.
175  */
176 struct iwm_phy_db {
177 	struct iwm_phy_db_entry cfg;
178 	struct iwm_phy_db_entry calib_nch;
179 	int n_group_papd;
180 	struct iwm_phy_db_entry *calib_ch_group_papd;
181 	int n_group_txp;
182 	struct iwm_phy_db_entry *calib_ch_group_txp;
183 
184 	struct iwm_softc *sc;
185 };
186 
187 struct iwm_phy_db *
188 iwm_phy_db_init(struct iwm_softc *sc)
189 {
190 	struct iwm_phy_db *phy_db = malloc(sizeof(struct iwm_phy_db),
191 	    M_DEVBUF, M_NOWAIT | M_ZERO);
192 
193 	if (!phy_db)
194 		return phy_db;
195 
196 	phy_db->sc = sc;
197 
198 	phy_db->n_group_txp = -1;
199 	phy_db->n_group_papd = -1;
200 
201 	/* TODO: add default values of the phy db. */
202 	return phy_db;
203 }
204 
205 /*
206  * get phy db section: returns a pointer to a phy db section specified by
207  * type and channel group id.
208  */
209 static struct iwm_phy_db_entry *
210 iwm_phy_db_get_section(struct iwm_phy_db *phy_db,
211 		       enum iwm_phy_db_section_type type,
212 		       uint16_t chg_id)
213 {
214 	if (!phy_db || type >= IWM_PHY_DB_MAX)
215 		return NULL;
216 
217 	switch (type) {
218 	case IWM_PHY_DB_CFG:
219 		return &phy_db->cfg;
220 	case IWM_PHY_DB_CALIB_NCH:
221 		return &phy_db->calib_nch;
222 	case IWM_PHY_DB_CALIB_CHG_PAPD:
223 		if (chg_id >= phy_db->n_group_papd)
224 			return NULL;
225 		return &phy_db->calib_ch_group_papd[chg_id];
226 	case IWM_PHY_DB_CALIB_CHG_TXP:
227 		if (chg_id >= phy_db->n_group_txp)
228 			return NULL;
229 		return &phy_db->calib_ch_group_txp[chg_id];
230 	default:
231 		return NULL;
232 	}
233 	return NULL;
234 }
235 
236 static void
237 iwm_phy_db_free_section(struct iwm_phy_db *phy_db,
238 			enum iwm_phy_db_section_type type, uint16_t chg_id)
239 {
240 	struct iwm_phy_db_entry *entry =
241 				iwm_phy_db_get_section(phy_db, type, chg_id);
242 	if (!entry)
243 		return;
244 
245 	if (entry->data != NULL)
246 		free(entry->data, M_DEVBUF);
247 	entry->data = NULL;
248 	entry->size = 0;
249 }
250 
251 void
252 iwm_phy_db_free(struct iwm_phy_db *phy_db)
253 {
254 	int i;
255 
256 	if (!phy_db)
257 		return;
258 
259 	iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CFG, 0);
260 	iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_NCH, 0);
261 
262 	for (i = 0; i < phy_db->n_group_papd; i++)
263 		iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_PAPD, i);
264 	if (phy_db->calib_ch_group_papd != NULL)
265 		free(phy_db->calib_ch_group_papd, M_DEVBUF);
266 
267 	for (i = 0; i < phy_db->n_group_txp; i++)
268 		iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_TXP, i);
269 	if (phy_db->calib_ch_group_txp != NULL)
270 		free(phy_db->calib_ch_group_txp, M_DEVBUF);
271 
272 	free(phy_db, M_DEVBUF);
273 }
274 
275 int
276 iwm_phy_db_set_section(struct iwm_phy_db *phy_db,
277 		       struct iwm_rx_packet *pkt)
278 {
279 	struct iwm_calib_res_notif_phy_db *phy_db_notif =
280 			(struct iwm_calib_res_notif_phy_db *)pkt->data;
281 	enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
282         uint16_t size  = le16toh(phy_db_notif->length);
283         struct iwm_phy_db_entry *entry;
284         uint16_t chg_id = 0;
285 
286 	if (!phy_db)
287 		return EINVAL;
288 
289 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD) {
290 		chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
291 		if (phy_db && !phy_db->calib_ch_group_papd) {
292 			/*
293 			 * Firmware sends the largest index first, so we can use
294 			 * it to know how much we should allocate.
295 			 */
296 			phy_db->calib_ch_group_papd = malloc(
297 			    (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
298 			    M_DEVBUF, M_NOWAIT | M_ZERO);
299 			if (!phy_db->calib_ch_group_papd)
300 				return ENOMEM;
301 			phy_db->n_group_papd = chg_id + 1;
302 		}
303 	} else if (type == IWM_PHY_DB_CALIB_CHG_TXP) {
304 		chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
305 		if (phy_db && !phy_db->calib_ch_group_txp) {
306 			/*
307 			 * Firmware sends the largest index first, so we can use
308 			 * it to know how much we should allocate.
309 			 */
310 			phy_db->calib_ch_group_txp = malloc(
311 			    (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
312 			    M_DEVBUF, M_NOWAIT | M_ZERO);
313 			if (!phy_db->calib_ch_group_txp)
314 				return ENOMEM;
315 			phy_db->n_group_txp = chg_id + 1;
316 		}
317 	}
318 
319 	entry = iwm_phy_db_get_section(phy_db, type, chg_id);
320 	if (!entry)
321 		return EINVAL;
322 
323 	if (entry->data != NULL)
324 		free(entry->data, M_DEVBUF);
325 	entry->data = malloc(size, M_DEVBUF, M_NOWAIT);
326 	if (!entry->data) {
327 		entry->size = 0;
328 		return ENOMEM;
329 	}
330 	memcpy(entry->data, phy_db_notif->data, size);
331 
332 	entry->size = size;
333 
334 	IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
335 		    "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
336 		    __func__, __LINE__, type, size);
337 
338 	return 0;
339 }
340 
341 static int
342 is_valid_channel(uint16_t ch_id)
343 {
344 	if (ch_id <= 14 ||
345 	    (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
346 	    (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
347 	    (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
348 		return 1;
349 	return 0;
350 }
351 
352 static uint8_t
353 ch_id_to_ch_index(uint16_t ch_id)
354 {
355 	if (!is_valid_channel(ch_id))
356 		return 0xff;
357 
358 	if (ch_id <= 14)
359 		return ch_id - 1;
360 	if (ch_id <= 64)
361 		return (ch_id + 20) / 4;
362 	if (ch_id <= 140)
363 		return (ch_id - 12) / 4;
364 	return (ch_id - 13) / 4;
365 }
366 
367 
368 static uint16_t
369 channel_id_to_papd(uint16_t ch_id)
370 {
371 	if (!is_valid_channel(ch_id))
372 		return 0xff;
373 
374 	if (1 <= ch_id && ch_id <= 14)
375 		return 0;
376 	if (36 <= ch_id && ch_id <= 64)
377 		return 1;
378 	if (100 <= ch_id && ch_id <= 140)
379 		return 2;
380 	return 3;
381 }
382 
383 static uint16_t
384 channel_id_to_txp(struct iwm_phy_db *phy_db, uint16_t ch_id)
385 {
386 	struct iwm_phy_db_chg_txp *txp_chg;
387 	int i;
388 	uint8_t ch_index = ch_id_to_ch_index(ch_id);
389 	if (ch_index == 0xff)
390 		return 0xff;
391 
392 	for (i = 0; i < phy_db->n_group_txp; i++) {
393 		txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
394 		if (!txp_chg)
395 			return 0xff;
396 		/*
397 		 * Looking for the first channel group that its max channel is
398 		 * higher then wanted channel.
399 		 */
400 		if (le16toh(txp_chg->max_channel_idx) >= ch_index)
401 			return i;
402 	}
403 	return 0xff;
404 }
405 
406 static int
407 iwm_phy_db_get_section_data(struct iwm_phy_db *phy_db,
408 			   uint32_t type, uint8_t **data, uint16_t *size,
409 			   uint16_t ch_id)
410 {
411 	struct iwm_phy_db_entry *entry;
412 	uint16_t ch_group_id = 0;
413 
414 	if (!phy_db)
415 		return EINVAL;
416 
417 	/* find wanted channel group */
418 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
419 		ch_group_id = channel_id_to_papd(ch_id);
420 	else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
421 		ch_group_id = channel_id_to_txp(phy_db, ch_id);
422 
423 	entry = iwm_phy_db_get_section(phy_db, type, ch_group_id);
424 	if (!entry)
425 		return EINVAL;
426 
427 	*data = entry->data;
428 	*size = entry->size;
429 
430 	IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
431 		   "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
432 		   __func__, __LINE__, type, *size);
433 
434 	return 0;
435 }
436 
437 static int
438 iwm_send_phy_db_cmd(struct iwm_phy_db *phy_db, uint16_t type,
439 		    uint16_t length, void *data)
440 {
441 	struct iwm_phy_db_cmd phy_db_cmd;
442 	struct iwm_host_cmd cmd = {
443 		.id = IWM_PHY_DB_CMD,
444 	};
445 
446 	IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
447 		   "Sending PHY-DB hcmd of type %d, of length %d\n",
448 		   type, length);
449 
450 	/* Set phy db cmd variables */
451 	phy_db_cmd.type = htole16(type);
452 	phy_db_cmd.length = htole16(length);
453 
454 	/* Set hcmd variables */
455 	cmd.data[0] = &phy_db_cmd;
456 	cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
457 	cmd.data[1] = data;
458 	cmd.len[1] = length;
459 #ifdef notyet
460 	cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY;
461 #endif
462 
463 	return iwm_send_cmd(phy_db->sc, &cmd);
464 }
465 
466 static int
467 iwm_phy_db_send_all_channel_groups(struct iwm_phy_db *phy_db,
468 				   enum iwm_phy_db_section_type type,
469 				   uint8_t max_ch_groups)
470 {
471 	uint16_t i;
472 	int err;
473 	struct iwm_phy_db_entry *entry;
474 
475 	/* Send all the channel specific groups to operational fw */
476 	for (i = 0; i < max_ch_groups; i++) {
477 		entry = iwm_phy_db_get_section(phy_db,
478                                                type,
479                                                i);
480 		if (!entry)
481 			return EINVAL;
482 
483 		if (!entry->size)
484 			continue;
485 
486 		/* Send the requested PHY DB section */
487 		err = iwm_send_phy_db_cmd(phy_db,
488 					  type,
489 					  entry->size,
490 					  entry->data);
491 		if (err) {
492 			device_printf(phy_db->sc->sc_dev,
493 				"Can't SEND phy_db section %d (%d), err %d\n",
494 				type, i, err);
495 			return err;
496 		}
497 
498 		IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD,
499 		    "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
500 	}
501 
502 	return 0;
503 }
504 
505 int
506 iwm_send_phy_db_data(struct iwm_phy_db *phy_db)
507 {
508 	uint8_t *data = NULL;
509 	uint16_t size = 0;
510 	int err;
511 
512 	IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
513 	    "%s: Sending phy db data and configuration to runtime image\n",
514 	    __func__);
515 
516 	/* Send PHY DB CFG section */
517 	err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CFG,
518 					  &data, &size, 0);
519 	if (err) {
520 		device_printf(phy_db->sc->sc_dev,
521 		    "%s: Cannot get Phy DB cfg section, %d\n",
522 		    __func__, err);
523 		return err;
524 	}
525 
526 	err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CFG, size, data);
527 	if (err) {
528 		device_printf(phy_db->sc->sc_dev,
529 		    "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
530 		    __func__, err);
531 		return err;
532 	}
533 
534 	err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CALIB_NCH,
535 	    &data, &size, 0);
536 	if (err) {
537 		device_printf(phy_db->sc->sc_dev,
538 		    "%s: Cannot get Phy DB non specific channel section, "
539 		    "%d\n", __func__, err);
540 		return err;
541 	}
542 
543 	err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CALIB_NCH, size, data);
544 	if (err) {
545 		device_printf(phy_db->sc->sc_dev,
546 		    "%s: Cannot send HCMD of Phy DB non specific channel "
547 		    "sect, %d\n", __func__, err);
548 		return err;
549 	}
550 
551 	/* Send all the TXP channel specific data */
552 	err = iwm_phy_db_send_all_channel_groups(phy_db,
553 	    IWM_PHY_DB_CALIB_CHG_PAPD, phy_db->n_group_papd);
554 	if (err) {
555 		device_printf(phy_db->sc->sc_dev,
556 		    "%s: Cannot send channel specific PAPD groups, %d\n",
557 		    __func__, err);
558 		return err;
559 	}
560 
561 	/* Send all the TXP channel specific data */
562 	err = iwm_phy_db_send_all_channel_groups(phy_db,
563 	    IWM_PHY_DB_CALIB_CHG_TXP, phy_db->n_group_txp);
564 	if (err) {
565 		device_printf(phy_db->sc->sc_dev,
566 		    "%s: Cannot send channel specific TX power groups, "
567 		    "%d\n", __func__, err);
568 		return err;
569 	}
570 
571 	IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
572 	    "%s: Finished sending phy db non channel data\n",
573 	    __func__);
574 	return 0;
575 }
576