xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_util.c (revision 303bb3ad4e201bd4a701bcb67da6e1b33695dd81)
124a8d46aSMatthew Dillon /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
224a8d46aSMatthew Dillon 
324a8d46aSMatthew Dillon /*
424a8d46aSMatthew Dillon  * Copyright (c) 2014 genua mbh <info@genua.de>
524a8d46aSMatthew Dillon  * Copyright (c) 2014 Fixup Software Ltd.
624a8d46aSMatthew Dillon  *
724a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
824a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
924a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
1024a8d46aSMatthew Dillon  *
1124a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1224a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1324a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1424a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1524a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1624a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1724a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1824a8d46aSMatthew Dillon  */
1924a8d46aSMatthew Dillon 
2024a8d46aSMatthew Dillon /*-
2124a8d46aSMatthew Dillon  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
2224a8d46aSMatthew Dillon  * which were used as the reference documentation for this implementation.
2324a8d46aSMatthew Dillon  *
2424a8d46aSMatthew Dillon  * Driver version we are currently based off of is
2524a8d46aSMatthew Dillon  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
2624a8d46aSMatthew Dillon  *
2724a8d46aSMatthew Dillon  ***********************************************************************
2824a8d46aSMatthew Dillon  *
2924a8d46aSMatthew Dillon  * This file is provided under a dual BSD/GPLv2 license.  When using or
3024a8d46aSMatthew Dillon  * redistributing this file, you may do so under either license.
3124a8d46aSMatthew Dillon  *
3224a8d46aSMatthew Dillon  * GPL LICENSE SUMMARY
3324a8d46aSMatthew Dillon  *
3424a8d46aSMatthew Dillon  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
3524a8d46aSMatthew Dillon  *
3624a8d46aSMatthew Dillon  * This program is free software; you can redistribute it and/or modify
3724a8d46aSMatthew Dillon  * it under the terms of version 2 of the GNU General Public License as
3824a8d46aSMatthew Dillon  * published by the Free Software Foundation.
3924a8d46aSMatthew Dillon  *
4024a8d46aSMatthew Dillon  * This program is distributed in the hope that it will be useful, but
4124a8d46aSMatthew Dillon  * WITHOUT ANY WARRANTY; without even the implied warranty of
4224a8d46aSMatthew Dillon  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4324a8d46aSMatthew Dillon  * General Public License for more details.
4424a8d46aSMatthew Dillon  *
4524a8d46aSMatthew Dillon  * You should have received a copy of the GNU General Public License
4624a8d46aSMatthew Dillon  * along with this program; if not, write to the Free Software
4724a8d46aSMatthew Dillon  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
4824a8d46aSMatthew Dillon  * USA
4924a8d46aSMatthew Dillon  *
5024a8d46aSMatthew Dillon  * The full GNU General Public License is included in this distribution
5124a8d46aSMatthew Dillon  * in the file called COPYING.
5224a8d46aSMatthew Dillon  *
5324a8d46aSMatthew Dillon  * Contact Information:
5424a8d46aSMatthew Dillon  *  Intel Linux Wireless <ilw@linux.intel.com>
5524a8d46aSMatthew Dillon  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
5624a8d46aSMatthew Dillon  *
5724a8d46aSMatthew Dillon  *
5824a8d46aSMatthew Dillon  * BSD LICENSE
5924a8d46aSMatthew Dillon  *
6024a8d46aSMatthew Dillon  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
6124a8d46aSMatthew Dillon  * All rights reserved.
6224a8d46aSMatthew Dillon  *
6324a8d46aSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
6424a8d46aSMatthew Dillon  * modification, are permitted provided that the following conditions
6524a8d46aSMatthew Dillon  * are met:
6624a8d46aSMatthew Dillon  *
6724a8d46aSMatthew Dillon  *  * Redistributions of source code must retain the above copyright
6824a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
6924a8d46aSMatthew Dillon  *  * Redistributions in binary form must reproduce the above copyright
7024a8d46aSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
7124a8d46aSMatthew Dillon  *    the documentation and/or other materials provided with the
7224a8d46aSMatthew Dillon  *    distribution.
7324a8d46aSMatthew Dillon  *  * Neither the name Intel Corporation nor the names of its
7424a8d46aSMatthew Dillon  *    contributors may be used to endorse or promote products derived
7524a8d46aSMatthew Dillon  *    from this software without specific prior written permission.
7624a8d46aSMatthew Dillon  *
7724a8d46aSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7824a8d46aSMatthew Dillon  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7924a8d46aSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8024a8d46aSMatthew Dillon  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8124a8d46aSMatthew Dillon  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8224a8d46aSMatthew Dillon  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
8324a8d46aSMatthew Dillon  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
8424a8d46aSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
8524a8d46aSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
8624a8d46aSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
8724a8d46aSMatthew Dillon  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
8824a8d46aSMatthew Dillon  */
8924a8d46aSMatthew Dillon 
9024a8d46aSMatthew Dillon /*-
9124a8d46aSMatthew Dillon  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
9224a8d46aSMatthew Dillon  *
9324a8d46aSMatthew Dillon  * Permission to use, copy, modify, and distribute this software for any
9424a8d46aSMatthew Dillon  * purpose with or without fee is hereby granted, provided that the above
9524a8d46aSMatthew Dillon  * copyright notice and this permission notice appear in all copies.
9624a8d46aSMatthew Dillon  *
9724a8d46aSMatthew Dillon  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9824a8d46aSMatthew Dillon  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9924a8d46aSMatthew Dillon  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10024a8d46aSMatthew Dillon  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
10124a8d46aSMatthew Dillon  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
10224a8d46aSMatthew Dillon  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
10324a8d46aSMatthew Dillon  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
10424a8d46aSMatthew Dillon  */
10524a8d46aSMatthew Dillon #include <sys/cdefs.h>
10624a8d46aSMatthew Dillon __FBSDID("$FreeBSD$");
10724a8d46aSMatthew Dillon 
10824a8d46aSMatthew Dillon #include <sys/param.h>
10924a8d46aSMatthew Dillon #include <sys/bus.h>
11024a8d46aSMatthew Dillon #include <sys/endian.h>
11124a8d46aSMatthew Dillon #include <sys/firmware.h>
11224a8d46aSMatthew Dillon #include <sys/kernel.h>
11324a8d46aSMatthew Dillon #include <sys/malloc.h>
11424a8d46aSMatthew Dillon #include <sys/mbuf.h>
11524a8d46aSMatthew Dillon #include <sys/mutex.h>
11624a8d46aSMatthew Dillon #include <sys/module.h>
11724a8d46aSMatthew Dillon #include <sys/proc.h>
11824a8d46aSMatthew Dillon #include <sys/rman.h>
11924a8d46aSMatthew Dillon #include <sys/socket.h>
12024a8d46aSMatthew Dillon #include <sys/sockio.h>
12124a8d46aSMatthew Dillon #include <sys/sysctl.h>
12224a8d46aSMatthew Dillon #include <sys/linker.h>
12324a8d46aSMatthew Dillon 
12424a8d46aSMatthew Dillon #include <machine/endian.h>
12524a8d46aSMatthew Dillon 
12645bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
12745bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
12824a8d46aSMatthew Dillon 
12924a8d46aSMatthew Dillon #include <net/bpf.h>
13024a8d46aSMatthew Dillon 
13124a8d46aSMatthew Dillon #include <net/if.h>
13224a8d46aSMatthew Dillon #include <net/if_var.h>
13324a8d46aSMatthew Dillon #include <net/if_arp.h>
13424a8d46aSMatthew Dillon #include <net/if_dl.h>
13524a8d46aSMatthew Dillon #include <net/if_media.h>
13624a8d46aSMatthew Dillon #include <net/if_types.h>
13724a8d46aSMatthew Dillon 
13824a8d46aSMatthew Dillon #include <netinet/in.h>
13924a8d46aSMatthew Dillon #include <netinet/in_systm.h>
14024a8d46aSMatthew Dillon #include <netinet/if_ether.h>
14124a8d46aSMatthew Dillon #include <netinet/ip.h>
14224a8d46aSMatthew Dillon 
14345bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
14445bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
14545bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
14645bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
14724a8d46aSMatthew Dillon 
14845bc40b1SMatthew Dillon #include "if_iwmreg.h"
14945bc40b1SMatthew Dillon #include "if_iwmvar.h"
15045bc40b1SMatthew Dillon #include "if_iwm_debug.h"
15145bc40b1SMatthew Dillon #include "if_iwm_binding.h"
15245bc40b1SMatthew Dillon #include "if_iwm_util.h"
15345bc40b1SMatthew Dillon #include "if_iwm_pcie_trans.h"
15445bc40b1SMatthew Dillon 
15524a8d46aSMatthew Dillon /*
15624a8d46aSMatthew Dillon  * Send a command to the firmware.  We try to implement the Linux
15724a8d46aSMatthew Dillon  * driver interface for the routine.
15824a8d46aSMatthew Dillon  * mostly from if_iwn (iwn_cmd()).
15924a8d46aSMatthew Dillon  *
16024a8d46aSMatthew Dillon  * For now, we always copy the first part and map the second one (if it exists).
16124a8d46aSMatthew Dillon  */
16224a8d46aSMatthew Dillon int
16324a8d46aSMatthew Dillon iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
16424a8d46aSMatthew Dillon {
16524a8d46aSMatthew Dillon 	struct iwm_tx_ring *ring = &sc->txq[IWM_MVM_CMD_QUEUE];
16624a8d46aSMatthew Dillon 	struct iwm_tfd *desc;
167edfc8a07SImre Vadász 	struct iwm_tx_data *txdata = NULL;
168edfc8a07SImre Vadász 	struct iwm_device_cmd *cmd;
16965b2e1baSImre Vadász 	struct mbuf *m;
17065b2e1baSImre Vadász 	bus_dma_segment_t seg;
17124a8d46aSMatthew Dillon 	bus_addr_t paddr;
17224a8d46aSMatthew Dillon 	uint32_t addr_lo;
17324a8d46aSMatthew Dillon 	int error = 0, i, paylen, off;
17424a8d46aSMatthew Dillon 	int code;
17524a8d46aSMatthew Dillon 	int async, wantresp;
176edfc8a07SImre Vadász 	int group_id;
17765b2e1baSImre Vadász 	int nsegs;
178edfc8a07SImre Vadász 	size_t hdrlen, datasz;
179edfc8a07SImre Vadász 	uint8_t *data;
18024a8d46aSMatthew Dillon 
18124a8d46aSMatthew Dillon 	code = hcmd->id;
18224a8d46aSMatthew Dillon 	async = hcmd->flags & IWM_CMD_ASYNC;
18324a8d46aSMatthew Dillon 	wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
18445bc40b1SMatthew Dillon 	data = NULL;
18524a8d46aSMatthew Dillon 
18624a8d46aSMatthew Dillon 	for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
18724a8d46aSMatthew Dillon 		paylen += hcmd->len[i];
18824a8d46aSMatthew Dillon 	}
18924a8d46aSMatthew Dillon 
19024a8d46aSMatthew Dillon 	/* if the command wants an answer, busy sc_cmd_resp */
19124a8d46aSMatthew Dillon 	if (wantresp) {
19224a8d46aSMatthew Dillon 		KASSERT(!async, ("invalid async parameter"));
193b0b29253SMatthew Dillon 		while (sc->sc_wantresp != -1) {
194b0b29253SMatthew Dillon #if defined(__DragonFly__)
195*303bb3adSImre Vadász 			lksleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0);
196b0b29253SMatthew Dillon #else
197b0b29253SMatthew Dillon 			msleep(&sc->sc_wantresp, &sc->sc_mtx, 0, "iwmcmdsl", 0);
198b0b29253SMatthew Dillon #endif
199b0b29253SMatthew Dillon 		}
20024a8d46aSMatthew Dillon 		sc->sc_wantresp = ring->qid << 16 | ring->cur;
20124a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
20224a8d46aSMatthew Dillon 		    "wantresp is %x\n", sc->sc_wantresp);
20324a8d46aSMatthew Dillon 	}
20424a8d46aSMatthew Dillon 
20524a8d46aSMatthew Dillon 	/*
20624a8d46aSMatthew Dillon 	 * Is the hardware still available?  (after e.g. above wait).
20724a8d46aSMatthew Dillon 	 */
20824a8d46aSMatthew Dillon 	if (sc->sc_flags & IWM_FLAG_STOPPED) {
20924a8d46aSMatthew Dillon 		error = ENXIO;
21024a8d46aSMatthew Dillon 		goto out;
21124a8d46aSMatthew Dillon 	}
21224a8d46aSMatthew Dillon 
21324a8d46aSMatthew Dillon 	desc = &ring->desc[ring->cur];
214edfc8a07SImre Vadász 	txdata = &ring->data[ring->cur];
21524a8d46aSMatthew Dillon 
216edfc8a07SImre Vadász 	group_id = iwm_cmd_groupid(code);
217edfc8a07SImre Vadász 	if (group_id != 0) {
218edfc8a07SImre Vadász 		hdrlen = sizeof(cmd->hdr_wide);
219edfc8a07SImre Vadász 		datasz = sizeof(cmd->data_wide);
220edfc8a07SImre Vadász 	} else {
221edfc8a07SImre Vadász 		hdrlen = sizeof(cmd->hdr);
222edfc8a07SImre Vadász 		datasz = sizeof(cmd->data);
223edfc8a07SImre Vadász 	}
224edfc8a07SImre Vadász 
225edfc8a07SImre Vadász 	if (paylen > datasz) {
22624a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
22724a8d46aSMatthew Dillon 		    "large command paylen=%u len0=%u\n",
22824a8d46aSMatthew Dillon 			paylen, hcmd->len[0]);
22924a8d46aSMatthew Dillon 		/* Command is too large */
230edfc8a07SImre Vadász 		size_t totlen = hdrlen + paylen;
231b70c1eaaSImre Vadász 		if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) {
232b70c1eaaSImre Vadász 			device_printf(sc->sc_dev,
233b70c1eaaSImre Vadász 			    "firmware command too long (%zd bytes)\n",
234edfc8a07SImre Vadász 			    totlen);
23524a8d46aSMatthew Dillon 			error = EINVAL;
23624a8d46aSMatthew Dillon 			goto out;
23724a8d46aSMatthew Dillon 		}
23865b2e1baSImre Vadász 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, IWM_RBUF_SIZE);
23965b2e1baSImre Vadász 		if (m == NULL) {
24065b2e1baSImre Vadász 			error = ENOBUFS;
24124a8d46aSMatthew Dillon 			goto out;
24265b2e1baSImre Vadász 		}
24365b2e1baSImre Vadász 
24465b2e1baSImre Vadász 		m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
24565b2e1baSImre Vadász #if defined(__DragonFly__)
24665b2e1baSImre Vadász 		error = bus_dmamap_load_mbuf_segment(ring->data_dmat,
24765b2e1baSImre Vadász 		    txdata->map, m, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
24865b2e1baSImre Vadász #else
24965b2e1baSImre Vadász 		error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
25065b2e1baSImre Vadász 		    txdata->map, m, &seg, &nsegs, BUS_DMA_NOWAIT);
25165b2e1baSImre Vadász #endif
25265b2e1baSImre Vadász 		if (error != 0) {
25365b2e1baSImre Vadász 			device_printf(sc->sc_dev,
25465b2e1baSImre Vadász 			    "%s: can't map mbuf, error %d\n", __func__, error);
25565b2e1baSImre Vadász 			m_freem(m);
25624a8d46aSMatthew Dillon 			goto out;
25765b2e1baSImre Vadász 		}
25865b2e1baSImre Vadász 		txdata->m = m; /* mbuf will be freed in iwm_cmd_done() */
25965b2e1baSImre Vadász 		cmd = mtod(m, struct iwm_device_cmd *);
26065b2e1baSImre Vadász 		paddr = seg.ds_addr;
26124a8d46aSMatthew Dillon 	} else {
26224a8d46aSMatthew Dillon 		cmd = &ring->cmd[ring->cur];
263edfc8a07SImre Vadász 		paddr = txdata->cmd_paddr;
26424a8d46aSMatthew Dillon 	}
26524a8d46aSMatthew Dillon 
266edfc8a07SImre Vadász 	if (group_id != 0) {
267edfc8a07SImre Vadász 		cmd->hdr_wide.opcode = iwm_cmd_opcode(code);
268edfc8a07SImre Vadász 		cmd->hdr_wide.group_id = group_id;
269edfc8a07SImre Vadász 		cmd->hdr_wide.qid = ring->qid;
270edfc8a07SImre Vadász 		cmd->hdr_wide.idx = ring->cur;
271edfc8a07SImre Vadász 		cmd->hdr_wide.length = htole16(paylen);
272edfc8a07SImre Vadász 		cmd->hdr_wide.version = iwm_cmd_version(code);
273edfc8a07SImre Vadász 		data = cmd->data_wide;
274edfc8a07SImre Vadász 	} else {
27545078908SImre Vadász 		cmd->hdr.code = iwm_cmd_opcode(code);
27624a8d46aSMatthew Dillon 		cmd->hdr.flags = 0;
27724a8d46aSMatthew Dillon 		cmd->hdr.qid = ring->qid;
27824a8d46aSMatthew Dillon 		cmd->hdr.idx = ring->cur;
279edfc8a07SImre Vadász 		data = cmd->data;
280edfc8a07SImre Vadász 	}
28124a8d46aSMatthew Dillon 
28224a8d46aSMatthew Dillon 	for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
28324a8d46aSMatthew Dillon 		if (hcmd->len[i] == 0)
28424a8d46aSMatthew Dillon 			continue;
285edfc8a07SImre Vadász 		memcpy(data + off, hcmd->data[i], hcmd->len[i]);
28624a8d46aSMatthew Dillon 		off += hcmd->len[i];
28724a8d46aSMatthew Dillon 	}
28824a8d46aSMatthew Dillon 	KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
28924a8d46aSMatthew Dillon 
29024a8d46aSMatthew Dillon 	/* lo field is not aligned */
29124a8d46aSMatthew Dillon 	addr_lo = htole32((uint32_t)paddr);
29224a8d46aSMatthew Dillon 	memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
29324a8d46aSMatthew Dillon 	desc->tbs[0].hi_n_len  = htole16(iwm_get_dma_hi_addr(paddr)
294edfc8a07SImre Vadász 	    | ((hdrlen + paylen) << 4));
29524a8d46aSMatthew Dillon 	desc->num_tbs = 1;
29624a8d46aSMatthew Dillon 
29724a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
298edfc8a07SImre Vadász 	    "iwm_send_cmd 0x%x size=%lu %s\n",
29924a8d46aSMatthew Dillon 	    code,
300edfc8a07SImre Vadász 	    (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen),
30124a8d46aSMatthew Dillon 	    async ? " (async)" : "");
30224a8d46aSMatthew Dillon 
303edfc8a07SImre Vadász 	if (paylen > datasz) {
304edfc8a07SImre Vadász 		bus_dmamap_sync(ring->data_dmat, txdata->map,
30524a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
30624a8d46aSMatthew Dillon 	} else {
30724a8d46aSMatthew Dillon 		bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
30824a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
30924a8d46aSMatthew Dillon 	}
31024a8d46aSMatthew Dillon 	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
31124a8d46aSMatthew Dillon 	    BUS_DMASYNC_PREWRITE);
31224a8d46aSMatthew Dillon 
31324a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
31424a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
31524a8d46aSMatthew Dillon 	if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
31624a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
31724a8d46aSMatthew Dillon 	    (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
31824a8d46aSMatthew Dillon 	     IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000)) {
31924a8d46aSMatthew Dillon 		device_printf(sc->sc_dev,
32024a8d46aSMatthew Dillon 		    "%s: acquiring device failed\n", __func__);
32124a8d46aSMatthew Dillon 		error = EBUSY;
32224a8d46aSMatthew Dillon 		goto out;
32324a8d46aSMatthew Dillon 	}
32424a8d46aSMatthew Dillon 
32524a8d46aSMatthew Dillon #if 0
32624a8d46aSMatthew Dillon 	iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
32724a8d46aSMatthew Dillon #endif
32824a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
32924a8d46aSMatthew Dillon 	    "sending command 0x%x qid %d, idx %d\n",
33024a8d46aSMatthew Dillon 	    code, ring->qid, ring->cur);
33124a8d46aSMatthew Dillon 
33224a8d46aSMatthew Dillon 	/* Kick command ring. */
33324a8d46aSMatthew Dillon 	ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
33424a8d46aSMatthew Dillon 	IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
33524a8d46aSMatthew Dillon 
33624a8d46aSMatthew Dillon 	if (!async) {
33724a8d46aSMatthew Dillon 		/* m..m-mmyy-mmyyyy-mym-ym m-my generation */
33824a8d46aSMatthew Dillon 		int generation = sc->sc_generation;
339b0b29253SMatthew Dillon #if defined(__DragonFly__)
340*303bb3adSImre Vadász 		error = lksleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz);
341b0b29253SMatthew Dillon #else
342b0b29253SMatthew Dillon 		error = msleep(desc, &sc->sc_mtx, PCATCH, "iwmcmd", hz);
343b0b29253SMatthew Dillon #endif
34424a8d46aSMatthew Dillon 		if (error == 0) {
34524a8d46aSMatthew Dillon 			/* if hardware is no longer up, return error */
34624a8d46aSMatthew Dillon 			if (generation != sc->sc_generation) {
34724a8d46aSMatthew Dillon 				error = ENXIO;
34824a8d46aSMatthew Dillon 			} else {
34924a8d46aSMatthew Dillon 				hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
35024a8d46aSMatthew Dillon 			}
35124a8d46aSMatthew Dillon 		}
35224a8d46aSMatthew Dillon 	}
35324a8d46aSMatthew Dillon  out:
35424a8d46aSMatthew Dillon 	if (wantresp && error != 0) {
35524a8d46aSMatthew Dillon 		iwm_free_resp(sc, hcmd);
35624a8d46aSMatthew Dillon 	}
35724a8d46aSMatthew Dillon 
35824a8d46aSMatthew Dillon 	return error;
35924a8d46aSMatthew Dillon }
36024a8d46aSMatthew Dillon 
36124a8d46aSMatthew Dillon int
36224a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu(struct iwm_softc *sc, uint8_t id,
36324a8d46aSMatthew Dillon 	uint32_t flags, uint16_t len, const void *data)
36424a8d46aSMatthew Dillon {
36524a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
36624a8d46aSMatthew Dillon 		.id = id,
36724a8d46aSMatthew Dillon 		.len = { len, },
36824a8d46aSMatthew Dillon 		.data = { data, },
36924a8d46aSMatthew Dillon 		.flags = flags,
37024a8d46aSMatthew Dillon 	};
37124a8d46aSMatthew Dillon 
37224a8d46aSMatthew Dillon 	return iwm_send_cmd(sc, &cmd);
37324a8d46aSMatthew Dillon }
37424a8d46aSMatthew Dillon 
37524a8d46aSMatthew Dillon int
37624a8d46aSMatthew Dillon iwm_mvm_send_cmd_status(struct iwm_softc *sc,
37724a8d46aSMatthew Dillon 	struct iwm_host_cmd *cmd, uint32_t *status)
37824a8d46aSMatthew Dillon {
37924a8d46aSMatthew Dillon 	struct iwm_rx_packet *pkt;
38024a8d46aSMatthew Dillon 	struct iwm_cmd_response *resp;
38124a8d46aSMatthew Dillon 	int error, resp_len;
38224a8d46aSMatthew Dillon 
38324a8d46aSMatthew Dillon 	KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0,
38424a8d46aSMatthew Dillon 	    ("invalid command"));
38524a8d46aSMatthew Dillon 	cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB;
38624a8d46aSMatthew Dillon 
38724a8d46aSMatthew Dillon 	if ((error = iwm_send_cmd(sc, cmd)) != 0)
38824a8d46aSMatthew Dillon 		return error;
38924a8d46aSMatthew Dillon 	pkt = cmd->resp_pkt;
39024a8d46aSMatthew Dillon 
39124a8d46aSMatthew Dillon 	/* Can happen if RFKILL is asserted */
39224a8d46aSMatthew Dillon 	if (!pkt) {
39324a8d46aSMatthew Dillon 		error = 0;
39424a8d46aSMatthew Dillon 		goto out_free_resp;
39524a8d46aSMatthew Dillon 	}
39624a8d46aSMatthew Dillon 
39724a8d46aSMatthew Dillon 	if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) {
39824a8d46aSMatthew Dillon 		error = EIO;
39924a8d46aSMatthew Dillon 		goto out_free_resp;
40024a8d46aSMatthew Dillon 	}
40124a8d46aSMatthew Dillon 
40224a8d46aSMatthew Dillon 	resp_len = iwm_rx_packet_payload_len(pkt);
40324a8d46aSMatthew Dillon 	if (resp_len != sizeof(*resp)) {
40424a8d46aSMatthew Dillon 		error = EIO;
40524a8d46aSMatthew Dillon 		goto out_free_resp;
40624a8d46aSMatthew Dillon 	}
40724a8d46aSMatthew Dillon 
40824a8d46aSMatthew Dillon 	resp = (void *)pkt->data;
40924a8d46aSMatthew Dillon 	*status = le32toh(resp->status);
41024a8d46aSMatthew Dillon  out_free_resp:
41124a8d46aSMatthew Dillon 	iwm_free_resp(sc, cmd);
41224a8d46aSMatthew Dillon 	return error;
41324a8d46aSMatthew Dillon }
41424a8d46aSMatthew Dillon 
41524a8d46aSMatthew Dillon int
41624a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu_status(struct iwm_softc *sc, uint8_t id,
41724a8d46aSMatthew Dillon 	uint16_t len, const void *data, uint32_t *status)
41824a8d46aSMatthew Dillon {
41924a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
42024a8d46aSMatthew Dillon 		.id = id,
42124a8d46aSMatthew Dillon 		.len = { len, },
42224a8d46aSMatthew Dillon 		.data = { data, },
42324a8d46aSMatthew Dillon 	};
42424a8d46aSMatthew Dillon 
42524a8d46aSMatthew Dillon 	return iwm_mvm_send_cmd_status(sc, &cmd, status);
42624a8d46aSMatthew Dillon }
42724a8d46aSMatthew Dillon 
42824a8d46aSMatthew Dillon void
42924a8d46aSMatthew Dillon iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
43024a8d46aSMatthew Dillon {
43124a8d46aSMatthew Dillon 	KASSERT(sc->sc_wantresp != -1, ("already freed"));
43224a8d46aSMatthew Dillon 	KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC))
43324a8d46aSMatthew Dillon 	    == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags"));
43424a8d46aSMatthew Dillon 	sc->sc_wantresp = -1;
43524a8d46aSMatthew Dillon 	wakeup(&sc->sc_wantresp);
43624a8d46aSMatthew Dillon }
437edfc8a07SImre Vadász 
438edfc8a07SImre Vadász uint8_t
439edfc8a07SImre Vadász iwm_fw_valid_tx_ant(struct iwm_softc *sc)
440edfc8a07SImre Vadász {
441edfc8a07SImre Vadász 	uint8_t tx_ant;
442edfc8a07SImre Vadász 
443edfc8a07SImre Vadász 	tx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_TX_CHAIN)
444edfc8a07SImre Vadász 	    >> IWM_FW_PHY_CFG_TX_CHAIN_POS);
445edfc8a07SImre Vadász 
446edfc8a07SImre Vadász 	if (sc->sc_nvm.valid_tx_ant)
447edfc8a07SImre Vadász 		tx_ant &= sc->sc_nvm.valid_tx_ant;
448edfc8a07SImre Vadász 
449edfc8a07SImre Vadász 	return tx_ant;
450edfc8a07SImre Vadász }
451edfc8a07SImre Vadász 
452edfc8a07SImre Vadász uint8_t
453edfc8a07SImre Vadász iwm_fw_valid_rx_ant(struct iwm_softc *sc)
454edfc8a07SImre Vadász {
455edfc8a07SImre Vadász 	uint8_t rx_ant;
456edfc8a07SImre Vadász 
457edfc8a07SImre Vadász 	rx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_RX_CHAIN)
458edfc8a07SImre Vadász 	    >> IWM_FW_PHY_CFG_RX_CHAIN_POS);
459edfc8a07SImre Vadász 
460edfc8a07SImre Vadász 	if (sc->sc_nvm.valid_rx_ant)
461edfc8a07SImre Vadász 		rx_ant &= sc->sc_nvm.valid_rx_ant;
462edfc8a07SImre Vadász 
463edfc8a07SImre Vadász 	return rx_ant;
464edfc8a07SImre Vadász }
465