1d4886179SRui Paulo /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */
2d4886179SRui Paulo
3d4886179SRui Paulo /*
4d4886179SRui Paulo * Copyright (c) 2014 genua mbh <info@genua.de>
5d4886179SRui Paulo * Copyright (c) 2014 Fixup Software Ltd.
6d4886179SRui Paulo *
7d4886179SRui Paulo * Permission to use, copy, modify, and distribute this software for any
8d4886179SRui Paulo * purpose with or without fee is hereby granted, provided that the above
9d4886179SRui Paulo * copyright notice and this permission notice appear in all copies.
10d4886179SRui Paulo *
11d4886179SRui Paulo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12d4886179SRui Paulo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13d4886179SRui Paulo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14d4886179SRui Paulo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15d4886179SRui Paulo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16d4886179SRui Paulo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17d4886179SRui Paulo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18d4886179SRui Paulo */
19d4886179SRui Paulo
20d4886179SRui Paulo /*-
21d4886179SRui Paulo * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22d4886179SRui Paulo * which were used as the reference documentation for this implementation.
23d4886179SRui Paulo *
24d4886179SRui Paulo * Driver version we are currently based off of is
25d4886179SRui Paulo * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26d4886179SRui Paulo *
27d4886179SRui Paulo ***********************************************************************
28d4886179SRui Paulo *
29d4886179SRui Paulo * This file is provided under a dual BSD/GPLv2 license. When using or
30d4886179SRui Paulo * redistributing this file, you may do so under either license.
31d4886179SRui Paulo *
32d4886179SRui Paulo * GPL LICENSE SUMMARY
33d4886179SRui Paulo *
34d4886179SRui Paulo * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35d4886179SRui Paulo *
36d4886179SRui Paulo * This program is free software; you can redistribute it and/or modify
37d4886179SRui Paulo * it under the terms of version 2 of the GNU General Public License as
38d4886179SRui Paulo * published by the Free Software Foundation.
39d4886179SRui Paulo *
40d4886179SRui Paulo * This program is distributed in the hope that it will be useful, but
41d4886179SRui Paulo * WITHOUT ANY WARRANTY; without even the implied warranty of
42d4886179SRui Paulo * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
43d4886179SRui Paulo * General Public License for more details.
44d4886179SRui Paulo *
45d4886179SRui Paulo * You should have received a copy of the GNU General Public License
46d4886179SRui Paulo * along with this program; if not, write to the Free Software
47d4886179SRui Paulo * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48d4886179SRui Paulo * USA
49d4886179SRui Paulo *
50d4886179SRui Paulo * The full GNU General Public License is included in this distribution
51d4886179SRui Paulo * in the file called COPYING.
52d4886179SRui Paulo *
53d4886179SRui Paulo * Contact Information:
54d4886179SRui Paulo * Intel Linux Wireless <ilw@linux.intel.com>
55d4886179SRui Paulo * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56d4886179SRui Paulo *
57d4886179SRui Paulo *
58d4886179SRui Paulo * BSD LICENSE
59d4886179SRui Paulo *
60d4886179SRui Paulo * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61d4886179SRui Paulo * All rights reserved.
62d4886179SRui Paulo *
63d4886179SRui Paulo * Redistribution and use in source and binary forms, with or without
64d4886179SRui Paulo * modification, are permitted provided that the following conditions
65d4886179SRui Paulo * are met:
66d4886179SRui Paulo *
67d4886179SRui Paulo * * Redistributions of source code must retain the above copyright
68d4886179SRui Paulo * notice, this list of conditions and the following disclaimer.
69d4886179SRui Paulo * * Redistributions in binary form must reproduce the above copyright
70d4886179SRui Paulo * notice, this list of conditions and the following disclaimer in
71d4886179SRui Paulo * the documentation and/or other materials provided with the
72d4886179SRui Paulo * distribution.
73d4886179SRui Paulo * * Neither the name Intel Corporation nor the names of its
74d4886179SRui Paulo * contributors may be used to endorse or promote products derived
75d4886179SRui Paulo * from this software without specific prior written permission.
76d4886179SRui Paulo *
77d4886179SRui Paulo * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78d4886179SRui Paulo * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79d4886179SRui Paulo * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80d4886179SRui Paulo * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81d4886179SRui Paulo * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82d4886179SRui Paulo * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83d4886179SRui Paulo * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84d4886179SRui Paulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85d4886179SRui Paulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86d4886179SRui Paulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87d4886179SRui Paulo * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88d4886179SRui Paulo */
89d4886179SRui Paulo
90d4886179SRui Paulo /*-
91d4886179SRui Paulo * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92d4886179SRui Paulo *
93d4886179SRui Paulo * Permission to use, copy, modify, and distribute this software for any
94d4886179SRui Paulo * purpose with or without fee is hereby granted, provided that the above
95d4886179SRui Paulo * copyright notice and this permission notice appear in all copies.
96d4886179SRui Paulo *
97d4886179SRui Paulo * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98d4886179SRui Paulo * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99d4886179SRui Paulo * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100d4886179SRui Paulo * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101d4886179SRui Paulo * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102d4886179SRui Paulo * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103d4886179SRui Paulo * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104d4886179SRui Paulo */
105d4886179SRui Paulo #include <sys/cdefs.h>
106b789292fSAndriy Voskoboinyk #include "opt_wlan.h"
107616201d1SAdrian Chadd #include "opt_iwm.h"
108b789292fSAndriy Voskoboinyk
109d4886179SRui Paulo #include <sys/param.h>
110d4886179SRui Paulo #include <sys/bus.h>
111d4886179SRui Paulo #include <sys/conf.h>
112d4886179SRui Paulo #include <sys/endian.h>
113d4886179SRui Paulo #include <sys/firmware.h>
114d4886179SRui Paulo #include <sys/kernel.h>
115d4886179SRui Paulo #include <sys/malloc.h>
116d4886179SRui Paulo #include <sys/mbuf.h>
117d4886179SRui Paulo #include <sys/mutex.h>
118d4886179SRui Paulo #include <sys/module.h>
119d4886179SRui Paulo #include <sys/proc.h>
120d4886179SRui Paulo #include <sys/rman.h>
121d4886179SRui Paulo #include <sys/socket.h>
122d4886179SRui Paulo #include <sys/sockio.h>
123d4886179SRui Paulo #include <sys/sysctl.h>
124d4886179SRui Paulo #include <sys/linker.h>
125d4886179SRui Paulo
126d4886179SRui Paulo #include <machine/bus.h>
127d4886179SRui Paulo #include <machine/endian.h>
128d4886179SRui Paulo #include <machine/resource.h>
129d4886179SRui Paulo
130d4886179SRui Paulo #include <dev/pci/pcivar.h>
131d4886179SRui Paulo #include <dev/pci/pcireg.h>
132d4886179SRui Paulo
133d4886179SRui Paulo #include <net/bpf.h>
134d4886179SRui Paulo
135d4886179SRui Paulo #include <net/if.h>
136d4886179SRui Paulo #include <net/if_var.h>
137d4886179SRui Paulo #include <net/if_arp.h>
138d4886179SRui Paulo #include <net/if_dl.h>
139d4886179SRui Paulo #include <net/if_media.h>
140d4886179SRui Paulo #include <net/if_types.h>
141d4886179SRui Paulo
142d4886179SRui Paulo #include <netinet/in.h>
143d4886179SRui Paulo #include <netinet/in_systm.h>
144d4886179SRui Paulo #include <netinet/if_ether.h>
145d4886179SRui Paulo #include <netinet/ip.h>
146d4886179SRui Paulo
147d4886179SRui Paulo #include <net80211/ieee80211_var.h>
148d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
149d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
150d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
151d4886179SRui Paulo
15230a0fd92SAdrian Chadd #include "if_iwmreg.h"
15330a0fd92SAdrian Chadd #include "if_iwmvar.h"
15430a0fd92SAdrian Chadd #include "if_iwm_debug.h"
15530a0fd92SAdrian Chadd #include "if_iwm_util.h"
15630a0fd92SAdrian Chadd #include "if_iwm_phy_db.h"
15730a0fd92SAdrian Chadd
15830a0fd92SAdrian Chadd #define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */
15930a0fd92SAdrian Chadd
16030a0fd92SAdrian Chadd struct iwm_phy_db_entry {
16130a0fd92SAdrian Chadd uint16_t size;
16230a0fd92SAdrian Chadd uint8_t *data;
16330a0fd92SAdrian Chadd };
16430a0fd92SAdrian Chadd
16530a0fd92SAdrian Chadd /**
16630a0fd92SAdrian Chadd * struct iwm_phy_db - stores phy configuration and calibration data.
16730a0fd92SAdrian Chadd *
16830a0fd92SAdrian Chadd * @cfg: phy configuration.
16930a0fd92SAdrian Chadd * @calib_nch: non channel specific calibration data.
17030a0fd92SAdrian Chadd * @calib_ch: channel specific calibration data.
17130a0fd92SAdrian Chadd * @n_group_papd: number of entries in papd channel group.
17230a0fd92SAdrian Chadd * @calib_ch_group_papd: calibration data related to papd channel group.
17330a0fd92SAdrian Chadd * @n_group_txp: number of entries in tx power channel group.
17430a0fd92SAdrian Chadd * @calib_ch_group_txp: calibration data related to tx power chanel group.
17530a0fd92SAdrian Chadd */
17630a0fd92SAdrian Chadd struct iwm_phy_db {
17730a0fd92SAdrian Chadd struct iwm_phy_db_entry cfg;
17830a0fd92SAdrian Chadd struct iwm_phy_db_entry calib_nch;
17930a0fd92SAdrian Chadd int n_group_papd;
18030a0fd92SAdrian Chadd struct iwm_phy_db_entry *calib_ch_group_papd;
18130a0fd92SAdrian Chadd int n_group_txp;
18230a0fd92SAdrian Chadd struct iwm_phy_db_entry *calib_ch_group_txp;
18330a0fd92SAdrian Chadd
18430a0fd92SAdrian Chadd struct iwm_softc *sc;
18530a0fd92SAdrian Chadd };
18630a0fd92SAdrian Chadd
18730a0fd92SAdrian Chadd struct iwm_phy_db *
iwm_phy_db_init(struct iwm_softc * sc)18830a0fd92SAdrian Chadd iwm_phy_db_init(struct iwm_softc *sc)
18930a0fd92SAdrian Chadd {
19030a0fd92SAdrian Chadd struct iwm_phy_db *phy_db = malloc(sizeof(struct iwm_phy_db),
19130a0fd92SAdrian Chadd M_DEVBUF, M_NOWAIT | M_ZERO);
19230a0fd92SAdrian Chadd
19330a0fd92SAdrian Chadd if (!phy_db)
19430a0fd92SAdrian Chadd return phy_db;
19530a0fd92SAdrian Chadd
19630a0fd92SAdrian Chadd phy_db->sc = sc;
19730a0fd92SAdrian Chadd
19830a0fd92SAdrian Chadd phy_db->n_group_txp = -1;
19930a0fd92SAdrian Chadd phy_db->n_group_papd = -1;
20030a0fd92SAdrian Chadd
20130a0fd92SAdrian Chadd /* TODO: add default values of the phy db. */
20230a0fd92SAdrian Chadd return phy_db;
20330a0fd92SAdrian Chadd }
20430a0fd92SAdrian Chadd
205d4886179SRui Paulo /*
206d4886179SRui Paulo * get phy db section: returns a pointer to a phy db section specified by
207d4886179SRui Paulo * type and channel group id.
208d4886179SRui Paulo */
209d4886179SRui Paulo static struct iwm_phy_db_entry *
iwm_phy_db_get_section(struct iwm_phy_db * phy_db,enum iwm_phy_db_section_type type,uint16_t chg_id)21030a0fd92SAdrian Chadd iwm_phy_db_get_section(struct iwm_phy_db *phy_db,
21130a0fd92SAdrian Chadd enum iwm_phy_db_section_type type,
21230a0fd92SAdrian Chadd uint16_t chg_id)
213d4886179SRui Paulo {
21430a0fd92SAdrian Chadd if (!phy_db || type >= IWM_PHY_DB_MAX)
215d4886179SRui Paulo return NULL;
216d4886179SRui Paulo
217d4886179SRui Paulo switch (type) {
218d4886179SRui Paulo case IWM_PHY_DB_CFG:
219d4886179SRui Paulo return &phy_db->cfg;
220d4886179SRui Paulo case IWM_PHY_DB_CALIB_NCH:
221d4886179SRui Paulo return &phy_db->calib_nch;
222d4886179SRui Paulo case IWM_PHY_DB_CALIB_CHG_PAPD:
22330a0fd92SAdrian Chadd if (chg_id >= phy_db->n_group_papd)
224d4886179SRui Paulo return NULL;
225d4886179SRui Paulo return &phy_db->calib_ch_group_papd[chg_id];
226d4886179SRui Paulo case IWM_PHY_DB_CALIB_CHG_TXP:
22730a0fd92SAdrian Chadd if (chg_id >= phy_db->n_group_txp)
228d4886179SRui Paulo return NULL;
229d4886179SRui Paulo return &phy_db->calib_ch_group_txp[chg_id];
230d4886179SRui Paulo default:
231d4886179SRui Paulo return NULL;
232d4886179SRui Paulo }
233d4886179SRui Paulo return NULL;
234d4886179SRui Paulo }
235d4886179SRui Paulo
23630a0fd92SAdrian Chadd static void
iwm_phy_db_free_section(struct iwm_phy_db * phy_db,enum iwm_phy_db_section_type type,uint16_t chg_id)23730a0fd92SAdrian Chadd iwm_phy_db_free_section(struct iwm_phy_db *phy_db,
23830a0fd92SAdrian Chadd enum iwm_phy_db_section_type type, uint16_t chg_id)
23930a0fd92SAdrian Chadd {
24030a0fd92SAdrian Chadd struct iwm_phy_db_entry *entry =
24130a0fd92SAdrian Chadd iwm_phy_db_get_section(phy_db, type, chg_id);
24230a0fd92SAdrian Chadd if (!entry)
24330a0fd92SAdrian Chadd return;
24430a0fd92SAdrian Chadd
24530a0fd92SAdrian Chadd if (entry->data != NULL)
24630a0fd92SAdrian Chadd free(entry->data, M_DEVBUF);
24730a0fd92SAdrian Chadd entry->data = NULL;
24830a0fd92SAdrian Chadd entry->size = 0;
24930a0fd92SAdrian Chadd }
25030a0fd92SAdrian Chadd
25130a0fd92SAdrian Chadd void
iwm_phy_db_free(struct iwm_phy_db * phy_db)25230a0fd92SAdrian Chadd iwm_phy_db_free(struct iwm_phy_db *phy_db)
25330a0fd92SAdrian Chadd {
25430a0fd92SAdrian Chadd int i;
25530a0fd92SAdrian Chadd
25630a0fd92SAdrian Chadd if (!phy_db)
25730a0fd92SAdrian Chadd return;
25830a0fd92SAdrian Chadd
25930a0fd92SAdrian Chadd iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CFG, 0);
26030a0fd92SAdrian Chadd iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_NCH, 0);
26130a0fd92SAdrian Chadd
26230a0fd92SAdrian Chadd for (i = 0; i < phy_db->n_group_papd; i++)
26330a0fd92SAdrian Chadd iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_PAPD, i);
26430a0fd92SAdrian Chadd if (phy_db->calib_ch_group_papd != NULL)
26530a0fd92SAdrian Chadd free(phy_db->calib_ch_group_papd, M_DEVBUF);
26630a0fd92SAdrian Chadd
26730a0fd92SAdrian Chadd for (i = 0; i < phy_db->n_group_txp; i++)
26830a0fd92SAdrian Chadd iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_TXP, i);
26930a0fd92SAdrian Chadd if (phy_db->calib_ch_group_txp != NULL)
27030a0fd92SAdrian Chadd free(phy_db->calib_ch_group_txp, M_DEVBUF);
27130a0fd92SAdrian Chadd
27230a0fd92SAdrian Chadd free(phy_db, M_DEVBUF);
27330a0fd92SAdrian Chadd }
27430a0fd92SAdrian Chadd
275d4886179SRui Paulo int
iwm_phy_db_set_section(struct iwm_phy_db * phy_db,struct iwm_rx_packet * pkt)27630a0fd92SAdrian Chadd iwm_phy_db_set_section(struct iwm_phy_db *phy_db,
27742155115SAdrian Chadd struct iwm_rx_packet *pkt)
278d4886179SRui Paulo {
27942155115SAdrian Chadd struct iwm_calib_res_notif_phy_db *phy_db_notif =
28042155115SAdrian Chadd (struct iwm_calib_res_notif_phy_db *)pkt->data;
281d4886179SRui Paulo enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
282d4886179SRui Paulo uint16_t size = le16toh(phy_db_notif->length);
283d4886179SRui Paulo struct iwm_phy_db_entry *entry;
284d4886179SRui Paulo uint16_t chg_id = 0;
285d4886179SRui Paulo
28630a0fd92SAdrian Chadd if (!phy_db)
28730a0fd92SAdrian Chadd return EINVAL;
288d4886179SRui Paulo
28930a0fd92SAdrian Chadd if (type == IWM_PHY_DB_CALIB_CHG_PAPD) {
29030a0fd92SAdrian Chadd chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
29130a0fd92SAdrian Chadd if (phy_db && !phy_db->calib_ch_group_papd) {
29230a0fd92SAdrian Chadd /*
29330a0fd92SAdrian Chadd * Firmware sends the largest index first, so we can use
29430a0fd92SAdrian Chadd * it to know how much we should allocate.
29530a0fd92SAdrian Chadd */
29630a0fd92SAdrian Chadd phy_db->calib_ch_group_papd = malloc(
29730a0fd92SAdrian Chadd (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
29830a0fd92SAdrian Chadd M_DEVBUF, M_NOWAIT | M_ZERO);
29930a0fd92SAdrian Chadd if (!phy_db->calib_ch_group_papd)
30030a0fd92SAdrian Chadd return ENOMEM;
30130a0fd92SAdrian Chadd phy_db->n_group_papd = chg_id + 1;
30230a0fd92SAdrian Chadd }
30330a0fd92SAdrian Chadd } else if (type == IWM_PHY_DB_CALIB_CHG_TXP) {
30430a0fd92SAdrian Chadd chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
30530a0fd92SAdrian Chadd if (phy_db && !phy_db->calib_ch_group_txp) {
30630a0fd92SAdrian Chadd /*
30730a0fd92SAdrian Chadd * Firmware sends the largest index first, so we can use
30830a0fd92SAdrian Chadd * it to know how much we should allocate.
30930a0fd92SAdrian Chadd */
31030a0fd92SAdrian Chadd phy_db->calib_ch_group_txp = malloc(
31130a0fd92SAdrian Chadd (chg_id + 1) * sizeof(struct iwm_phy_db_entry),
31230a0fd92SAdrian Chadd M_DEVBUF, M_NOWAIT | M_ZERO);
31330a0fd92SAdrian Chadd if (!phy_db->calib_ch_group_txp)
31430a0fd92SAdrian Chadd return ENOMEM;
31530a0fd92SAdrian Chadd phy_db->n_group_txp = chg_id + 1;
31630a0fd92SAdrian Chadd }
31730a0fd92SAdrian Chadd }
31830a0fd92SAdrian Chadd
31930a0fd92SAdrian Chadd entry = iwm_phy_db_get_section(phy_db, type, chg_id);
320d4886179SRui Paulo if (!entry)
321d4886179SRui Paulo return EINVAL;
322d4886179SRui Paulo
32330a0fd92SAdrian Chadd if (entry->data != NULL)
324d4886179SRui Paulo free(entry->data, M_DEVBUF);
325d4886179SRui Paulo entry->data = malloc(size, M_DEVBUF, M_NOWAIT);
326d4886179SRui Paulo if (!entry->data) {
327d4886179SRui Paulo entry->size = 0;
328d4886179SRui Paulo return ENOMEM;
329d4886179SRui Paulo }
330d4886179SRui Paulo memcpy(entry->data, phy_db_notif->data, size);
33130a0fd92SAdrian Chadd
332d4886179SRui Paulo entry->size = size;
333d4886179SRui Paulo
33430a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
33530a0fd92SAdrian Chadd "%s(%d): [PHYDB]SET: Type %d , Size: %d\n",
33630a0fd92SAdrian Chadd __func__, __LINE__, type, size);
337d4886179SRui Paulo
338d4886179SRui Paulo return 0;
339d4886179SRui Paulo }
340d4886179SRui Paulo
341d4886179SRui Paulo static int
is_valid_channel(uint16_t ch_id)34230a0fd92SAdrian Chadd is_valid_channel(uint16_t ch_id)
343d4886179SRui Paulo {
344d4886179SRui Paulo if (ch_id <= 14 ||
345d4886179SRui Paulo (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
346d4886179SRui Paulo (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
347d4886179SRui Paulo (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
348d4886179SRui Paulo return 1;
349d4886179SRui Paulo return 0;
350d4886179SRui Paulo }
351d4886179SRui Paulo
352d4886179SRui Paulo static uint8_t
ch_id_to_ch_index(uint16_t ch_id)35330a0fd92SAdrian Chadd ch_id_to_ch_index(uint16_t ch_id)
354d4886179SRui Paulo {
35530a0fd92SAdrian Chadd if (!is_valid_channel(ch_id))
356d4886179SRui Paulo return 0xff;
357d4886179SRui Paulo
358d4886179SRui Paulo if (ch_id <= 14)
359d4886179SRui Paulo return ch_id - 1;
360d4886179SRui Paulo if (ch_id <= 64)
361d4886179SRui Paulo return (ch_id + 20) / 4;
362d4886179SRui Paulo if (ch_id <= 140)
363d4886179SRui Paulo return (ch_id - 12) / 4;
364d4886179SRui Paulo return (ch_id - 13) / 4;
365d4886179SRui Paulo }
366d4886179SRui Paulo
367d4886179SRui Paulo
368d4886179SRui Paulo static uint16_t
channel_id_to_papd(uint16_t ch_id)36930a0fd92SAdrian Chadd channel_id_to_papd(uint16_t ch_id)
370d4886179SRui Paulo {
37130a0fd92SAdrian Chadd if (!is_valid_channel(ch_id))
372d4886179SRui Paulo return 0xff;
373d4886179SRui Paulo
374d4886179SRui Paulo if (1 <= ch_id && ch_id <= 14)
375d4886179SRui Paulo return 0;
376d4886179SRui Paulo if (36 <= ch_id && ch_id <= 64)
377d4886179SRui Paulo return 1;
378d4886179SRui Paulo if (100 <= ch_id && ch_id <= 140)
379d4886179SRui Paulo return 2;
380d4886179SRui Paulo return 3;
381d4886179SRui Paulo }
382d4886179SRui Paulo
383d4886179SRui Paulo static uint16_t
channel_id_to_txp(struct iwm_phy_db * phy_db,uint16_t ch_id)38430a0fd92SAdrian Chadd channel_id_to_txp(struct iwm_phy_db *phy_db, uint16_t ch_id)
385d4886179SRui Paulo {
386d4886179SRui Paulo struct iwm_phy_db_chg_txp *txp_chg;
387d4886179SRui Paulo int i;
38830a0fd92SAdrian Chadd uint8_t ch_index = ch_id_to_ch_index(ch_id);
389d4886179SRui Paulo if (ch_index == 0xff)
390d4886179SRui Paulo return 0xff;
391d4886179SRui Paulo
39230a0fd92SAdrian Chadd for (i = 0; i < phy_db->n_group_txp; i++) {
393d4886179SRui Paulo txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
394d4886179SRui Paulo if (!txp_chg)
395d4886179SRui Paulo return 0xff;
396d4886179SRui Paulo /*
397d4886179SRui Paulo * Looking for the first channel group that its max channel is
398d4886179SRui Paulo * higher then wanted channel.
399d4886179SRui Paulo */
400d4886179SRui Paulo if (le16toh(txp_chg->max_channel_idx) >= ch_index)
401d4886179SRui Paulo return i;
402d4886179SRui Paulo }
403d4886179SRui Paulo return 0xff;
404d4886179SRui Paulo }
405d4886179SRui Paulo
406d4886179SRui Paulo static int
iwm_phy_db_get_section_data(struct iwm_phy_db * phy_db,uint32_t type,uint8_t ** data,uint16_t * size,uint16_t ch_id)40730a0fd92SAdrian Chadd iwm_phy_db_get_section_data(struct iwm_phy_db *phy_db,
40830a0fd92SAdrian Chadd uint32_t type, uint8_t **data, uint16_t *size,
40930a0fd92SAdrian Chadd uint16_t ch_id)
410d4886179SRui Paulo {
411d4886179SRui Paulo struct iwm_phy_db_entry *entry;
412d4886179SRui Paulo uint16_t ch_group_id = 0;
413d4886179SRui Paulo
41430a0fd92SAdrian Chadd if (!phy_db)
41530a0fd92SAdrian Chadd return EINVAL;
41630a0fd92SAdrian Chadd
417d4886179SRui Paulo /* find wanted channel group */
418d4886179SRui Paulo if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
41930a0fd92SAdrian Chadd ch_group_id = channel_id_to_papd(ch_id);
420d4886179SRui Paulo else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
42130a0fd92SAdrian Chadd ch_group_id = channel_id_to_txp(phy_db, ch_id);
422d4886179SRui Paulo
42330a0fd92SAdrian Chadd entry = iwm_phy_db_get_section(phy_db, type, ch_group_id);
424d4886179SRui Paulo if (!entry)
425d4886179SRui Paulo return EINVAL;
426d4886179SRui Paulo
427d4886179SRui Paulo *data = entry->data;
428d4886179SRui Paulo *size = entry->size;
429d4886179SRui Paulo
43030a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
431d4886179SRui Paulo "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
432d4886179SRui Paulo __func__, __LINE__, type, *size);
433d4886179SRui Paulo
434d4886179SRui Paulo return 0;
435d4886179SRui Paulo }
436d4886179SRui Paulo
437d4886179SRui Paulo static int
iwm_send_phy_db_cmd(struct iwm_phy_db * phy_db,uint16_t type,uint16_t length,void * data)43830a0fd92SAdrian Chadd iwm_send_phy_db_cmd(struct iwm_phy_db *phy_db, uint16_t type,
439d4886179SRui Paulo uint16_t length, void *data)
440d4886179SRui Paulo {
441d4886179SRui Paulo struct iwm_phy_db_cmd phy_db_cmd;
442d4886179SRui Paulo struct iwm_host_cmd cmd = {
443*355c1513SAdrian Chadd .id = IWM_PHY_DB_CMD,
444d4886179SRui Paulo };
445d4886179SRui Paulo
44630a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET,
447d4886179SRui Paulo "Sending PHY-DB hcmd of type %d, of length %d\n",
448d4886179SRui Paulo type, length);
449d4886179SRui Paulo
450d4886179SRui Paulo /* Set phy db cmd variables */
45130a0fd92SAdrian Chadd phy_db_cmd.type = htole16(type);
45230a0fd92SAdrian Chadd phy_db_cmd.length = htole16(length);
453d4886179SRui Paulo
454d4886179SRui Paulo /* Set hcmd variables */
455d4886179SRui Paulo cmd.data[0] = &phy_db_cmd;
456d4886179SRui Paulo cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
457d4886179SRui Paulo cmd.data[1] = data;
458d4886179SRui Paulo cmd.len[1] = length;
45930a0fd92SAdrian Chadd #ifdef notyet
46030a0fd92SAdrian Chadd cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY;
46130a0fd92SAdrian Chadd #endif
462d4886179SRui Paulo
46330a0fd92SAdrian Chadd return iwm_send_cmd(phy_db->sc, &cmd);
464d4886179SRui Paulo }
465d4886179SRui Paulo
466d4886179SRui Paulo static int
iwm_phy_db_send_all_channel_groups(struct iwm_phy_db * phy_db,enum iwm_phy_db_section_type type,uint8_t max_ch_groups)46730a0fd92SAdrian Chadd iwm_phy_db_send_all_channel_groups(struct iwm_phy_db *phy_db,
46830a0fd92SAdrian Chadd enum iwm_phy_db_section_type type,
46930a0fd92SAdrian Chadd uint8_t max_ch_groups)
470d4886179SRui Paulo {
471d4886179SRui Paulo uint16_t i;
472d4886179SRui Paulo int err;
473d4886179SRui Paulo struct iwm_phy_db_entry *entry;
474d4886179SRui Paulo
47530a0fd92SAdrian Chadd /* Send all the channel specific groups to operational fw */
476d4886179SRui Paulo for (i = 0; i < max_ch_groups; i++) {
47730a0fd92SAdrian Chadd entry = iwm_phy_db_get_section(phy_db,
47830a0fd92SAdrian Chadd type,
47930a0fd92SAdrian Chadd i);
480d4886179SRui Paulo if (!entry)
481d4886179SRui Paulo return EINVAL;
482d4886179SRui Paulo
483d4886179SRui Paulo if (!entry->size)
484d4886179SRui Paulo continue;
485d4886179SRui Paulo
486d4886179SRui Paulo /* Send the requested PHY DB section */
48730a0fd92SAdrian Chadd err = iwm_send_phy_db_cmd(phy_db,
48830a0fd92SAdrian Chadd type,
48930a0fd92SAdrian Chadd entry->size,
49030a0fd92SAdrian Chadd entry->data);
491d4886179SRui Paulo if (err) {
49230a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
49330a0fd92SAdrian Chadd "Can't SEND phy_db section %d (%d), err %d\n",
49430a0fd92SAdrian Chadd type, i, err);
495d4886179SRui Paulo return err;
496d4886179SRui Paulo }
497d4886179SRui Paulo
49830a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD,
499d4886179SRui Paulo "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
500d4886179SRui Paulo }
501d4886179SRui Paulo
502d4886179SRui Paulo return 0;
503d4886179SRui Paulo }
504d4886179SRui Paulo
505d4886179SRui Paulo int
iwm_send_phy_db_data(struct iwm_phy_db * phy_db)50630a0fd92SAdrian Chadd iwm_send_phy_db_data(struct iwm_phy_db *phy_db)
507d4886179SRui Paulo {
508d4886179SRui Paulo uint8_t *data = NULL;
509d4886179SRui Paulo uint16_t size = 0;
510d4886179SRui Paulo int err;
511d4886179SRui Paulo
51230a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
513d4886179SRui Paulo "%s: Sending phy db data and configuration to runtime image\n",
514d4886179SRui Paulo __func__);
515d4886179SRui Paulo
516d4886179SRui Paulo /* Send PHY DB CFG section */
51730a0fd92SAdrian Chadd err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CFG,
51830a0fd92SAdrian Chadd &data, &size, 0);
519d4886179SRui Paulo if (err) {
52030a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
521d4886179SRui Paulo "%s: Cannot get Phy DB cfg section, %d\n",
522d4886179SRui Paulo __func__, err);
523d4886179SRui Paulo return err;
524d4886179SRui Paulo }
525d4886179SRui Paulo
52630a0fd92SAdrian Chadd err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CFG, size, data);
527d4886179SRui Paulo if (err) {
52830a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
529d4886179SRui Paulo "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
530d4886179SRui Paulo __func__, err);
531d4886179SRui Paulo return err;
532d4886179SRui Paulo }
533d4886179SRui Paulo
53430a0fd92SAdrian Chadd err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CALIB_NCH,
535d4886179SRui Paulo &data, &size, 0);
536d4886179SRui Paulo if (err) {
53730a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
538d4886179SRui Paulo "%s: Cannot get Phy DB non specific channel section, "
539d4886179SRui Paulo "%d\n", __func__, err);
540d4886179SRui Paulo return err;
541d4886179SRui Paulo }
542d4886179SRui Paulo
54330a0fd92SAdrian Chadd err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CALIB_NCH, size, data);
544d4886179SRui Paulo if (err) {
54530a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
546d4886179SRui Paulo "%s: Cannot send HCMD of Phy DB non specific channel "
547d4886179SRui Paulo "sect, %d\n", __func__, err);
548d4886179SRui Paulo return err;
549d4886179SRui Paulo }
550d4886179SRui Paulo
551d4886179SRui Paulo /* Send all the TXP channel specific data */
55230a0fd92SAdrian Chadd err = iwm_phy_db_send_all_channel_groups(phy_db,
55330a0fd92SAdrian Chadd IWM_PHY_DB_CALIB_CHG_PAPD, phy_db->n_group_papd);
554d4886179SRui Paulo if (err) {
55530a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
556d4886179SRui Paulo "%s: Cannot send channel specific PAPD groups, %d\n",
557d4886179SRui Paulo __func__, err);
558d4886179SRui Paulo return err;
559d4886179SRui Paulo }
560d4886179SRui Paulo
561d4886179SRui Paulo /* Send all the TXP channel specific data */
56230a0fd92SAdrian Chadd err = iwm_phy_db_send_all_channel_groups(phy_db,
56330a0fd92SAdrian Chadd IWM_PHY_DB_CALIB_CHG_TXP, phy_db->n_group_txp);
564d4886179SRui Paulo if (err) {
56530a0fd92SAdrian Chadd device_printf(phy_db->sc->sc_dev,
566d4886179SRui Paulo "%s: Cannot send channel specific TX power groups, "
567d4886179SRui Paulo "%d\n", __func__, err);
568d4886179SRui Paulo return err;
569d4886179SRui Paulo }
570d4886179SRui Paulo
57130a0fd92SAdrian Chadd IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
572d4886179SRui Paulo "%s: Finished sending phy db non channel data\n",
573d4886179SRui Paulo __func__);
574d4886179SRui Paulo return 0;
575d4886179SRui Paulo }
576