xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_util.c (revision 45078908a305bce0e25cb0a5357c74b6ac2b521c)
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 
155b0b29253SMatthew Dillon #if defined(__DragonFly__)
15645bc40b1SMatthew Dillon int iwmsleep(void *chan, struct lock *lk, int flags, const char *wmesg, int to);
157b0b29253SMatthew Dillon #endif
15824a8d46aSMatthew Dillon 
15924a8d46aSMatthew Dillon /*
16024a8d46aSMatthew Dillon  * Send a command to the firmware.  We try to implement the Linux
16124a8d46aSMatthew Dillon  * driver interface for the routine.
16224a8d46aSMatthew Dillon  * mostly from if_iwn (iwn_cmd()).
16324a8d46aSMatthew Dillon  *
16424a8d46aSMatthew Dillon  * For now, we always copy the first part and map the second one (if it exists).
16524a8d46aSMatthew Dillon  */
16624a8d46aSMatthew Dillon int
16724a8d46aSMatthew Dillon iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
16824a8d46aSMatthew Dillon {
16924a8d46aSMatthew Dillon 	struct iwm_tx_ring *ring = &sc->txq[IWM_MVM_CMD_QUEUE];
17024a8d46aSMatthew Dillon 	struct iwm_tfd *desc;
171edfc8a07SImre Vadász 	struct iwm_tx_data *txdata = NULL;
172edfc8a07SImre Vadász 	struct iwm_device_cmd *cmd;
17365b2e1baSImre Vadász 	struct mbuf *m;
17465b2e1baSImre Vadász 	bus_dma_segment_t seg;
17524a8d46aSMatthew Dillon 	bus_addr_t paddr;
17624a8d46aSMatthew Dillon 	uint32_t addr_lo;
17724a8d46aSMatthew Dillon 	int error = 0, i, paylen, off;
17824a8d46aSMatthew Dillon 	int code;
17924a8d46aSMatthew Dillon 	int async, wantresp;
180edfc8a07SImre Vadász 	int group_id;
18165b2e1baSImre Vadász 	int nsegs;
182edfc8a07SImre Vadász 	size_t hdrlen, datasz;
183edfc8a07SImre Vadász 	uint8_t *data;
18424a8d46aSMatthew Dillon 
18524a8d46aSMatthew Dillon 	code = hcmd->id;
18624a8d46aSMatthew Dillon 	async = hcmd->flags & IWM_CMD_ASYNC;
18724a8d46aSMatthew Dillon 	wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
18845bc40b1SMatthew Dillon 	data = NULL;
18924a8d46aSMatthew Dillon 
19024a8d46aSMatthew Dillon 	for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
19124a8d46aSMatthew Dillon 		paylen += hcmd->len[i];
19224a8d46aSMatthew Dillon 	}
19324a8d46aSMatthew Dillon 
19424a8d46aSMatthew Dillon 	/* if the command wants an answer, busy sc_cmd_resp */
19524a8d46aSMatthew Dillon 	if (wantresp) {
19624a8d46aSMatthew Dillon 		KASSERT(!async, ("invalid async parameter"));
197b0b29253SMatthew Dillon 		while (sc->sc_wantresp != -1) {
198b0b29253SMatthew Dillon #if defined(__DragonFly__)
19945bc40b1SMatthew Dillon 			iwmsleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0);
200b0b29253SMatthew Dillon #else
201b0b29253SMatthew Dillon 			msleep(&sc->sc_wantresp, &sc->sc_mtx, 0, "iwmcmdsl", 0);
202b0b29253SMatthew Dillon #endif
203b0b29253SMatthew Dillon 		}
20424a8d46aSMatthew Dillon 		sc->sc_wantresp = ring->qid << 16 | ring->cur;
20524a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
20624a8d46aSMatthew Dillon 		    "wantresp is %x\n", sc->sc_wantresp);
20724a8d46aSMatthew Dillon 	}
20824a8d46aSMatthew Dillon 
20924a8d46aSMatthew Dillon 	/*
21024a8d46aSMatthew Dillon 	 * Is the hardware still available?  (after e.g. above wait).
21124a8d46aSMatthew Dillon 	 */
21224a8d46aSMatthew Dillon 	if (sc->sc_flags & IWM_FLAG_STOPPED) {
21324a8d46aSMatthew Dillon 		error = ENXIO;
21424a8d46aSMatthew Dillon 		goto out;
21524a8d46aSMatthew Dillon 	}
21624a8d46aSMatthew Dillon 
21724a8d46aSMatthew Dillon 	desc = &ring->desc[ring->cur];
218edfc8a07SImre Vadász 	txdata = &ring->data[ring->cur];
21924a8d46aSMatthew Dillon 
220edfc8a07SImre Vadász 	group_id = iwm_cmd_groupid(code);
221edfc8a07SImre Vadász 	if (group_id != 0) {
222edfc8a07SImre Vadász 		hdrlen = sizeof(cmd->hdr_wide);
223edfc8a07SImre Vadász 		datasz = sizeof(cmd->data_wide);
224edfc8a07SImre Vadász 	} else {
225edfc8a07SImre Vadász 		hdrlen = sizeof(cmd->hdr);
226edfc8a07SImre Vadász 		datasz = sizeof(cmd->data);
227edfc8a07SImre Vadász 	}
228edfc8a07SImre Vadász 
229edfc8a07SImre Vadász 	if (paylen > datasz) {
23024a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
23124a8d46aSMatthew Dillon 		    "large command paylen=%u len0=%u\n",
23224a8d46aSMatthew Dillon 			paylen, hcmd->len[0]);
23324a8d46aSMatthew Dillon 		/* Command is too large */
234edfc8a07SImre Vadász 		size_t totlen = hdrlen + paylen;
235b70c1eaaSImre Vadász 		if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) {
236b70c1eaaSImre Vadász 			device_printf(sc->sc_dev,
237b70c1eaaSImre Vadász 			    "firmware command too long (%zd bytes)\n",
238edfc8a07SImre Vadász 			    totlen);
23924a8d46aSMatthew Dillon 			error = EINVAL;
24024a8d46aSMatthew Dillon 			goto out;
24124a8d46aSMatthew Dillon 		}
24265b2e1baSImre Vadász 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, IWM_RBUF_SIZE);
24365b2e1baSImre Vadász 		if (m == NULL) {
24465b2e1baSImre Vadász 			error = ENOBUFS;
24524a8d46aSMatthew Dillon 			goto out;
24665b2e1baSImre Vadász 		}
24765b2e1baSImre Vadász 
24865b2e1baSImre Vadász 		m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
24965b2e1baSImre Vadász #if defined(__DragonFly__)
25065b2e1baSImre Vadász 		error = bus_dmamap_load_mbuf_segment(ring->data_dmat,
25165b2e1baSImre Vadász 		    txdata->map, m, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
25265b2e1baSImre Vadász #else
25365b2e1baSImre Vadász 		error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
25465b2e1baSImre Vadász 		    txdata->map, m, &seg, &nsegs, BUS_DMA_NOWAIT);
25565b2e1baSImre Vadász #endif
25665b2e1baSImre Vadász 		if (error != 0) {
25765b2e1baSImre Vadász 			device_printf(sc->sc_dev,
25865b2e1baSImre Vadász 			    "%s: can't map mbuf, error %d\n", __func__, error);
25965b2e1baSImre Vadász 			m_freem(m);
26024a8d46aSMatthew Dillon 			goto out;
26165b2e1baSImre Vadász 		}
26265b2e1baSImre Vadász 		txdata->m = m; /* mbuf will be freed in iwm_cmd_done() */
26365b2e1baSImre Vadász 		cmd = mtod(m, struct iwm_device_cmd *);
26465b2e1baSImre Vadász 		paddr = seg.ds_addr;
26524a8d46aSMatthew Dillon 	} else {
26624a8d46aSMatthew Dillon 		cmd = &ring->cmd[ring->cur];
267edfc8a07SImre Vadász 		paddr = txdata->cmd_paddr;
26824a8d46aSMatthew Dillon 	}
26924a8d46aSMatthew Dillon 
270edfc8a07SImre Vadász 	if (group_id != 0) {
271edfc8a07SImre Vadász 		cmd->hdr_wide.opcode = iwm_cmd_opcode(code);
272edfc8a07SImre Vadász 		cmd->hdr_wide.group_id = group_id;
273edfc8a07SImre Vadász 		cmd->hdr_wide.qid = ring->qid;
274edfc8a07SImre Vadász 		cmd->hdr_wide.idx = ring->cur;
275edfc8a07SImre Vadász 		cmd->hdr_wide.length = htole16(paylen);
276edfc8a07SImre Vadász 		cmd->hdr_wide.version = iwm_cmd_version(code);
277edfc8a07SImre Vadász 		data = cmd->data_wide;
278edfc8a07SImre Vadász 	} else {
279*45078908SImre Vadász 		cmd->hdr.code = iwm_cmd_opcode(code);
28024a8d46aSMatthew Dillon 		cmd->hdr.flags = 0;
28124a8d46aSMatthew Dillon 		cmd->hdr.qid = ring->qid;
28224a8d46aSMatthew Dillon 		cmd->hdr.idx = ring->cur;
283edfc8a07SImre Vadász 		data = cmd->data;
284edfc8a07SImre Vadász 	}
28524a8d46aSMatthew Dillon 
28624a8d46aSMatthew Dillon 	for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
28724a8d46aSMatthew Dillon 		if (hcmd->len[i] == 0)
28824a8d46aSMatthew Dillon 			continue;
289edfc8a07SImre Vadász 		memcpy(data + off, hcmd->data[i], hcmd->len[i]);
29024a8d46aSMatthew Dillon 		off += hcmd->len[i];
29124a8d46aSMatthew Dillon 	}
29224a8d46aSMatthew Dillon 	KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
29324a8d46aSMatthew Dillon 
29424a8d46aSMatthew Dillon 	/* lo field is not aligned */
29524a8d46aSMatthew Dillon 	addr_lo = htole32((uint32_t)paddr);
29624a8d46aSMatthew Dillon 	memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
29724a8d46aSMatthew Dillon 	desc->tbs[0].hi_n_len  = htole16(iwm_get_dma_hi_addr(paddr)
298edfc8a07SImre Vadász 	    | ((hdrlen + paylen) << 4));
29924a8d46aSMatthew Dillon 	desc->num_tbs = 1;
30024a8d46aSMatthew Dillon 
30124a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
302edfc8a07SImre Vadász 	    "iwm_send_cmd 0x%x size=%lu %s\n",
30324a8d46aSMatthew Dillon 	    code,
304edfc8a07SImre Vadász 	    (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen),
30524a8d46aSMatthew Dillon 	    async ? " (async)" : "");
30624a8d46aSMatthew Dillon 
307edfc8a07SImre Vadász 	if (paylen > datasz) {
308edfc8a07SImre Vadász 		bus_dmamap_sync(ring->data_dmat, txdata->map,
30924a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
31024a8d46aSMatthew Dillon 	} else {
31124a8d46aSMatthew Dillon 		bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
31224a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
31324a8d46aSMatthew Dillon 	}
31424a8d46aSMatthew Dillon 	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
31524a8d46aSMatthew Dillon 	    BUS_DMASYNC_PREWRITE);
31624a8d46aSMatthew Dillon 
31724a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
31824a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
31924a8d46aSMatthew Dillon 	if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
32024a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
32124a8d46aSMatthew Dillon 	    (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
32224a8d46aSMatthew Dillon 	     IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000)) {
32324a8d46aSMatthew Dillon 		device_printf(sc->sc_dev,
32424a8d46aSMatthew Dillon 		    "%s: acquiring device failed\n", __func__);
32524a8d46aSMatthew Dillon 		error = EBUSY;
32624a8d46aSMatthew Dillon 		goto out;
32724a8d46aSMatthew Dillon 	}
32824a8d46aSMatthew Dillon 
32924a8d46aSMatthew Dillon #if 0
33024a8d46aSMatthew Dillon 	iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
33124a8d46aSMatthew Dillon #endif
33224a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
33324a8d46aSMatthew Dillon 	    "sending command 0x%x qid %d, idx %d\n",
33424a8d46aSMatthew Dillon 	    code, ring->qid, ring->cur);
33524a8d46aSMatthew Dillon 
33624a8d46aSMatthew Dillon 	/* Kick command ring. */
33724a8d46aSMatthew Dillon 	ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
33824a8d46aSMatthew Dillon 	IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
33924a8d46aSMatthew Dillon 
34024a8d46aSMatthew Dillon 	if (!async) {
34124a8d46aSMatthew Dillon 		/* m..m-mmyy-mmyyyy-mym-ym m-my generation */
34224a8d46aSMatthew Dillon 		int generation = sc->sc_generation;
343b0b29253SMatthew Dillon #if defined(__DragonFly__)
34445bc40b1SMatthew Dillon 		error = iwmsleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz);
345b0b29253SMatthew Dillon #else
346b0b29253SMatthew Dillon 		error = msleep(desc, &sc->sc_mtx, PCATCH, "iwmcmd", hz);
347b0b29253SMatthew Dillon #endif
34824a8d46aSMatthew Dillon 		if (error == 0) {
34924a8d46aSMatthew Dillon 			/* if hardware is no longer up, return error */
35024a8d46aSMatthew Dillon 			if (generation != sc->sc_generation) {
35124a8d46aSMatthew Dillon 				error = ENXIO;
35224a8d46aSMatthew Dillon 			} else {
35324a8d46aSMatthew Dillon 				hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
35424a8d46aSMatthew Dillon 			}
35524a8d46aSMatthew Dillon 		}
35624a8d46aSMatthew Dillon 	}
35724a8d46aSMatthew Dillon  out:
35824a8d46aSMatthew Dillon 	if (wantresp && error != 0) {
35924a8d46aSMatthew Dillon 		iwm_free_resp(sc, hcmd);
36024a8d46aSMatthew Dillon 	}
36124a8d46aSMatthew Dillon 
36224a8d46aSMatthew Dillon 	return error;
36324a8d46aSMatthew Dillon }
36424a8d46aSMatthew Dillon 
36524a8d46aSMatthew Dillon int
36624a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu(struct iwm_softc *sc, uint8_t id,
36724a8d46aSMatthew Dillon 	uint32_t flags, uint16_t len, const void *data)
36824a8d46aSMatthew Dillon {
36924a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
37024a8d46aSMatthew Dillon 		.id = id,
37124a8d46aSMatthew Dillon 		.len = { len, },
37224a8d46aSMatthew Dillon 		.data = { data, },
37324a8d46aSMatthew Dillon 		.flags = flags,
37424a8d46aSMatthew Dillon 	};
37524a8d46aSMatthew Dillon 
37624a8d46aSMatthew Dillon 	return iwm_send_cmd(sc, &cmd);
37724a8d46aSMatthew Dillon }
37824a8d46aSMatthew Dillon 
37924a8d46aSMatthew Dillon int
38024a8d46aSMatthew Dillon iwm_mvm_send_cmd_status(struct iwm_softc *sc,
38124a8d46aSMatthew Dillon 	struct iwm_host_cmd *cmd, uint32_t *status)
38224a8d46aSMatthew Dillon {
38324a8d46aSMatthew Dillon 	struct iwm_rx_packet *pkt;
38424a8d46aSMatthew Dillon 	struct iwm_cmd_response *resp;
38524a8d46aSMatthew Dillon 	int error, resp_len;
38624a8d46aSMatthew Dillon 
38724a8d46aSMatthew Dillon 	KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0,
38824a8d46aSMatthew Dillon 	    ("invalid command"));
38924a8d46aSMatthew Dillon 	cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB;
39024a8d46aSMatthew Dillon 
39124a8d46aSMatthew Dillon 	if ((error = iwm_send_cmd(sc, cmd)) != 0)
39224a8d46aSMatthew Dillon 		return error;
39324a8d46aSMatthew Dillon 	pkt = cmd->resp_pkt;
39424a8d46aSMatthew Dillon 
39524a8d46aSMatthew Dillon 	/* Can happen if RFKILL is asserted */
39624a8d46aSMatthew Dillon 	if (!pkt) {
39724a8d46aSMatthew Dillon 		error = 0;
39824a8d46aSMatthew Dillon 		goto out_free_resp;
39924a8d46aSMatthew Dillon 	}
40024a8d46aSMatthew Dillon 
40124a8d46aSMatthew Dillon 	if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) {
40224a8d46aSMatthew Dillon 		error = EIO;
40324a8d46aSMatthew Dillon 		goto out_free_resp;
40424a8d46aSMatthew Dillon 	}
40524a8d46aSMatthew Dillon 
40624a8d46aSMatthew Dillon 	resp_len = iwm_rx_packet_payload_len(pkt);
40724a8d46aSMatthew Dillon 	if (resp_len != sizeof(*resp)) {
40824a8d46aSMatthew Dillon 		error = EIO;
40924a8d46aSMatthew Dillon 		goto out_free_resp;
41024a8d46aSMatthew Dillon 	}
41124a8d46aSMatthew Dillon 
41224a8d46aSMatthew Dillon 	resp = (void *)pkt->data;
41324a8d46aSMatthew Dillon 	*status = le32toh(resp->status);
41424a8d46aSMatthew Dillon  out_free_resp:
41524a8d46aSMatthew Dillon 	iwm_free_resp(sc, cmd);
41624a8d46aSMatthew Dillon 	return error;
41724a8d46aSMatthew Dillon }
41824a8d46aSMatthew Dillon 
41924a8d46aSMatthew Dillon int
42024a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu_status(struct iwm_softc *sc, uint8_t id,
42124a8d46aSMatthew Dillon 	uint16_t len, const void *data, uint32_t *status)
42224a8d46aSMatthew Dillon {
42324a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
42424a8d46aSMatthew Dillon 		.id = id,
42524a8d46aSMatthew Dillon 		.len = { len, },
42624a8d46aSMatthew Dillon 		.data = { data, },
42724a8d46aSMatthew Dillon 	};
42824a8d46aSMatthew Dillon 
42924a8d46aSMatthew Dillon 	return iwm_mvm_send_cmd_status(sc, &cmd, status);
43024a8d46aSMatthew Dillon }
43124a8d46aSMatthew Dillon 
43224a8d46aSMatthew Dillon void
43324a8d46aSMatthew Dillon iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
43424a8d46aSMatthew Dillon {
43524a8d46aSMatthew Dillon 	KASSERT(sc->sc_wantresp != -1, ("already freed"));
43624a8d46aSMatthew Dillon 	KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC))
43724a8d46aSMatthew Dillon 	    == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags"));
43824a8d46aSMatthew Dillon 	sc->sc_wantresp = -1;
43924a8d46aSMatthew Dillon 	wakeup(&sc->sc_wantresp);
44024a8d46aSMatthew Dillon }
441edfc8a07SImre Vadász 
442edfc8a07SImre Vadász uint8_t
443edfc8a07SImre Vadász iwm_fw_valid_tx_ant(struct iwm_softc *sc)
444edfc8a07SImre Vadász {
445edfc8a07SImre Vadász 	uint8_t tx_ant;
446edfc8a07SImre Vadász 
447edfc8a07SImre Vadász 	tx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_TX_CHAIN)
448edfc8a07SImre Vadász 	    >> IWM_FW_PHY_CFG_TX_CHAIN_POS);
449edfc8a07SImre Vadász 
450edfc8a07SImre Vadász 	if (sc->sc_nvm.valid_tx_ant)
451edfc8a07SImre Vadász 		tx_ant &= sc->sc_nvm.valid_tx_ant;
452edfc8a07SImre Vadász 
453edfc8a07SImre Vadász 	return tx_ant;
454edfc8a07SImre Vadász }
455edfc8a07SImre Vadász 
456edfc8a07SImre Vadász uint8_t
457edfc8a07SImre Vadász iwm_fw_valid_rx_ant(struct iwm_softc *sc)
458edfc8a07SImre Vadász {
459edfc8a07SImre Vadász 	uint8_t rx_ant;
460edfc8a07SImre Vadász 
461edfc8a07SImre Vadász 	rx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_RX_CHAIN)
462edfc8a07SImre Vadász 	    >> IWM_FW_PHY_CFG_RX_CHAIN_POS);
463edfc8a07SImre Vadász 
464edfc8a07SImre Vadász 	if (sc->sc_nvm.valid_rx_ant)
465edfc8a07SImre Vadász 		rx_ant &= sc->sc_nvm.valid_rx_ant;
466edfc8a07SImre Vadász 
467edfc8a07SImre Vadász 	return rx_ant;
468edfc8a07SImre Vadász }
469