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