1 /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */
4 * Copyright (c) 2014 genua mbh <info@genua.de>
5 * Copyright (c) 2014 Fixup Software Ltd.
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.
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.
21 * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22 * which were used as the reference documentation for this implementation.
24 * Driver version we are currently based off of is
25 * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
27 ***********************************************************************
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.
34 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
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.
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.
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,
50 * The full GNU General Public License is included in this distribution
51 * in the file called COPYING.
53 * Contact Information:
54 * Intel Linux Wireless <ilw@linux.intel.com>
55 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
60 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61 * All rights reserved.
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
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
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.
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.
91 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
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.
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.
105 #include <sys/cdefs.h>
106 __FBSDID("$FreeBSD$");
108 #include "opt_wlan.h"
110 #include <sys/param.h>
112 #include <sys/conf.h>
113 #include <sys/endian.h>
114 #include <sys/firmware.h>
115 #include <sys/kernel.h>
116 #include <sys/malloc.h>
117 #include <sys/mbuf.h>
118 #include <sys/mutex.h>
119 #include <sys/module.h>
120 #include <sys/proc.h>
121 #include <sys/rman.h>
122 #include <sys/socket.h>
123 #include <sys/sockio.h>
124 #include <sys/sysctl.h>
125 #include <sys/linker.h>
127 #include <machine/bus.h>
128 #include <machine/endian.h>
129 #include <machine/resource.h>
131 #include <dev/pci/pcivar.h>
132 #include <dev/pci/pcireg.h>
137 #include <net/if_var.h>
138 #include <net/if_arp.h>
139 #include <net/if_dl.h>
140 #include <net/if_media.h>
141 #include <net/if_types.h>
143 #include <netinet/in.h>
144 #include <netinet/in_systm.h>
145 #include <netinet/if_ether.h>
146 #include <netinet/ip.h>
148 #include <net80211/ieee80211_var.h>
149 #include <net80211/ieee80211_regdomain.h>
150 #include <net80211/ieee80211_ratectl.h>
151 #include <net80211/ieee80211_radiotap.h>
153 #include "if_iwmreg.h"
154 #include "if_iwmvar.h"
155 #include "if_iwm_debug.h"
156 #include "if_iwm_util.h"
157 #include "if_iwm_phy_db.h"
159 #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
161 struct iwm_phy_db_entry {
167 * struct iwm_phy_db - stores phy configuration and calibration data.
169 * @cfg: phy configuration.
170 * @calib_nch: non channel specific calibration data.
171 * @calib_ch: channel specific calibration data.
172 * @n_group_papd: number of entries in papd channel group.
173 * @calib_ch_group_papd: calibration data related to papd channel group.
174 * @n_group_txp: number of entries in tx power channel group.
175 * @calib_ch_group_txp: calibration data related to tx power chanel group.
178 struct iwm_phy_db_entry cfg;
179 struct iwm_phy_db_entry calib_nch;
181 struct iwm_phy_db_entry *calib_ch_group_papd;
183 struct iwm_phy_db_entry *calib_ch_group_txp;
185 struct iwm_softc *sc;
188 enum iwm_phy_db_section_type {
190 IWM_PHY_DB_CALIB_NCH,
192 IWM_PHY_DB_CALIB_CHG_PAPD,
193 IWM_PHY_DB_CALIB_CHG_TXP,
197 #define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */
200 * phy db - configure operational ucode
202 struct iwm_phy_db_cmd {
208 /* for parsing of tx power channel group data that comes from the firmware*/
209 struct iwm_phy_db_chg_txp {
211 uint16_t max_channel_idx;
215 * phy db - Receive phy db chunk after calibrations
217 struct iwm_calib_res_notif_phy_db {
224 iwm_phy_db_init(struct iwm_softc *sc)
226 struct iwm_phy_db *phy_db = malloc(sizeof(struct iwm_phy_db),
227 M_DEVBUF, M_NOWAIT | M_ZERO);
234 phy_db->n_group_txp = -1;
235 phy_db->n_group_papd = -1;
237 /* TODO: add default values of the phy db. */
242 * get phy db section: returns a pointer to a phy db section specified by
243 * type and channel group id.
245 static struct iwm_phy_db_entry *
246 iwm_phy_db_get_section(struct iwm_phy_db *phy_db,
247 enum iwm_phy_db_section_type type,
250 if (!phy_db || type >= IWM_PHY_DB_MAX)
256 case IWM_PHY_DB_CALIB_NCH:
257 return &phy_db->calib_nch;
258 case IWM_PHY_DB_CALIB_CHG_PAPD:
259 if (chg_id >= phy_db->n_group_papd)
261 return &phy_db->calib_ch_group_papd[chg_id];
262 case IWM_PHY_DB_CALIB_CHG_TXP:
263 if (chg_id >= phy_db->n_group_txp)
265 return &phy_db->calib_ch_group_txp[chg_id];
273 iwm_phy_db_free_section(struct iwm_phy_db *phy_db,
274 enum iwm_phy_db_section_type type, uint16_t chg_id)
276 struct iwm_phy_db_entry *entry =
277 iwm_phy_db_get_section(phy_db, type, chg_id);
281 if (entry->data != NULL)
282 free(entry->data, M_DEVBUF);
288 iwm_phy_db_free(struct iwm_phy_db *phy_db)
295 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CFG, 0);
296 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_NCH, 0);
298 for (i = 0; i < phy_db->n_group_papd; i++)
299 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_PAPD, i);
300 if (phy_db->calib_ch_group_papd != NULL)
301 free(phy_db->calib_ch_group_papd, M_DEVBUF);
303 for (i = 0; i < phy_db->n_group_txp; i++)
304 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_TXP, i);
305 if (phy_db->calib_ch_group_txp != NULL)
306 free(phy_db->calib_ch_group_txp, M_DEVBUF);
308 free(phy_db, M_DEVBUF);
312 iwm_phy_db_set_section(struct iwm_phy_db *phy_db,
313 struct iwm_rx_packet *pkt)
315 struct iwm_calib_res_notif_phy_db *phy_db_notif =
316 (struct iwm_calib_res_notif_phy_db *)pkt->data;
317 enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
318 uint16_t size = le16toh(phy_db_notif->length);
319 struct iwm_phy_db_entry *entry;
325 if (type == IWM_PHY_DB_CALIB_CHG_PAPD) {
326 chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
327 if (phy_db && !phy_db->calib_ch_group_papd) {
329 * Firmware sends the largest index first, so we can use
330 * it to know how much we should allocate.
332 phy_db->calib_ch_group_papd = malloc(
333 (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
334 M_DEVBUF, M_NOWAIT | M_ZERO);
335 if (!phy_db->calib_ch_group_papd)
337 phy_db->n_group_papd = chg_id + 1;
339 } else if (type == IWM_PHY_DB_CALIB_CHG_TXP) {
340 chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
341 if (phy_db && !phy_db->calib_ch_group_txp) {
343 * Firmware sends the largest index first, so we can use
344 * it to know how much we should allocate.
346 phy_db->calib_ch_group_txp = malloc(
347 (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
348 M_DEVBUF, M_NOWAIT | M_ZERO);
349 if (!phy_db->calib_ch_group_txp)
351 phy_db->n_group_txp = chg_id + 1;
355 entry = iwm_phy_db_get_section(phy_db, type, chg_id);
359 if (entry->data != NULL)
360 free(entry->data, M_DEVBUF);
361 entry->data = malloc(size, M_DEVBUF, M_NOWAIT);
366 memcpy(entry->data, phy_db_notif->data, size);
370 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
371 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
372 __func__, __LINE__, type, size);
378 is_valid_channel(uint16_t ch_id)
381 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
382 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
383 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
389 ch_id_to_ch_index(uint16_t ch_id)
391 if (!is_valid_channel(ch_id))
397 return (ch_id + 20) / 4;
399 return (ch_id - 12) / 4;
400 return (ch_id - 13) / 4;
405 channel_id_to_papd(uint16_t ch_id)
407 if (!is_valid_channel(ch_id))
410 if (1 <= ch_id && ch_id <= 14)
412 if (36 <= ch_id && ch_id <= 64)
414 if (100 <= ch_id && ch_id <= 140)
420 channel_id_to_txp(struct iwm_phy_db *phy_db, uint16_t ch_id)
422 struct iwm_phy_db_chg_txp *txp_chg;
424 uint8_t ch_index = ch_id_to_ch_index(ch_id);
425 if (ch_index == 0xff)
428 for (i = 0; i < phy_db->n_group_txp; i++) {
429 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
433 * Looking for the first channel group that its max channel is
434 * higher then wanted channel.
436 if (le16toh(txp_chg->max_channel_idx) >= ch_index)
443 iwm_phy_db_get_section_data(struct iwm_phy_db *phy_db,
444 uint32_t type, uint8_t **data, uint16_t *size,
447 struct iwm_phy_db_entry *entry;
448 uint16_t ch_group_id = 0;
453 /* find wanted channel group */
454 if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
455 ch_group_id = channel_id_to_papd(ch_id);
456 else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
457 ch_group_id = channel_id_to_txp(phy_db, ch_id);
459 entry = iwm_phy_db_get_section(phy_db, type, ch_group_id);
466 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
467 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
468 __func__, __LINE__, type, *size);
474 iwm_send_phy_db_cmd(struct iwm_phy_db *phy_db, uint16_t type,
475 uint16_t length, void *data)
477 struct iwm_phy_db_cmd phy_db_cmd;
478 struct iwm_host_cmd cmd = {
482 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
483 "Sending PHY-DB hcmd of type %d, of length %d\n",
486 /* Set phy db cmd variables */
487 phy_db_cmd.type = htole16(type);
488 phy_db_cmd.length = htole16(length);
490 /* Set hcmd variables */
491 cmd.data[0] = &phy_db_cmd;
492 cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
496 cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY;
499 return iwm_send_cmd(phy_db->sc, &cmd);
503 iwm_phy_db_send_all_channel_groups(struct iwm_phy_db *phy_db,
504 enum iwm_phy_db_section_type type,
505 uint8_t max_ch_groups)
509 struct iwm_phy_db_entry *entry;
511 /* Send all the channel specific groups to operational fw */
512 for (i = 0; i < max_ch_groups; i++) {
513 entry = iwm_phy_db_get_section(phy_db,
522 /* Send the requested PHY DB section */
523 err = iwm_send_phy_db_cmd(phy_db,
528 device_printf(phy_db->sc->sc_dev,
529 "Can't SEND phy_db section %d (%d), err %d\n",
534 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD,
535 "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
542 iwm_send_phy_db_data(struct iwm_phy_db *phy_db)
544 uint8_t *data = NULL;
548 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
549 "%s: Sending phy db data and configuration to runtime image\n",
552 /* Send PHY DB CFG section */
553 err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CFG,
556 device_printf(phy_db->sc->sc_dev,
557 "%s: Cannot get Phy DB cfg section, %d\n",
562 err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CFG, size, data);
564 device_printf(phy_db->sc->sc_dev,
565 "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
570 err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CALIB_NCH,
573 device_printf(phy_db->sc->sc_dev,
574 "%s: Cannot get Phy DB non specific channel section, "
575 "%d\n", __func__, err);
579 err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CALIB_NCH, size, data);
581 device_printf(phy_db->sc->sc_dev,
582 "%s: Cannot send HCMD of Phy DB non specific channel "
583 "sect, %d\n", __func__, err);
587 /* Send all the TXP channel specific data */
588 err = iwm_phy_db_send_all_channel_groups(phy_db,
589 IWM_PHY_DB_CALIB_CHG_PAPD, phy_db->n_group_papd);
591 device_printf(phy_db->sc->sc_dev,
592 "%s: Cannot send channel specific PAPD groups, %d\n",
597 /* Send all the TXP channel specific data */
598 err = iwm_phy_db_send_all_channel_groups(phy_db,
599 IWM_PHY_DB_CALIB_CHG_TXP, phy_db->n_group_txp);
601 device_printf(phy_db->sc->sc_dev,
602 "%s: Cannot send channel specific TX power groups, "
603 "%d\n", __func__, err);
607 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
608 "%s: Finished sending phy db non channel data\n",