xref: /freebsd/sys/dev/iwm/if_iwm_phy_db.c (revision d4886179cbebc2985084fb55240f87dbeb7909a3)
1*d4886179SRui Paulo /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2*d4886179SRui Paulo 
3*d4886179SRui Paulo /*
4*d4886179SRui Paulo  * Copyright (c) 2014 genua mbh <info@genua.de>
5*d4886179SRui Paulo  * Copyright (c) 2014 Fixup Software Ltd.
6*d4886179SRui Paulo  *
7*d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
8*d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
9*d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
10*d4886179SRui Paulo  *
11*d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12*d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13*d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14*d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15*d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16*d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17*d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*d4886179SRui Paulo  */
19*d4886179SRui Paulo 
20*d4886179SRui Paulo /*-
21*d4886179SRui Paulo  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22*d4886179SRui Paulo  * which were used as the reference documentation for this implementation.
23*d4886179SRui Paulo  *
24*d4886179SRui Paulo  * Driver version we are currently based off of is
25*d4886179SRui Paulo  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26*d4886179SRui Paulo  *
27*d4886179SRui Paulo  ***********************************************************************
28*d4886179SRui Paulo  *
29*d4886179SRui Paulo  * This file is provided under a dual BSD/GPLv2 license.  When using or
30*d4886179SRui Paulo  * redistributing this file, you may do so under either license.
31*d4886179SRui Paulo  *
32*d4886179SRui Paulo  * GPL LICENSE SUMMARY
33*d4886179SRui Paulo  *
34*d4886179SRui Paulo  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35*d4886179SRui Paulo  *
36*d4886179SRui Paulo  * This program is free software; you can redistribute it and/or modify
37*d4886179SRui Paulo  * it under the terms of version 2 of the GNU General Public License as
38*d4886179SRui Paulo  * published by the Free Software Foundation.
39*d4886179SRui Paulo  *
40*d4886179SRui Paulo  * This program is distributed in the hope that it will be useful, but
41*d4886179SRui Paulo  * WITHOUT ANY WARRANTY; without even the implied warranty of
42*d4886179SRui Paulo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43*d4886179SRui Paulo  * General Public License for more details.
44*d4886179SRui Paulo  *
45*d4886179SRui Paulo  * You should have received a copy of the GNU General Public License
46*d4886179SRui Paulo  * along with this program; if not, write to the Free Software
47*d4886179SRui Paulo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48*d4886179SRui Paulo  * USA
49*d4886179SRui Paulo  *
50*d4886179SRui Paulo  * The full GNU General Public License is included in this distribution
51*d4886179SRui Paulo  * in the file called COPYING.
52*d4886179SRui Paulo  *
53*d4886179SRui Paulo  * Contact Information:
54*d4886179SRui Paulo  *  Intel Linux Wireless <ilw@linux.intel.com>
55*d4886179SRui Paulo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56*d4886179SRui Paulo  *
57*d4886179SRui Paulo  *
58*d4886179SRui Paulo  * BSD LICENSE
59*d4886179SRui Paulo  *
60*d4886179SRui Paulo  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61*d4886179SRui Paulo  * All rights reserved.
62*d4886179SRui Paulo  *
63*d4886179SRui Paulo  * Redistribution and use in source and binary forms, with or without
64*d4886179SRui Paulo  * modification, are permitted provided that the following conditions
65*d4886179SRui Paulo  * are met:
66*d4886179SRui Paulo  *
67*d4886179SRui Paulo  *  * Redistributions of source code must retain the above copyright
68*d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer.
69*d4886179SRui Paulo  *  * Redistributions in binary form must reproduce the above copyright
70*d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer in
71*d4886179SRui Paulo  *    the documentation and/or other materials provided with the
72*d4886179SRui Paulo  *    distribution.
73*d4886179SRui Paulo  *  * Neither the name Intel Corporation nor the names of its
74*d4886179SRui Paulo  *    contributors may be used to endorse or promote products derived
75*d4886179SRui Paulo  *    from this software without specific prior written permission.
76*d4886179SRui Paulo  *
77*d4886179SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78*d4886179SRui Paulo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79*d4886179SRui Paulo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80*d4886179SRui Paulo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81*d4886179SRui Paulo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82*d4886179SRui Paulo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83*d4886179SRui Paulo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84*d4886179SRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85*d4886179SRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86*d4886179SRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87*d4886179SRui Paulo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88*d4886179SRui Paulo  */
89*d4886179SRui Paulo 
90*d4886179SRui Paulo /*-
91*d4886179SRui Paulo  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92*d4886179SRui Paulo  *
93*d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
94*d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
95*d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
96*d4886179SRui Paulo  *
97*d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98*d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99*d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100*d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101*d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102*d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103*d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104*d4886179SRui Paulo  */
105*d4886179SRui Paulo #include <sys/cdefs.h>
106*d4886179SRui Paulo __FBSDID("$FreeBSD$");
107*d4886179SRui Paulo 
108*d4886179SRui Paulo #include <sys/param.h>
109*d4886179SRui Paulo #include <sys/bus.h>
110*d4886179SRui Paulo #include <sys/conf.h>
111*d4886179SRui Paulo #include <sys/endian.h>
112*d4886179SRui Paulo #include <sys/firmware.h>
113*d4886179SRui Paulo #include <sys/kernel.h>
114*d4886179SRui Paulo #include <sys/malloc.h>
115*d4886179SRui Paulo #include <sys/mbuf.h>
116*d4886179SRui Paulo #include <sys/mutex.h>
117*d4886179SRui Paulo #include <sys/module.h>
118*d4886179SRui Paulo #include <sys/proc.h>
119*d4886179SRui Paulo #include <sys/rman.h>
120*d4886179SRui Paulo #include <sys/socket.h>
121*d4886179SRui Paulo #include <sys/sockio.h>
122*d4886179SRui Paulo #include <sys/sysctl.h>
123*d4886179SRui Paulo #include <sys/linker.h>
124*d4886179SRui Paulo 
125*d4886179SRui Paulo #include <machine/bus.h>
126*d4886179SRui Paulo #include <machine/endian.h>
127*d4886179SRui Paulo #include <machine/resource.h>
128*d4886179SRui Paulo 
129*d4886179SRui Paulo #include <dev/pci/pcivar.h>
130*d4886179SRui Paulo #include <dev/pci/pcireg.h>
131*d4886179SRui Paulo 
132*d4886179SRui Paulo #include <net/bpf.h>
133*d4886179SRui Paulo 
134*d4886179SRui Paulo #include <net/if.h>
135*d4886179SRui Paulo #include <net/if_var.h>
136*d4886179SRui Paulo #include <net/if_arp.h>
137*d4886179SRui Paulo #include <net/if_dl.h>
138*d4886179SRui Paulo #include <net/if_media.h>
139*d4886179SRui Paulo #include <net/if_types.h>
140*d4886179SRui Paulo 
141*d4886179SRui Paulo #include <netinet/in.h>
142*d4886179SRui Paulo #include <netinet/in_systm.h>
143*d4886179SRui Paulo #include <netinet/if_ether.h>
144*d4886179SRui Paulo #include <netinet/ip.h>
145*d4886179SRui Paulo 
146*d4886179SRui Paulo #include <net80211/ieee80211_var.h>
147*d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
148*d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
149*d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
150*d4886179SRui Paulo 
151*d4886179SRui Paulo #include <if_iwmreg.h>
152*d4886179SRui Paulo #include <if_iwmvar.h>
153*d4886179SRui Paulo #include <if_iwm_debug.h>
154*d4886179SRui Paulo #include <if_iwm_util.h>
155*d4886179SRui Paulo #include <if_iwm_phy_db.h>
156*d4886179SRui Paulo 
157*d4886179SRui Paulo /*
158*d4886179SRui Paulo  * BEGIN iwl-phy-db.c
159*d4886179SRui Paulo  */
160*d4886179SRui Paulo /*
161*d4886179SRui Paulo  * get phy db section: returns a pointer to a phy db section specified by
162*d4886179SRui Paulo  * type and channel group id.
163*d4886179SRui Paulo  */
164*d4886179SRui Paulo static struct iwm_phy_db_entry *
165*d4886179SRui Paulo iwm_phy_db_get_section(struct iwm_softc *sc,
166*d4886179SRui Paulo 	enum iwm_phy_db_section_type type, uint16_t chg_id)
167*d4886179SRui Paulo {
168*d4886179SRui Paulo 	struct iwm_phy_db *phy_db = &sc->sc_phy_db;
169*d4886179SRui Paulo 
170*d4886179SRui Paulo 	if (type >= IWM_PHY_DB_MAX)
171*d4886179SRui Paulo 		return NULL;
172*d4886179SRui Paulo 
173*d4886179SRui Paulo 	switch (type) {
174*d4886179SRui Paulo 	case IWM_PHY_DB_CFG:
175*d4886179SRui Paulo 		return &phy_db->cfg;
176*d4886179SRui Paulo 	case IWM_PHY_DB_CALIB_NCH:
177*d4886179SRui Paulo 		return &phy_db->calib_nch;
178*d4886179SRui Paulo 	case IWM_PHY_DB_CALIB_CHG_PAPD:
179*d4886179SRui Paulo 		if (chg_id >= IWM_NUM_PAPD_CH_GROUPS)
180*d4886179SRui Paulo 			return NULL;
181*d4886179SRui Paulo 		return &phy_db->calib_ch_group_papd[chg_id];
182*d4886179SRui Paulo 	case IWM_PHY_DB_CALIB_CHG_TXP:
183*d4886179SRui Paulo 		if (chg_id >= IWM_NUM_TXP_CH_GROUPS)
184*d4886179SRui Paulo 			return NULL;
185*d4886179SRui Paulo 		return &phy_db->calib_ch_group_txp[chg_id];
186*d4886179SRui Paulo 	default:
187*d4886179SRui Paulo 		return NULL;
188*d4886179SRui Paulo 	}
189*d4886179SRui Paulo 	return NULL;
190*d4886179SRui Paulo }
191*d4886179SRui Paulo 
192*d4886179SRui Paulo int
193*d4886179SRui Paulo iwm_phy_db_set_section(struct iwm_softc *sc,
194*d4886179SRui Paulo 	struct iwm_calib_res_notif_phy_db *phy_db_notif)
195*d4886179SRui Paulo {
196*d4886179SRui Paulo 	enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type);
197*d4886179SRui Paulo 	uint16_t size  = le16toh(phy_db_notif->length);
198*d4886179SRui Paulo 	struct iwm_phy_db_entry *entry;
199*d4886179SRui Paulo 	uint16_t chg_id = 0;
200*d4886179SRui Paulo 
201*d4886179SRui Paulo 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD ||
202*d4886179SRui Paulo 	    type == IWM_PHY_DB_CALIB_CHG_TXP)
203*d4886179SRui Paulo 		chg_id = le16toh(*(uint16_t *)phy_db_notif->data);
204*d4886179SRui Paulo 
205*d4886179SRui Paulo 	entry = iwm_phy_db_get_section(sc, type, chg_id);
206*d4886179SRui Paulo 	if (!entry)
207*d4886179SRui Paulo 		return EINVAL;
208*d4886179SRui Paulo 
209*d4886179SRui Paulo 	if (entry->data)
210*d4886179SRui Paulo 		free(entry->data, M_DEVBUF);
211*d4886179SRui Paulo 	entry->data = malloc(size, M_DEVBUF, M_NOWAIT);
212*d4886179SRui Paulo 	if (!entry->data) {
213*d4886179SRui Paulo 		entry->size = 0;
214*d4886179SRui Paulo 		return ENOMEM;
215*d4886179SRui Paulo 	}
216*d4886179SRui Paulo 	memcpy(entry->data, phy_db_notif->data, size);
217*d4886179SRui Paulo 	entry->size = size;
218*d4886179SRui Paulo 
219*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
220*d4886179SRui Paulo 	    "%s(%d): [PHYDB]SET: Type %d , Size: %d, data: %p\n",
221*d4886179SRui Paulo 	    __func__, __LINE__, type, size, entry->data);
222*d4886179SRui Paulo 
223*d4886179SRui Paulo 	return 0;
224*d4886179SRui Paulo }
225*d4886179SRui Paulo 
226*d4886179SRui Paulo static int
227*d4886179SRui Paulo iwm_is_valid_channel(uint16_t ch_id)
228*d4886179SRui Paulo {
229*d4886179SRui Paulo 	if (ch_id <= 14 ||
230*d4886179SRui Paulo 	    (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) ||
231*d4886179SRui Paulo 	    (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) ||
232*d4886179SRui Paulo 	    (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1))
233*d4886179SRui Paulo 		return 1;
234*d4886179SRui Paulo 	return 0;
235*d4886179SRui Paulo }
236*d4886179SRui Paulo 
237*d4886179SRui Paulo static uint8_t
238*d4886179SRui Paulo iwm_ch_id_to_ch_index(uint16_t ch_id)
239*d4886179SRui Paulo {
240*d4886179SRui Paulo 	if (!iwm_is_valid_channel(ch_id))
241*d4886179SRui Paulo 		return 0xff;
242*d4886179SRui Paulo 
243*d4886179SRui Paulo 	if (ch_id <= 14)
244*d4886179SRui Paulo 		return ch_id - 1;
245*d4886179SRui Paulo 	if (ch_id <= 64)
246*d4886179SRui Paulo 		return (ch_id + 20) / 4;
247*d4886179SRui Paulo 	if (ch_id <= 140)
248*d4886179SRui Paulo 		return (ch_id - 12) / 4;
249*d4886179SRui Paulo 	return (ch_id - 13) / 4;
250*d4886179SRui Paulo }
251*d4886179SRui Paulo 
252*d4886179SRui Paulo 
253*d4886179SRui Paulo static uint16_t
254*d4886179SRui Paulo iwm_channel_id_to_papd(uint16_t ch_id)
255*d4886179SRui Paulo {
256*d4886179SRui Paulo 	if (!iwm_is_valid_channel(ch_id))
257*d4886179SRui Paulo 		return 0xff;
258*d4886179SRui Paulo 
259*d4886179SRui Paulo 	if (1 <= ch_id && ch_id <= 14)
260*d4886179SRui Paulo 		return 0;
261*d4886179SRui Paulo 	if (36 <= ch_id && ch_id <= 64)
262*d4886179SRui Paulo 		return 1;
263*d4886179SRui Paulo 	if (100 <= ch_id && ch_id <= 140)
264*d4886179SRui Paulo 		return 2;
265*d4886179SRui Paulo 	return 3;
266*d4886179SRui Paulo }
267*d4886179SRui Paulo 
268*d4886179SRui Paulo static uint16_t
269*d4886179SRui Paulo iwm_channel_id_to_txp(struct iwm_softc *sc, uint16_t ch_id)
270*d4886179SRui Paulo {
271*d4886179SRui Paulo 	struct iwm_phy_db *phy_db = &sc->sc_phy_db;
272*d4886179SRui Paulo 	struct iwm_phy_db_chg_txp *txp_chg;
273*d4886179SRui Paulo 	int i;
274*d4886179SRui Paulo 	uint8_t ch_index = iwm_ch_id_to_ch_index(ch_id);
275*d4886179SRui Paulo 
276*d4886179SRui Paulo 	if (ch_index == 0xff)
277*d4886179SRui Paulo 		return 0xff;
278*d4886179SRui Paulo 
279*d4886179SRui Paulo 	for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++) {
280*d4886179SRui Paulo 		txp_chg = (void *)phy_db->calib_ch_group_txp[i].data;
281*d4886179SRui Paulo 		if (!txp_chg)
282*d4886179SRui Paulo 			return 0xff;
283*d4886179SRui Paulo 		/*
284*d4886179SRui Paulo 		 * Looking for the first channel group that its max channel is
285*d4886179SRui Paulo 		 * higher then wanted channel.
286*d4886179SRui Paulo 		 */
287*d4886179SRui Paulo 		if (le16toh(txp_chg->max_channel_idx) >= ch_index)
288*d4886179SRui Paulo 			return i;
289*d4886179SRui Paulo 	}
290*d4886179SRui Paulo 	return 0xff;
291*d4886179SRui Paulo }
292*d4886179SRui Paulo 
293*d4886179SRui Paulo static int
294*d4886179SRui Paulo iwm_phy_db_get_section_data(struct iwm_softc *sc,
295*d4886179SRui Paulo 	uint32_t type, uint8_t **data, uint16_t *size, uint16_t ch_id)
296*d4886179SRui Paulo {
297*d4886179SRui Paulo 	struct iwm_phy_db_entry *entry;
298*d4886179SRui Paulo 	uint16_t ch_group_id = 0;
299*d4886179SRui Paulo 
300*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__);
301*d4886179SRui Paulo 	/* find wanted channel group */
302*d4886179SRui Paulo 	if (type == IWM_PHY_DB_CALIB_CHG_PAPD)
303*d4886179SRui Paulo 		ch_group_id = iwm_channel_id_to_papd(ch_id);
304*d4886179SRui Paulo 	else if (type == IWM_PHY_DB_CALIB_CHG_TXP)
305*d4886179SRui Paulo 		ch_group_id = iwm_channel_id_to_txp(sc, ch_id);
306*d4886179SRui Paulo 
307*d4886179SRui Paulo 	entry = iwm_phy_db_get_section(sc, type, ch_group_id);
308*d4886179SRui Paulo 	if (!entry)
309*d4886179SRui Paulo 		return EINVAL;
310*d4886179SRui Paulo 
311*d4886179SRui Paulo 	*data = entry->data;
312*d4886179SRui Paulo 	*size = entry->size;
313*d4886179SRui Paulo 
314*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_RESET,
315*d4886179SRui Paulo 	    "%s(%d): [PHYDB] GET: Type %d , Size: %d\n",
316*d4886179SRui Paulo 	    __func__, __LINE__, type, *size);
317*d4886179SRui Paulo 
318*d4886179SRui Paulo 	return 0;
319*d4886179SRui Paulo }
320*d4886179SRui Paulo 
321*d4886179SRui Paulo static int
322*d4886179SRui Paulo iwm_send_phy_db_cmd(struct iwm_softc *sc, uint16_t type,
323*d4886179SRui Paulo 	uint16_t length, void *data)
324*d4886179SRui Paulo {
325*d4886179SRui Paulo 	struct iwm_phy_db_cmd phy_db_cmd;
326*d4886179SRui Paulo 	struct iwm_host_cmd cmd = {
327*d4886179SRui Paulo 		.id = IWM_PHY_DB_CMD,
328*d4886179SRui Paulo 		.flags = IWM_CMD_SYNC,
329*d4886179SRui Paulo 	};
330*d4886179SRui Paulo 
331*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
332*d4886179SRui Paulo 	    "Sending PHY-DB hcmd of type %d, of length %d\n",
333*d4886179SRui Paulo 	    type, length);
334*d4886179SRui Paulo 
335*d4886179SRui Paulo 	/* Set phy db cmd variables */
336*d4886179SRui Paulo 	phy_db_cmd.type = le16toh(type);
337*d4886179SRui Paulo 	phy_db_cmd.length = le16toh(length);
338*d4886179SRui Paulo 
339*d4886179SRui Paulo 	/* Set hcmd variables */
340*d4886179SRui Paulo 	cmd.data[0] = &phy_db_cmd;
341*d4886179SRui Paulo 	cmd.len[0] = sizeof(struct iwm_phy_db_cmd);
342*d4886179SRui Paulo 	cmd.data[1] = data;
343*d4886179SRui Paulo 	cmd.len[1] = length;
344*d4886179SRui Paulo 	cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY;
345*d4886179SRui Paulo 
346*d4886179SRui Paulo 	return iwm_send_cmd(sc, &cmd);
347*d4886179SRui Paulo }
348*d4886179SRui Paulo 
349*d4886179SRui Paulo static int
350*d4886179SRui Paulo iwm_phy_db_send_all_channel_groups(struct iwm_softc *sc,
351*d4886179SRui Paulo 	enum iwm_phy_db_section_type type, uint8_t max_ch_groups)
352*d4886179SRui Paulo {
353*d4886179SRui Paulo 	uint16_t i;
354*d4886179SRui Paulo 	int err;
355*d4886179SRui Paulo 	struct iwm_phy_db_entry *entry;
356*d4886179SRui Paulo 
357*d4886179SRui Paulo 	/* Send all the channel-specific groups to operational fw */
358*d4886179SRui Paulo 	for (i = 0; i < max_ch_groups; i++) {
359*d4886179SRui Paulo 		entry = iwm_phy_db_get_section(sc, type, i);
360*d4886179SRui Paulo 		if (!entry)
361*d4886179SRui Paulo 			return EINVAL;
362*d4886179SRui Paulo 
363*d4886179SRui Paulo 		if (!entry->size)
364*d4886179SRui Paulo 			continue;
365*d4886179SRui Paulo 
366*d4886179SRui Paulo 		/* Send the requested PHY DB section */
367*d4886179SRui Paulo 		err = iwm_send_phy_db_cmd(sc, type, entry->size, entry->data);
368*d4886179SRui Paulo 		if (err) {
369*d4886179SRui Paulo 			IWM_DPRINTF(sc, IWM_DEBUG_CMD,
370*d4886179SRui Paulo 			    "%s: Can't SEND phy_db section %d (%d), "
371*d4886179SRui Paulo 			    "err %d\n", __func__, type, i, err);
372*d4886179SRui Paulo 			return err;
373*d4886179SRui Paulo 		}
374*d4886179SRui Paulo 
375*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
376*d4886179SRui Paulo 		    "Sent PHY_DB HCMD, type = %d num = %d\n", type, i);
377*d4886179SRui Paulo 	}
378*d4886179SRui Paulo 
379*d4886179SRui Paulo 	return 0;
380*d4886179SRui Paulo }
381*d4886179SRui Paulo 
382*d4886179SRui Paulo int
383*d4886179SRui Paulo iwm_send_phy_db_data(struct iwm_softc *sc)
384*d4886179SRui Paulo {
385*d4886179SRui Paulo 	uint8_t *data = NULL;
386*d4886179SRui Paulo 	uint16_t size = 0;
387*d4886179SRui Paulo 	int err;
388*d4886179SRui Paulo 
389*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
390*d4886179SRui Paulo 	    "%s: Sending phy db data and configuration to runtime image\n",
391*d4886179SRui Paulo 	    __func__);
392*d4886179SRui Paulo 
393*d4886179SRui Paulo 	/* Send PHY DB CFG section */
394*d4886179SRui Paulo 	err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CFG, &data, &size, 0);
395*d4886179SRui Paulo 	if (err) {
396*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
397*d4886179SRui Paulo 		    "%s: Cannot get Phy DB cfg section, %d\n",
398*d4886179SRui Paulo 		    __func__, err);
399*d4886179SRui Paulo 		return err;
400*d4886179SRui Paulo 	}
401*d4886179SRui Paulo 
402*d4886179SRui Paulo 	err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CFG, size, data);
403*d4886179SRui Paulo 	if (err) {
404*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
405*d4886179SRui Paulo 		    "%s: Cannot send HCMD of Phy DB cfg section, %d\n",
406*d4886179SRui Paulo 		    __func__, err);
407*d4886179SRui Paulo 		return err;
408*d4886179SRui Paulo 	}
409*d4886179SRui Paulo 
410*d4886179SRui Paulo 	err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CALIB_NCH,
411*d4886179SRui Paulo 	    &data, &size, 0);
412*d4886179SRui Paulo 	if (err) {
413*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
414*d4886179SRui Paulo 		    "%s: Cannot get Phy DB non specific channel section, "
415*d4886179SRui Paulo 		    "%d\n", __func__, err);
416*d4886179SRui Paulo 		return err;
417*d4886179SRui Paulo 	}
418*d4886179SRui Paulo 
419*d4886179SRui Paulo 	err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CALIB_NCH, size, data);
420*d4886179SRui Paulo 	if (err) {
421*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
422*d4886179SRui Paulo 		    "%s: Cannot send HCMD of Phy DB non specific channel "
423*d4886179SRui Paulo 		    "sect, %d\n", __func__, err);
424*d4886179SRui Paulo 		return err;
425*d4886179SRui Paulo 	}
426*d4886179SRui Paulo 
427*d4886179SRui Paulo 	/* Send all the TXP channel specific data */
428*d4886179SRui Paulo 	err = iwm_phy_db_send_all_channel_groups(sc,
429*d4886179SRui Paulo 	    IWM_PHY_DB_CALIB_CHG_PAPD, IWM_NUM_PAPD_CH_GROUPS);
430*d4886179SRui Paulo 	if (err) {
431*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
432*d4886179SRui Paulo 		    "%s: Cannot send channel specific PAPD groups, %d\n",
433*d4886179SRui Paulo 		    __func__, err);
434*d4886179SRui Paulo 		return err;
435*d4886179SRui Paulo 	}
436*d4886179SRui Paulo 
437*d4886179SRui Paulo 	/* Send all the TXP channel specific data */
438*d4886179SRui Paulo 	err = iwm_phy_db_send_all_channel_groups(sc,
439*d4886179SRui Paulo 	    IWM_PHY_DB_CALIB_CHG_TXP, IWM_NUM_TXP_CH_GROUPS);
440*d4886179SRui Paulo 	if (err) {
441*d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
442*d4886179SRui Paulo 		    "%s: Cannot send channel specific TX power groups, "
443*d4886179SRui Paulo 		    "%d\n", __func__, err);
444*d4886179SRui Paulo 		return err;
445*d4886179SRui Paulo 	}
446*d4886179SRui Paulo 
447*d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET,
448*d4886179SRui Paulo 	    "%s: Finished sending phy db non channel data\n",
449*d4886179SRui Paulo 	    __func__);
450*d4886179SRui Paulo 	return 0;
451*d4886179SRui Paulo }
452