xref: /dflybsd-src/sys/dev/netif/iwm/if_iwm_util.c (revision b0b29253e3db91e34b7961057796c165fbdd272e)
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>
13445bc40b1SMatthew Dillon #include <net/ethernet.h>
13524a8d46aSMatthew Dillon #include <net/if_dl.h>
13624a8d46aSMatthew Dillon #include <net/if_media.h>
13724a8d46aSMatthew Dillon #include <net/if_types.h>
13845bc40b1SMatthew Dillon #include <net/ifq_var.h>
13924a8d46aSMatthew Dillon 
14024a8d46aSMatthew Dillon #include <netinet/in.h>
14124a8d46aSMatthew Dillon #include <netinet/in_systm.h>
14224a8d46aSMatthew Dillon #include <netinet/if_ether.h>
14324a8d46aSMatthew Dillon #include <netinet/ip.h>
14424a8d46aSMatthew Dillon 
14545bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
14645bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
14745bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
14845bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
14924a8d46aSMatthew Dillon 
15045bc40b1SMatthew Dillon #include "if_iwmreg.h"
15145bc40b1SMatthew Dillon #include "if_iwmvar.h"
15245bc40b1SMatthew Dillon #include "if_iwm_debug.h"
15345bc40b1SMatthew Dillon #include "if_iwm_binding.h"
15445bc40b1SMatthew Dillon #include "if_iwm_util.h"
15545bc40b1SMatthew Dillon #include "if_iwm_pcie_trans.h"
15645bc40b1SMatthew Dillon 
157*b0b29253SMatthew Dillon #if defined(__DragonFly__)
15845bc40b1SMatthew Dillon int iwmsleep(void *chan, struct lock *lk, int flags, const char *wmesg, int to);
159*b0b29253SMatthew Dillon #endif
16024a8d46aSMatthew Dillon 
16124a8d46aSMatthew Dillon static void
16224a8d46aSMatthew Dillon iwm_dma_map_mem(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
16324a8d46aSMatthew Dillon {
16424a8d46aSMatthew Dillon         if (error != 0)
16524a8d46aSMatthew Dillon                 return;
16624a8d46aSMatthew Dillon 	KASSERT(nsegs <= 2, ("too many DMA segments, %d should be <= 2",
16724a8d46aSMatthew Dillon 	    nsegs));
16824a8d46aSMatthew Dillon 	if (nsegs > 1)
16924a8d46aSMatthew Dillon 		KASSERT(segs[1].ds_addr == segs[0].ds_addr + segs[0].ds_len,
17024a8d46aSMatthew Dillon 		    ("fragmented DMA memory"));
17124a8d46aSMatthew Dillon 	*(bus_addr_t *)arg = segs[0].ds_addr;
17224a8d46aSMatthew Dillon }
17324a8d46aSMatthew Dillon 
17424a8d46aSMatthew Dillon /*
17524a8d46aSMatthew Dillon  * Send a command to the firmware.  We try to implement the Linux
17624a8d46aSMatthew Dillon  * driver interface for the routine.
17724a8d46aSMatthew Dillon  * mostly from if_iwn (iwn_cmd()).
17824a8d46aSMatthew Dillon  *
17924a8d46aSMatthew Dillon  * For now, we always copy the first part and map the second one (if it exists).
18024a8d46aSMatthew Dillon  */
18124a8d46aSMatthew Dillon int
18224a8d46aSMatthew Dillon iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
18324a8d46aSMatthew Dillon {
18424a8d46aSMatthew Dillon 	struct iwm_tx_ring *ring = &sc->txq[IWM_MVM_CMD_QUEUE];
18524a8d46aSMatthew Dillon 	struct iwm_tfd *desc;
18624a8d46aSMatthew Dillon 	struct iwm_tx_data *data;
18724a8d46aSMatthew Dillon 	struct iwm_device_cmd *cmd = NULL;
18824a8d46aSMatthew Dillon 	bus_addr_t paddr;
18924a8d46aSMatthew Dillon 	uint32_t addr_lo;
19024a8d46aSMatthew Dillon 	int error = 0, i, paylen, off;
19124a8d46aSMatthew Dillon 	int code;
19224a8d46aSMatthew Dillon 	int async, wantresp;
19324a8d46aSMatthew Dillon 
19424a8d46aSMatthew Dillon 	code = hcmd->id;
19524a8d46aSMatthew Dillon 	async = hcmd->flags & IWM_CMD_ASYNC;
19624a8d46aSMatthew Dillon 	wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
19745bc40b1SMatthew Dillon 	data = NULL;
19824a8d46aSMatthew Dillon 
19924a8d46aSMatthew Dillon 	for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
20024a8d46aSMatthew Dillon 		paylen += hcmd->len[i];
20124a8d46aSMatthew Dillon 	}
20224a8d46aSMatthew Dillon 
20324a8d46aSMatthew Dillon 	/* if the command wants an answer, busy sc_cmd_resp */
20424a8d46aSMatthew Dillon 	if (wantresp) {
20524a8d46aSMatthew Dillon 		KASSERT(!async, ("invalid async parameter"));
206*b0b29253SMatthew Dillon 		while (sc->sc_wantresp != -1) {
207*b0b29253SMatthew Dillon #if defined(__DragonFly__)
20845bc40b1SMatthew Dillon 			iwmsleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0);
209*b0b29253SMatthew Dillon #else
210*b0b29253SMatthew Dillon 			msleep(&sc->sc_wantresp, &sc->sc_mtx, 0, "iwmcmdsl", 0);
211*b0b29253SMatthew Dillon #endif
212*b0b29253SMatthew Dillon 		}
21324a8d46aSMatthew Dillon 		sc->sc_wantresp = ring->qid << 16 | ring->cur;
21424a8d46aSMatthew Dillon 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
21524a8d46aSMatthew Dillon 		    "wantresp is %x\n", sc->sc_wantresp);
21624a8d46aSMatthew Dillon 	}
21724a8d46aSMatthew Dillon 
21824a8d46aSMatthew Dillon 	/*
21924a8d46aSMatthew Dillon 	 * Is the hardware still available?  (after e.g. above wait).
22024a8d46aSMatthew Dillon 	 */
22124a8d46aSMatthew Dillon 	if (sc->sc_flags & IWM_FLAG_STOPPED) {
22224a8d46aSMatthew Dillon 		error = ENXIO;
22324a8d46aSMatthew Dillon 		goto out;
22424a8d46aSMatthew Dillon 	}
22524a8d46aSMatthew Dillon 
22624a8d46aSMatthew Dillon 	desc = &ring->desc[ring->cur];
22724a8d46aSMatthew Dillon 	data = &ring->data[ring->cur];
22824a8d46aSMatthew Dillon 
22924a8d46aSMatthew Dillon 	if (paylen > sizeof(cmd->data)) {
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 */
23424a8d46aSMatthew Dillon 		if (sizeof(cmd->hdr) + paylen > IWM_RBUF_SIZE) {
23524a8d46aSMatthew Dillon 			error = EINVAL;
23624a8d46aSMatthew Dillon 			goto out;
23724a8d46aSMatthew Dillon 		}
23824a8d46aSMatthew Dillon 		error = bus_dmamem_alloc(ring->data_dmat, (void **)&cmd,
23924a8d46aSMatthew Dillon 		    BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &data->map);
24024a8d46aSMatthew Dillon 		if (error != 0)
24124a8d46aSMatthew Dillon 			goto out;
24224a8d46aSMatthew Dillon 		error = bus_dmamap_load(ring->data_dmat, data->map,
24324a8d46aSMatthew Dillon 		    cmd, paylen + sizeof(cmd->hdr), iwm_dma_map_mem,
24424a8d46aSMatthew Dillon 		    &paddr, BUS_DMA_NOWAIT);
24524a8d46aSMatthew Dillon 		if (error != 0)
24624a8d46aSMatthew Dillon 			goto out;
24724a8d46aSMatthew Dillon 	} else {
24824a8d46aSMatthew Dillon 		cmd = &ring->cmd[ring->cur];
24924a8d46aSMatthew Dillon 		paddr = data->cmd_paddr;
25024a8d46aSMatthew Dillon 	}
25124a8d46aSMatthew Dillon 
25224a8d46aSMatthew Dillon 	cmd->hdr.code = code;
25324a8d46aSMatthew Dillon 	cmd->hdr.flags = 0;
25424a8d46aSMatthew Dillon 	cmd->hdr.qid = ring->qid;
25524a8d46aSMatthew Dillon 	cmd->hdr.idx = ring->cur;
25624a8d46aSMatthew Dillon 
25724a8d46aSMatthew Dillon 	for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
25824a8d46aSMatthew Dillon 		if (hcmd->len[i] == 0)
25924a8d46aSMatthew Dillon 			continue;
26024a8d46aSMatthew Dillon 		memcpy(cmd->data + off, hcmd->data[i], hcmd->len[i]);
26124a8d46aSMatthew Dillon 		off += hcmd->len[i];
26224a8d46aSMatthew Dillon 	}
26324a8d46aSMatthew Dillon 	KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
26424a8d46aSMatthew Dillon 
26524a8d46aSMatthew Dillon 	/* lo field is not aligned */
26624a8d46aSMatthew Dillon 	addr_lo = htole32((uint32_t)paddr);
26724a8d46aSMatthew Dillon 	memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
26824a8d46aSMatthew Dillon 	desc->tbs[0].hi_n_len  = htole16(iwm_get_dma_hi_addr(paddr)
26924a8d46aSMatthew Dillon 	    | ((sizeof(cmd->hdr) + paylen) << 4));
27024a8d46aSMatthew Dillon 	desc->num_tbs = 1;
27124a8d46aSMatthew Dillon 
27224a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
27324a8d46aSMatthew Dillon 	    "%s: iwm_send_cmd 0x%x size=%lu %s\n",
27424a8d46aSMatthew Dillon 	    __func__,
27524a8d46aSMatthew Dillon 	    code,
27624a8d46aSMatthew Dillon 	    (unsigned long) (hcmd->len[0] + hcmd->len[1] + sizeof(cmd->hdr)),
27724a8d46aSMatthew Dillon 	    async ? " (async)" : "");
27824a8d46aSMatthew Dillon 
27924a8d46aSMatthew Dillon 	if (hcmd->len[0] > sizeof(cmd->data)) {
28024a8d46aSMatthew Dillon 		bus_dmamap_sync(ring->data_dmat, data->map,
28124a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
28224a8d46aSMatthew Dillon 	} else {
28324a8d46aSMatthew Dillon 		bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
28424a8d46aSMatthew Dillon 		    BUS_DMASYNC_PREWRITE);
28524a8d46aSMatthew Dillon 	}
28624a8d46aSMatthew Dillon 	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
28724a8d46aSMatthew Dillon 	    BUS_DMASYNC_PREWRITE);
28824a8d46aSMatthew Dillon 
28924a8d46aSMatthew Dillon 	IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
29024a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
29124a8d46aSMatthew Dillon 	if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
29224a8d46aSMatthew Dillon 	    IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
29324a8d46aSMatthew Dillon 	    (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
29424a8d46aSMatthew Dillon 	     IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000)) {
29524a8d46aSMatthew Dillon 		device_printf(sc->sc_dev,
29624a8d46aSMatthew Dillon 		    "%s: acquiring device failed\n", __func__);
29724a8d46aSMatthew Dillon 		error = EBUSY;
29824a8d46aSMatthew Dillon 		goto out;
29924a8d46aSMatthew Dillon 	}
30024a8d46aSMatthew Dillon 
30124a8d46aSMatthew Dillon #if 0
30224a8d46aSMatthew Dillon 	iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
30324a8d46aSMatthew Dillon #endif
30424a8d46aSMatthew Dillon 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
30524a8d46aSMatthew Dillon 	    "sending command 0x%x qid %d, idx %d\n",
30624a8d46aSMatthew Dillon 	    code, ring->qid, ring->cur);
30724a8d46aSMatthew Dillon 
30824a8d46aSMatthew Dillon 	/* Kick command ring. */
30924a8d46aSMatthew Dillon 	ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
31024a8d46aSMatthew Dillon 	IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
31124a8d46aSMatthew Dillon 
31224a8d46aSMatthew Dillon 	if (!async) {
31324a8d46aSMatthew Dillon 		/* m..m-mmyy-mmyyyy-mym-ym m-my generation */
31424a8d46aSMatthew Dillon 		int generation = sc->sc_generation;
315*b0b29253SMatthew Dillon #if defined(__DragonFly__)
31645bc40b1SMatthew Dillon 		error = iwmsleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz);
317*b0b29253SMatthew Dillon #else
318*b0b29253SMatthew Dillon 		error = msleep(desc, &sc->sc_mtx, PCATCH, "iwmcmd", hz);
319*b0b29253SMatthew Dillon #endif
32024a8d46aSMatthew Dillon 		if (error == 0) {
32124a8d46aSMatthew Dillon 			/* if hardware is no longer up, return error */
32224a8d46aSMatthew Dillon 			if (generation != sc->sc_generation) {
32324a8d46aSMatthew Dillon 				error = ENXIO;
32424a8d46aSMatthew Dillon 			} else {
32524a8d46aSMatthew Dillon 				hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
32624a8d46aSMatthew Dillon 			}
32724a8d46aSMatthew Dillon 		}
32824a8d46aSMatthew Dillon 	}
32924a8d46aSMatthew Dillon  out:
33024a8d46aSMatthew Dillon 	if (cmd && paylen > sizeof(cmd->data))
33124a8d46aSMatthew Dillon 		bus_dmamem_free(ring->data_dmat, cmd, data->map);
33224a8d46aSMatthew Dillon 	if (wantresp && error != 0) {
33324a8d46aSMatthew Dillon 		iwm_free_resp(sc, hcmd);
33424a8d46aSMatthew Dillon 	}
33524a8d46aSMatthew Dillon 
33624a8d46aSMatthew Dillon 	return error;
33724a8d46aSMatthew Dillon }
33824a8d46aSMatthew Dillon 
33924a8d46aSMatthew Dillon /* iwlwifi: mvm/utils.c */
34024a8d46aSMatthew Dillon int
34124a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu(struct iwm_softc *sc, uint8_t id,
34224a8d46aSMatthew Dillon 	uint32_t flags, uint16_t len, const void *data)
34324a8d46aSMatthew Dillon {
34424a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
34524a8d46aSMatthew Dillon 		.id = id,
34624a8d46aSMatthew Dillon 		.len = { len, },
34724a8d46aSMatthew Dillon 		.data = { data, },
34824a8d46aSMatthew Dillon 		.flags = flags,
34924a8d46aSMatthew Dillon 	};
35024a8d46aSMatthew Dillon 
35124a8d46aSMatthew Dillon 	return iwm_send_cmd(sc, &cmd);
35224a8d46aSMatthew Dillon }
35324a8d46aSMatthew Dillon 
35424a8d46aSMatthew Dillon /* iwlwifi: mvm/utils.c */
35524a8d46aSMatthew Dillon int
35624a8d46aSMatthew Dillon iwm_mvm_send_cmd_status(struct iwm_softc *sc,
35724a8d46aSMatthew Dillon 	struct iwm_host_cmd *cmd, uint32_t *status)
35824a8d46aSMatthew Dillon {
35924a8d46aSMatthew Dillon 	struct iwm_rx_packet *pkt;
36024a8d46aSMatthew Dillon 	struct iwm_cmd_response *resp;
36124a8d46aSMatthew Dillon 	int error, resp_len;
36224a8d46aSMatthew Dillon 
36324a8d46aSMatthew Dillon 	KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0,
36424a8d46aSMatthew Dillon 	    ("invalid command"));
36524a8d46aSMatthew Dillon 	cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB;
36624a8d46aSMatthew Dillon 
36724a8d46aSMatthew Dillon 	if ((error = iwm_send_cmd(sc, cmd)) != 0)
36824a8d46aSMatthew Dillon 		return error;
36924a8d46aSMatthew Dillon 	pkt = cmd->resp_pkt;
37024a8d46aSMatthew Dillon 
37124a8d46aSMatthew Dillon 	/* Can happen if RFKILL is asserted */
37224a8d46aSMatthew Dillon 	if (!pkt) {
37324a8d46aSMatthew Dillon 		error = 0;
37424a8d46aSMatthew Dillon 		goto out_free_resp;
37524a8d46aSMatthew Dillon 	}
37624a8d46aSMatthew Dillon 
37724a8d46aSMatthew Dillon 	if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) {
37824a8d46aSMatthew Dillon 		error = EIO;
37924a8d46aSMatthew Dillon 		goto out_free_resp;
38024a8d46aSMatthew Dillon 	}
38124a8d46aSMatthew Dillon 
38224a8d46aSMatthew Dillon 	resp_len = iwm_rx_packet_payload_len(pkt);
38324a8d46aSMatthew Dillon 	if (resp_len != sizeof(*resp)) {
38424a8d46aSMatthew Dillon 		error = EIO;
38524a8d46aSMatthew Dillon 		goto out_free_resp;
38624a8d46aSMatthew Dillon 	}
38724a8d46aSMatthew Dillon 
38824a8d46aSMatthew Dillon 	resp = (void *)pkt->data;
38924a8d46aSMatthew Dillon 	*status = le32toh(resp->status);
39024a8d46aSMatthew Dillon  out_free_resp:
39124a8d46aSMatthew Dillon 	iwm_free_resp(sc, cmd);
39224a8d46aSMatthew Dillon 	return error;
39324a8d46aSMatthew Dillon }
39424a8d46aSMatthew Dillon 
39524a8d46aSMatthew Dillon /* iwlwifi/mvm/utils.c */
39624a8d46aSMatthew Dillon int
39724a8d46aSMatthew Dillon iwm_mvm_send_cmd_pdu_status(struct iwm_softc *sc, uint8_t id,
39824a8d46aSMatthew Dillon 	uint16_t len, const void *data, uint32_t *status)
39924a8d46aSMatthew Dillon {
40024a8d46aSMatthew Dillon 	struct iwm_host_cmd cmd = {
40124a8d46aSMatthew Dillon 		.id = id,
40224a8d46aSMatthew Dillon 		.len = { len, },
40324a8d46aSMatthew Dillon 		.data = { data, },
40424a8d46aSMatthew Dillon 	};
40524a8d46aSMatthew Dillon 
40624a8d46aSMatthew Dillon 	return iwm_mvm_send_cmd_status(sc, &cmd, status);
40724a8d46aSMatthew Dillon }
40824a8d46aSMatthew Dillon 
40924a8d46aSMatthew Dillon void
41024a8d46aSMatthew Dillon iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
41124a8d46aSMatthew Dillon {
41224a8d46aSMatthew Dillon 	KASSERT(sc->sc_wantresp != -1, ("already freed"));
41324a8d46aSMatthew Dillon 	KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC))
41424a8d46aSMatthew Dillon 	    == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags"));
41524a8d46aSMatthew Dillon 	sc->sc_wantresp = -1;
41624a8d46aSMatthew Dillon 	wakeup(&sc->sc_wantresp);
41724a8d46aSMatthew Dillon }
418