xref: /freebsd/sys/dev/iwm/if_iwm_util.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1d4886179SRui Paulo /*	$OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $	*/
2d4886179SRui Paulo 
3d4886179SRui Paulo /*
4d4886179SRui Paulo  * Copyright (c) 2014 genua mbh <info@genua.de>
5d4886179SRui Paulo  * Copyright (c) 2014 Fixup Software Ltd.
6d4886179SRui Paulo  *
7d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
8d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
9d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
10d4886179SRui Paulo  *
11d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18d4886179SRui Paulo  */
19d4886179SRui Paulo 
20d4886179SRui Paulo /*-
21d4886179SRui Paulo  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
22d4886179SRui Paulo  * which were used as the reference documentation for this implementation.
23d4886179SRui Paulo  *
24d4886179SRui Paulo  * Driver version we are currently based off of is
25d4886179SRui Paulo  * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd)
26d4886179SRui Paulo  *
27d4886179SRui Paulo  ***********************************************************************
28d4886179SRui Paulo  *
29d4886179SRui Paulo  * This file is provided under a dual BSD/GPLv2 license.  When using or
30d4886179SRui Paulo  * redistributing this file, you may do so under either license.
31d4886179SRui Paulo  *
32d4886179SRui Paulo  * GPL LICENSE SUMMARY
33d4886179SRui Paulo  *
34d4886179SRui Paulo  * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved.
35d4886179SRui Paulo  *
36d4886179SRui Paulo  * This program is free software; you can redistribute it and/or modify
37d4886179SRui Paulo  * it under the terms of version 2 of the GNU General Public License as
38d4886179SRui Paulo  * published by the Free Software Foundation.
39d4886179SRui Paulo  *
40d4886179SRui Paulo  * This program is distributed in the hope that it will be useful, but
41d4886179SRui Paulo  * WITHOUT ANY WARRANTY; without even the implied warranty of
42d4886179SRui Paulo  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
43d4886179SRui Paulo  * General Public License for more details.
44d4886179SRui Paulo  *
45d4886179SRui Paulo  * You should have received a copy of the GNU General Public License
46d4886179SRui Paulo  * along with this program; if not, write to the Free Software
47d4886179SRui Paulo  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
48d4886179SRui Paulo  * USA
49d4886179SRui Paulo  *
50d4886179SRui Paulo  * The full GNU General Public License is included in this distribution
51d4886179SRui Paulo  * in the file called COPYING.
52d4886179SRui Paulo  *
53d4886179SRui Paulo  * Contact Information:
54d4886179SRui Paulo  *  Intel Linux Wireless <ilw@linux.intel.com>
55d4886179SRui Paulo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
56d4886179SRui Paulo  *
57d4886179SRui Paulo  *
58d4886179SRui Paulo  * BSD LICENSE
59d4886179SRui Paulo  *
60d4886179SRui Paulo  * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
61d4886179SRui Paulo  * All rights reserved.
62d4886179SRui Paulo  *
63d4886179SRui Paulo  * Redistribution and use in source and binary forms, with or without
64d4886179SRui Paulo  * modification, are permitted provided that the following conditions
65d4886179SRui Paulo  * are met:
66d4886179SRui Paulo  *
67d4886179SRui Paulo  *  * Redistributions of source code must retain the above copyright
68d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer.
69d4886179SRui Paulo  *  * Redistributions in binary form must reproduce the above copyright
70d4886179SRui Paulo  *    notice, this list of conditions and the following disclaimer in
71d4886179SRui Paulo  *    the documentation and/or other materials provided with the
72d4886179SRui Paulo  *    distribution.
73d4886179SRui Paulo  *  * Neither the name Intel Corporation nor the names of its
74d4886179SRui Paulo  *    contributors may be used to endorse or promote products derived
75d4886179SRui Paulo  *    from this software without specific prior written permission.
76d4886179SRui Paulo  *
77d4886179SRui Paulo  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
78d4886179SRui Paulo  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
79d4886179SRui Paulo  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
80d4886179SRui Paulo  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
81d4886179SRui Paulo  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
82d4886179SRui Paulo  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
83d4886179SRui Paulo  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
84d4886179SRui Paulo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
85d4886179SRui Paulo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
86d4886179SRui Paulo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
87d4886179SRui Paulo  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
88d4886179SRui Paulo  */
89d4886179SRui Paulo 
90d4886179SRui Paulo /*-
91d4886179SRui Paulo  * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr>
92d4886179SRui Paulo  *
93d4886179SRui Paulo  * Permission to use, copy, modify, and distribute this software for any
94d4886179SRui Paulo  * purpose with or without fee is hereby granted, provided that the above
95d4886179SRui Paulo  * copyright notice and this permission notice appear in all copies.
96d4886179SRui Paulo  *
97d4886179SRui Paulo  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98d4886179SRui Paulo  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
99d4886179SRui Paulo  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
100d4886179SRui Paulo  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
101d4886179SRui Paulo  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
102d4886179SRui Paulo  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
103d4886179SRui Paulo  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
104d4886179SRui Paulo  */
105d4886179SRui Paulo #include <sys/cdefs.h>
106b789292fSAndriy Voskoboinyk #include "opt_wlan.h"
107616201d1SAdrian Chadd #include "opt_iwm.h"
108b789292fSAndriy Voskoboinyk 
109d4886179SRui Paulo #include <sys/param.h>
110d4886179SRui Paulo #include <sys/bus.h>
111d4886179SRui Paulo #include <sys/conf.h>
112d4886179SRui Paulo #include <sys/endian.h>
113d4886179SRui Paulo #include <sys/firmware.h>
114d4886179SRui Paulo #include <sys/kernel.h>
115d4886179SRui Paulo #include <sys/malloc.h>
116d4886179SRui Paulo #include <sys/mbuf.h>
117d4886179SRui Paulo #include <sys/mutex.h>
118d4886179SRui Paulo #include <sys/module.h>
119d4886179SRui Paulo #include <sys/proc.h>
120d4886179SRui Paulo #include <sys/rman.h>
121d4886179SRui Paulo #include <sys/socket.h>
122d4886179SRui Paulo #include <sys/sockio.h>
123d4886179SRui Paulo #include <sys/sysctl.h>
124d4886179SRui Paulo #include <sys/linker.h>
125d4886179SRui Paulo 
126d4886179SRui Paulo #include <machine/bus.h>
127d4886179SRui Paulo #include <machine/endian.h>
128d4886179SRui Paulo #include <machine/resource.h>
129d4886179SRui Paulo 
130d4886179SRui Paulo #include <dev/pci/pcivar.h>
131d4886179SRui Paulo #include <dev/pci/pcireg.h>
132d4886179SRui Paulo 
133d4886179SRui Paulo #include <net/bpf.h>
134d4886179SRui Paulo 
135d4886179SRui Paulo #include <net/if.h>
136d4886179SRui Paulo #include <net/if_var.h>
137d4886179SRui Paulo #include <net/if_arp.h>
138d4886179SRui Paulo #include <net/if_dl.h>
139d4886179SRui Paulo #include <net/if_media.h>
140d4886179SRui Paulo #include <net/if_types.h>
141d4886179SRui Paulo 
142d4886179SRui Paulo #include <netinet/in.h>
143d4886179SRui Paulo #include <netinet/in_systm.h>
144d4886179SRui Paulo #include <netinet/if_ether.h>
145d4886179SRui Paulo #include <netinet/ip.h>
146d4886179SRui Paulo 
147d4886179SRui Paulo #include <net80211/ieee80211_var.h>
148d4886179SRui Paulo #include <net80211/ieee80211_regdomain.h>
149d4886179SRui Paulo #include <net80211/ieee80211_ratectl.h>
150d4886179SRui Paulo #include <net80211/ieee80211_radiotap.h>
151d4886179SRui Paulo 
15249fdbf0aSRui Paulo #include <dev/iwm/if_iwmreg.h>
15349fdbf0aSRui Paulo #include <dev/iwm/if_iwmvar.h>
154c44c4d8dSAdrian Chadd #include <dev/iwm/if_iwm_config.h>
15549fdbf0aSRui Paulo #include <dev/iwm/if_iwm_debug.h>
15649fdbf0aSRui Paulo #include <dev/iwm/if_iwm_binding.h>
15749fdbf0aSRui Paulo #include <dev/iwm/if_iwm_util.h>
15849fdbf0aSRui Paulo #include <dev/iwm/if_iwm_pcie_trans.h>
159d4886179SRui Paulo 
160d4886179SRui Paulo /*
161d4886179SRui Paulo  * Send a command to the firmware.  We try to implement the Linux
162d4886179SRui Paulo  * driver interface for the routine.
163d4886179SRui Paulo  * mostly from if_iwn (iwn_cmd()).
164d4886179SRui Paulo  *
165d4886179SRui Paulo  * For now, we always copy the first part and map the second one (if it exists).
166d4886179SRui Paulo  */
167d4886179SRui Paulo int
iwm_send_cmd(struct iwm_softc * sc,struct iwm_host_cmd * hcmd)168d4886179SRui Paulo iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
169d4886179SRui Paulo {
170*e7065dd1SMark Johnston 	struct iwm_tx_ring *ring = &sc->txq[IWM_CMD_QUEUE];
171d4886179SRui Paulo 	struct iwm_tfd *desc;
1726a5bc1d1SSean Bruno 	struct iwm_tx_data *txdata = NULL;
1731eccf203SAdrian Chadd 	struct iwm_device_cmd *cmd;
1741eccf203SAdrian Chadd 	struct mbuf *m;
1751eccf203SAdrian Chadd 	bus_dma_segment_t seg;
176d4886179SRui Paulo 	bus_addr_t paddr;
177d4886179SRui Paulo 	uint32_t addr_lo;
178d4886179SRui Paulo 	int error = 0, i, paylen, off;
179d4886179SRui Paulo 	int code;
180d4886179SRui Paulo 	int async, wantresp;
1816a5bc1d1SSean Bruno 	int group_id;
1821eccf203SAdrian Chadd 	int nsegs;
1836a5bc1d1SSean Bruno 	size_t hdrlen, datasz;
1846a5bc1d1SSean Bruno 	uint8_t *data;
185d4886179SRui Paulo 
186d4886179SRui Paulo 	code = hcmd->id;
187d4886179SRui Paulo 	async = hcmd->flags & IWM_CMD_ASYNC;
188d4886179SRui Paulo 	wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
1896a5bc1d1SSean Bruno 	data = NULL;
190d4886179SRui Paulo 
191d4886179SRui Paulo 	for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
192d4886179SRui Paulo 		paylen += hcmd->len[i];
193d4886179SRui Paulo 	}
194d4886179SRui Paulo 
195d4886179SRui Paulo 	/* if the command wants an answer, busy sc_cmd_resp */
196d4886179SRui Paulo 	if (wantresp) {
197d4886179SRui Paulo 		KASSERT(!async, ("invalid async parameter"));
198d4886179SRui Paulo 		while (sc->sc_wantresp != -1)
199d4886179SRui Paulo 			msleep(&sc->sc_wantresp, &sc->sc_mtx, 0, "iwmcmdsl", 0);
200d4886179SRui Paulo 		sc->sc_wantresp = ring->qid << 16 | ring->cur;
201d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
202d4886179SRui Paulo 		    "wantresp is %x\n", sc->sc_wantresp);
203d4886179SRui Paulo 	}
204d4886179SRui Paulo 
205d4886179SRui Paulo 	/*
206d4886179SRui Paulo 	 * Is the hardware still available?  (after e.g. above wait).
207d4886179SRui Paulo 	 */
208d4886179SRui Paulo 	if (sc->sc_flags & IWM_FLAG_STOPPED) {
209d4886179SRui Paulo 		error = ENXIO;
210d4886179SRui Paulo 		goto out;
211d4886179SRui Paulo 	}
212d4886179SRui Paulo 
213d4886179SRui Paulo 	desc = &ring->desc[ring->cur];
2146a5bc1d1SSean Bruno 	txdata = &ring->data[ring->cur];
215d4886179SRui Paulo 
2166a5bc1d1SSean Bruno 	group_id = iwm_cmd_groupid(code);
2176a5bc1d1SSean Bruno 	if (group_id != 0) {
2186a5bc1d1SSean Bruno 		hdrlen = sizeof(cmd->hdr_wide);
2196a5bc1d1SSean Bruno 		datasz = sizeof(cmd->data_wide);
2206a5bc1d1SSean Bruno 	} else {
2216a5bc1d1SSean Bruno 		hdrlen = sizeof(cmd->hdr);
2226a5bc1d1SSean Bruno 		datasz = sizeof(cmd->data);
2236a5bc1d1SSean Bruno 	}
2246a5bc1d1SSean Bruno 
2256a5bc1d1SSean Bruno 	if (paylen > datasz) {
226d4886179SRui Paulo 		IWM_DPRINTF(sc, IWM_DEBUG_CMD,
227d4886179SRui Paulo 		    "large command paylen=%u len0=%u\n",
228d4886179SRui Paulo 			paylen, hcmd->len[0]);
229d4886179SRui Paulo 		/* Command is too large */
2306a5bc1d1SSean Bruno 		size_t totlen = hdrlen + paylen;
2317a8b94e1SAdrian Chadd 		if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) {
2327a8b94e1SAdrian Chadd 			device_printf(sc->sc_dev,
2337a8b94e1SAdrian Chadd 			    "firmware command too long (%zd bytes)\n",
2346a5bc1d1SSean Bruno 			    totlen);
235d4886179SRui Paulo 			error = EINVAL;
236d4886179SRui Paulo 			goto out;
237d4886179SRui Paulo 		}
2381eccf203SAdrian Chadd 		m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, IWM_RBUF_SIZE);
2391eccf203SAdrian Chadd 		if (m == NULL) {
2401eccf203SAdrian Chadd 			error = ENOBUFS;
241d4886179SRui Paulo 			goto out;
2421eccf203SAdrian Chadd 		}
2431eccf203SAdrian Chadd 
2441eccf203SAdrian Chadd 		m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
2451eccf203SAdrian Chadd 		error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
2466a5bc1d1SSean Bruno 		    txdata->map, m, &seg, &nsegs, BUS_DMA_NOWAIT);
2471eccf203SAdrian Chadd 		if (error != 0) {
2481eccf203SAdrian Chadd 			device_printf(sc->sc_dev,
2491eccf203SAdrian Chadd 			    "%s: can't map mbuf, error %d\n", __func__, error);
2501eccf203SAdrian Chadd 			m_freem(m);
251d4886179SRui Paulo 			goto out;
2521eccf203SAdrian Chadd 		}
2536a5bc1d1SSean Bruno 		txdata->m = m; /* mbuf will be freed in iwm_cmd_done() */
2541eccf203SAdrian Chadd 		cmd = mtod(m, struct iwm_device_cmd *);
2551eccf203SAdrian Chadd 		paddr = seg.ds_addr;
256d4886179SRui Paulo 	} else {
257d4886179SRui Paulo 		cmd = &ring->cmd[ring->cur];
2586a5bc1d1SSean Bruno 		paddr = txdata->cmd_paddr;
259d4886179SRui Paulo 	}
260d4886179SRui Paulo 
2616a5bc1d1SSean Bruno 	if (group_id != 0) {
2626a5bc1d1SSean Bruno 		cmd->hdr_wide.opcode = iwm_cmd_opcode(code);
2636a5bc1d1SSean Bruno 		cmd->hdr_wide.group_id = group_id;
2646a5bc1d1SSean Bruno 		cmd->hdr_wide.qid = ring->qid;
2656a5bc1d1SSean Bruno 		cmd->hdr_wide.idx = ring->cur;
2666a5bc1d1SSean Bruno 		cmd->hdr_wide.length = htole16(paylen);
2676a5bc1d1SSean Bruno 		cmd->hdr_wide.version = iwm_cmd_version(code);
2686a5bc1d1SSean Bruno 		data = cmd->data_wide;
2696a5bc1d1SSean Bruno 	} else {
2706a5bc1d1SSean Bruno 		cmd->hdr.code = iwm_cmd_opcode(code);
271d4886179SRui Paulo 		cmd->hdr.flags = 0;
272d4886179SRui Paulo 		cmd->hdr.qid = ring->qid;
273d4886179SRui Paulo 		cmd->hdr.idx = ring->cur;
2746a5bc1d1SSean Bruno 		data = cmd->data;
2756a5bc1d1SSean Bruno 	}
276d4886179SRui Paulo 
277d4886179SRui Paulo 	for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
278d4886179SRui Paulo 		if (hcmd->len[i] == 0)
279d4886179SRui Paulo 			continue;
2806a5bc1d1SSean Bruno 		memcpy(data + off, hcmd->data[i], hcmd->len[i]);
281d4886179SRui Paulo 		off += hcmd->len[i];
282d4886179SRui Paulo 	}
283d4886179SRui Paulo 	KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
284d4886179SRui Paulo 
285d4886179SRui Paulo 	/* lo field is not aligned */
286d4886179SRui Paulo 	addr_lo = htole32((uint32_t)paddr);
287d4886179SRui Paulo 	memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
288d4886179SRui Paulo 	desc->tbs[0].hi_n_len  = htole16(iwm_get_dma_hi_addr(paddr)
2896a5bc1d1SSean Bruno 	    | ((hdrlen + paylen) << 4));
290d4886179SRui Paulo 	desc->num_tbs = 1;
291d4886179SRui Paulo 
292d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
2936a5bc1d1SSean Bruno 	    "iwm_send_cmd 0x%x size=%lu %s\n",
294d4886179SRui Paulo 	    code,
2956a5bc1d1SSean Bruno 	    (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen),
296d4886179SRui Paulo 	    async ? " (async)" : "");
297d4886179SRui Paulo 
2986a5bc1d1SSean Bruno 	if (paylen > datasz) {
2996a5bc1d1SSean Bruno 		bus_dmamap_sync(ring->data_dmat, txdata->map,
300d4886179SRui Paulo 		    BUS_DMASYNC_PREWRITE);
301d4886179SRui Paulo 	} else {
302d4886179SRui Paulo 		bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
303d4886179SRui Paulo 		    BUS_DMASYNC_PREWRITE);
304d4886179SRui Paulo 	}
305d4886179SRui Paulo 	bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
306d4886179SRui Paulo 	    BUS_DMASYNC_PREWRITE);
307d4886179SRui Paulo 
30899baf303SAdrian Chadd 	error = iwm_pcie_set_cmd_in_flight(sc);
30999baf303SAdrian Chadd 	if (error)
310d4886179SRui Paulo 		goto out;
31199baf303SAdrian Chadd 	ring->queued++;
312d4886179SRui Paulo 
313d4886179SRui Paulo #if 0
314d4886179SRui Paulo 	iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
315d4886179SRui Paulo #endif
316d4886179SRui Paulo 	IWM_DPRINTF(sc, IWM_DEBUG_CMD,
317d4886179SRui Paulo 	    "sending command 0x%x qid %d, idx %d\n",
318d4886179SRui Paulo 	    code, ring->qid, ring->cur);
319d4886179SRui Paulo 
320d4886179SRui Paulo 	/* Kick command ring. */
321d4886179SRui Paulo 	ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
322d4886179SRui Paulo 	IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
323d4886179SRui Paulo 
324d4886179SRui Paulo 	if (!async) {
325d4886179SRui Paulo 		/* m..m-mmyy-mmyyyy-mym-ym m-my generation */
326d4886179SRui Paulo 		int generation = sc->sc_generation;
327d4886179SRui Paulo 		error = msleep(desc, &sc->sc_mtx, PCATCH, "iwmcmd", hz);
328d4886179SRui Paulo 		if (error == 0) {
329d4886179SRui Paulo 			/* if hardware is no longer up, return error */
330d4886179SRui Paulo 			if (generation != sc->sc_generation) {
331d4886179SRui Paulo 				error = ENXIO;
332d4886179SRui Paulo 			} else {
333d4886179SRui Paulo 				hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
334d4886179SRui Paulo 			}
335d4886179SRui Paulo 		}
336d4886179SRui Paulo 	}
337d4886179SRui Paulo  out:
338d4886179SRui Paulo 	if (wantresp && error != 0) {
339d4886179SRui Paulo 		iwm_free_resp(sc, hcmd);
340d4886179SRui Paulo 	}
341d4886179SRui Paulo 
342d4886179SRui Paulo 	return error;
343d4886179SRui Paulo }
344d4886179SRui Paulo 
345d4886179SRui Paulo /* iwlwifi: mvm/utils.c */
346d4886179SRui Paulo int
iwm_send_cmd_pdu(struct iwm_softc * sc,uint32_t id,uint32_t flags,uint16_t len,const void * data)347*e7065dd1SMark Johnston iwm_send_cmd_pdu(struct iwm_softc *sc, uint32_t id,
348d4886179SRui Paulo 	uint32_t flags, uint16_t len, const void *data)
349d4886179SRui Paulo {
350d4886179SRui Paulo 	struct iwm_host_cmd cmd = {
351d4886179SRui Paulo 		.id = id,
352d4886179SRui Paulo 		.len = { len, },
353d4886179SRui Paulo 		.data = { data, },
354d4886179SRui Paulo 		.flags = flags,
355d4886179SRui Paulo 	};
356d4886179SRui Paulo 
357d4886179SRui Paulo 	return iwm_send_cmd(sc, &cmd);
358d4886179SRui Paulo }
359d4886179SRui Paulo 
360d4886179SRui Paulo /* iwlwifi: mvm/utils.c */
361d4886179SRui Paulo int
iwm_send_cmd_status(struct iwm_softc * sc,struct iwm_host_cmd * cmd,uint32_t * status)362*e7065dd1SMark Johnston iwm_send_cmd_status(struct iwm_softc *sc,
363d4886179SRui Paulo 	struct iwm_host_cmd *cmd, uint32_t *status)
364d4886179SRui Paulo {
365d4886179SRui Paulo 	struct iwm_rx_packet *pkt;
366d4886179SRui Paulo 	struct iwm_cmd_response *resp;
367d4886179SRui Paulo 	int error, resp_len;
368d4886179SRui Paulo 
369d4886179SRui Paulo 	KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0,
370d4886179SRui Paulo 	    ("invalid command"));
371d4886179SRui Paulo 	cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB;
372d4886179SRui Paulo 
373d4886179SRui Paulo 	if ((error = iwm_send_cmd(sc, cmd)) != 0)
374d4886179SRui Paulo 		return error;
375d4886179SRui Paulo 	pkt = cmd->resp_pkt;
376d4886179SRui Paulo 
377d4886179SRui Paulo 	/* Can happen if RFKILL is asserted */
378d4886179SRui Paulo 	if (!pkt) {
379d4886179SRui Paulo 		error = 0;
380d4886179SRui Paulo 		goto out_free_resp;
381d4886179SRui Paulo 	}
382d4886179SRui Paulo 
383d4886179SRui Paulo 	if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) {
384d4886179SRui Paulo 		error = EIO;
385d4886179SRui Paulo 		goto out_free_resp;
386d4886179SRui Paulo 	}
387d4886179SRui Paulo 
388d4886179SRui Paulo 	resp_len = iwm_rx_packet_payload_len(pkt);
389d4886179SRui Paulo 	if (resp_len != sizeof(*resp)) {
390d4886179SRui Paulo 		error = EIO;
391d4886179SRui Paulo 		goto out_free_resp;
392d4886179SRui Paulo 	}
393d4886179SRui Paulo 
394d4886179SRui Paulo 	resp = (void *)pkt->data;
395d4886179SRui Paulo 	*status = le32toh(resp->status);
396d4886179SRui Paulo  out_free_resp:
397d4886179SRui Paulo 	iwm_free_resp(sc, cmd);
398d4886179SRui Paulo 	return error;
399d4886179SRui Paulo }
400d4886179SRui Paulo 
401d4886179SRui Paulo /* iwlwifi/mvm/utils.c */
402d4886179SRui Paulo int
iwm_send_cmd_pdu_status(struct iwm_softc * sc,uint32_t id,uint16_t len,const void * data,uint32_t * status)403*e7065dd1SMark Johnston iwm_send_cmd_pdu_status(struct iwm_softc *sc, uint32_t id,
404d4886179SRui Paulo 	uint16_t len, const void *data, uint32_t *status)
405d4886179SRui Paulo {
406d4886179SRui Paulo 	struct iwm_host_cmd cmd = {
407d4886179SRui Paulo 		.id = id,
408d4886179SRui Paulo 		.len = { len, },
409d4886179SRui Paulo 		.data = { data, },
410d4886179SRui Paulo 	};
411d4886179SRui Paulo 
412*e7065dd1SMark Johnston 	return iwm_send_cmd_status(sc, &cmd, status);
413d4886179SRui Paulo }
414d4886179SRui Paulo 
415d4886179SRui Paulo void
iwm_free_resp(struct iwm_softc * sc,struct iwm_host_cmd * hcmd)416d4886179SRui Paulo iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
417d4886179SRui Paulo {
418d4886179SRui Paulo 	KASSERT(sc->sc_wantresp != -1, ("already freed"));
419d4886179SRui Paulo 	KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC))
420d4886179SRui Paulo 	    == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags"));
421d4886179SRui Paulo 	sc->sc_wantresp = -1;
422d4886179SRui Paulo 	wakeup(&sc->sc_wantresp);
423d4886179SRui Paulo }
424b28a6ab3SAdrian Chadd 
425b28a6ab3SAdrian Chadd static void
iwm_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nsegs,int error)426b28a6ab3SAdrian Chadd iwm_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
427b28a6ab3SAdrian Chadd {
428b28a6ab3SAdrian Chadd         if (error != 0)
429b28a6ab3SAdrian Chadd                 return;
430b28a6ab3SAdrian Chadd 	KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
431b28a6ab3SAdrian Chadd 	*(bus_addr_t *)arg = segs[0].ds_addr;
432b28a6ab3SAdrian Chadd }
433b28a6ab3SAdrian Chadd 
434b28a6ab3SAdrian Chadd int
iwm_dma_contig_alloc(bus_dma_tag_t tag,struct iwm_dma_info * dma,bus_size_t size,bus_size_t alignment)435b28a6ab3SAdrian Chadd iwm_dma_contig_alloc(bus_dma_tag_t tag, struct iwm_dma_info *dma,
436b28a6ab3SAdrian Chadd     bus_size_t size, bus_size_t alignment)
437b28a6ab3SAdrian Chadd {
438b28a6ab3SAdrian Chadd 	int error;
439b28a6ab3SAdrian Chadd 
440b28a6ab3SAdrian Chadd 	dma->tag = NULL;
441b28a6ab3SAdrian Chadd 	dma->map = NULL;
442b28a6ab3SAdrian Chadd 	dma->size = size;
443b28a6ab3SAdrian Chadd 	dma->vaddr = NULL;
444b28a6ab3SAdrian Chadd 
445b28a6ab3SAdrian Chadd 	error = bus_dma_tag_create(tag, alignment,
446b28a6ab3SAdrian Chadd             0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
447b28a6ab3SAdrian Chadd             1, size, 0, NULL, NULL, &dma->tag);
448b28a6ab3SAdrian Chadd         if (error != 0)
449b28a6ab3SAdrian Chadd                 goto fail;
450b28a6ab3SAdrian Chadd 
451b28a6ab3SAdrian Chadd         error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
452b28a6ab3SAdrian Chadd             BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
453b28a6ab3SAdrian Chadd         if (error != 0)
454b28a6ab3SAdrian Chadd                 goto fail;
455b28a6ab3SAdrian Chadd 
456b28a6ab3SAdrian Chadd         error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
457b28a6ab3SAdrian Chadd             iwm_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
458b28a6ab3SAdrian Chadd         if (error != 0) {
459b28a6ab3SAdrian Chadd 		bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
460b28a6ab3SAdrian Chadd 		dma->vaddr = NULL;
461b28a6ab3SAdrian Chadd 		goto fail;
462b28a6ab3SAdrian Chadd 	}
463b28a6ab3SAdrian Chadd 
464b28a6ab3SAdrian Chadd 	bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
465b28a6ab3SAdrian Chadd 
466b28a6ab3SAdrian Chadd 	return 0;
467b28a6ab3SAdrian Chadd 
468b28a6ab3SAdrian Chadd fail:
469b28a6ab3SAdrian Chadd 	iwm_dma_contig_free(dma);
470b28a6ab3SAdrian Chadd 
471b28a6ab3SAdrian Chadd 	return error;
472b28a6ab3SAdrian Chadd }
473b28a6ab3SAdrian Chadd 
474b28a6ab3SAdrian Chadd void
iwm_dma_contig_free(struct iwm_dma_info * dma)475b28a6ab3SAdrian Chadd iwm_dma_contig_free(struct iwm_dma_info *dma)
476b28a6ab3SAdrian Chadd {
477b28a6ab3SAdrian Chadd 	if (dma->vaddr != NULL) {
478b28a6ab3SAdrian Chadd 		bus_dmamap_sync(dma->tag, dma->map,
479b28a6ab3SAdrian Chadd 		    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
480b28a6ab3SAdrian Chadd 		bus_dmamap_unload(dma->tag, dma->map);
481b28a6ab3SAdrian Chadd 		bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
482b28a6ab3SAdrian Chadd 		dma->vaddr = NULL;
483b28a6ab3SAdrian Chadd 	}
484b28a6ab3SAdrian Chadd 	if (dma->tag != NULL) {
485b28a6ab3SAdrian Chadd 		bus_dma_tag_destroy(dma->tag);
486b28a6ab3SAdrian Chadd 		dma->tag = NULL;
487b28a6ab3SAdrian Chadd 	}
488b28a6ab3SAdrian Chadd }
489c44c4d8dSAdrian Chadd 
490f55c604bSAdrian Chadd /**
491*e7065dd1SMark Johnston  * iwm_send_lq_cmd() - Send link quality command
492f55c604bSAdrian Chadd  * @init: This command is sent as part of station initialization right
493f55c604bSAdrian Chadd  *        after station has been added.
494f55c604bSAdrian Chadd  *
495f55c604bSAdrian Chadd  * The link quality command is sent as the last step of station creation.
496f55c604bSAdrian Chadd  * This is the special case in which init is set and we call a callback in
497f55c604bSAdrian Chadd  * this case to clear the state indicating that station creation is in
498f55c604bSAdrian Chadd  * progress.
499f55c604bSAdrian Chadd  */
500f55c604bSAdrian Chadd int
iwm_send_lq_cmd(struct iwm_softc * sc,struct iwm_lq_cmd * lq,boolean_t init)501*e7065dd1SMark Johnston iwm_send_lq_cmd(struct iwm_softc *sc, struct iwm_lq_cmd *lq, boolean_t init)
502f55c604bSAdrian Chadd {
503f55c604bSAdrian Chadd 	struct iwm_host_cmd cmd = {
504f55c604bSAdrian Chadd 		.id = IWM_LQ_CMD,
505f55c604bSAdrian Chadd 		.len = { sizeof(struct iwm_lq_cmd), },
506f55c604bSAdrian Chadd 		.flags = init ? 0 : IWM_CMD_ASYNC,
507f55c604bSAdrian Chadd 		.data = { lq, },
508f55c604bSAdrian Chadd 	};
509f55c604bSAdrian Chadd 
510*e7065dd1SMark Johnston 	if (lq->sta_id == IWM_STATION_COUNT)
511f55c604bSAdrian Chadd 		return EINVAL;
512f55c604bSAdrian Chadd 
513f55c604bSAdrian Chadd 	return iwm_send_cmd(sc, &cmd);
514f55c604bSAdrian Chadd }
515f55c604bSAdrian Chadd 
516c44c4d8dSAdrian Chadd boolean_t
iwm_rx_diversity_allowed(struct iwm_softc * sc)517*e7065dd1SMark Johnston iwm_rx_diversity_allowed(struct iwm_softc *sc)
518c44c4d8dSAdrian Chadd {
519*e7065dd1SMark Johnston 	if (num_of_ant(iwm_get_valid_rx_ant(sc)) == 1)
520c44c4d8dSAdrian Chadd 		return FALSE;
521c44c4d8dSAdrian Chadd 
522c44c4d8dSAdrian Chadd 	/*
523c44c4d8dSAdrian Chadd 	 * XXX Also return FALSE when SMPS (Spatial Multiplexing Powersave)
524c44c4d8dSAdrian Chadd 	 *     is used on any vap (in the future).
525c44c4d8dSAdrian Chadd 	 */
526c44c4d8dSAdrian Chadd 
527c44c4d8dSAdrian Chadd 	return TRUE;
528c44c4d8dSAdrian Chadd }
529