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