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/param.h>
10624a8d46aSMatthew Dillon #include <sys/bus.h>
107*6acbba79SMatthew Dillon #include <sys/conf.h>
10824a8d46aSMatthew Dillon #include <sys/endian.h>
10924a8d46aSMatthew Dillon #include <sys/firmware.h>
11024a8d46aSMatthew Dillon #include <sys/kernel.h>
11124a8d46aSMatthew Dillon #include <sys/malloc.h>
11224a8d46aSMatthew Dillon #include <sys/mbuf.h>
11324a8d46aSMatthew Dillon #include <sys/rman.h>
11424a8d46aSMatthew Dillon #include <sys/sysctl.h>
11524a8d46aSMatthew Dillon #include <sys/linker.h>
11624a8d46aSMatthew Dillon
11724a8d46aSMatthew Dillon #include <machine/endian.h>
11824a8d46aSMatthew Dillon
11945bc40b1SMatthew Dillon #include <bus/pci/pcivar.h>
12045bc40b1SMatthew Dillon #include <bus/pci/pcireg.h>
12124a8d46aSMatthew Dillon
12224a8d46aSMatthew Dillon #include <net/bpf.h>
12324a8d46aSMatthew Dillon
12424a8d46aSMatthew Dillon #include <net/if.h>
12524a8d46aSMatthew Dillon #include <net/if_var.h>
12624a8d46aSMatthew Dillon #include <net/if_arp.h>
12724a8d46aSMatthew Dillon #include <net/if_dl.h>
12824a8d46aSMatthew Dillon #include <net/if_media.h>
12924a8d46aSMatthew Dillon #include <net/if_types.h>
13024a8d46aSMatthew Dillon
13124a8d46aSMatthew Dillon #include <netinet/in.h>
13224a8d46aSMatthew Dillon #include <netinet/in_systm.h>
13324a8d46aSMatthew Dillon #include <netinet/if_ether.h>
13424a8d46aSMatthew Dillon #include <netinet/ip.h>
13524a8d46aSMatthew Dillon
13645bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_var.h>
13745bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_regdomain.h>
13845bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_ratectl.h>
13945bc40b1SMatthew Dillon #include <netproto/802_11/ieee80211_radiotap.h>
14024a8d46aSMatthew Dillon
14145bc40b1SMatthew Dillon #include "if_iwmreg.h"
14245bc40b1SMatthew Dillon #include "if_iwmvar.h"
1433b7fc5aaSImre Vadász #include "if_iwm_config.h"
14445bc40b1SMatthew Dillon #include "if_iwm_debug.h"
14545bc40b1SMatthew Dillon #include "if_iwm_binding.h"
14645bc40b1SMatthew Dillon #include "if_iwm_util.h"
14745bc40b1SMatthew Dillon #include "if_iwm_pcie_trans.h"
14845bc40b1SMatthew Dillon
14924a8d46aSMatthew Dillon /*
15024a8d46aSMatthew Dillon * Send a command to the firmware. We try to implement the Linux
15124a8d46aSMatthew Dillon * driver interface for the routine.
15224a8d46aSMatthew Dillon * mostly from if_iwn (iwn_cmd()).
15324a8d46aSMatthew Dillon *
15424a8d46aSMatthew Dillon * For now, we always copy the first part and map the second one (if it exists).
15524a8d46aSMatthew Dillon */
15624a8d46aSMatthew Dillon int
iwm_send_cmd(struct iwm_softc * sc,struct iwm_host_cmd * hcmd)15724a8d46aSMatthew Dillon iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
15824a8d46aSMatthew Dillon {
159*6acbba79SMatthew Dillon struct iwm_tx_ring *ring = &sc->txq[IWM_CMD_QUEUE];
16024a8d46aSMatthew Dillon struct iwm_tfd *desc;
161edfc8a07SImre Vadász struct iwm_tx_data *txdata = NULL;
162edfc8a07SImre Vadász struct iwm_device_cmd *cmd;
16365b2e1baSImre Vadász struct mbuf *m;
16465b2e1baSImre Vadász bus_dma_segment_t seg;
16524a8d46aSMatthew Dillon bus_addr_t paddr;
16624a8d46aSMatthew Dillon uint32_t addr_lo;
16724a8d46aSMatthew Dillon int error = 0, i, paylen, off;
16824a8d46aSMatthew Dillon int code;
16924a8d46aSMatthew Dillon int async, wantresp;
170edfc8a07SImre Vadász int group_id;
17165b2e1baSImre Vadász int nsegs;
172edfc8a07SImre Vadász size_t hdrlen, datasz;
173edfc8a07SImre Vadász uint8_t *data;
17424a8d46aSMatthew Dillon
17524a8d46aSMatthew Dillon code = hcmd->id;
17624a8d46aSMatthew Dillon async = hcmd->flags & IWM_CMD_ASYNC;
17724a8d46aSMatthew Dillon wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
17845bc40b1SMatthew Dillon data = NULL;
17924a8d46aSMatthew Dillon
18024a8d46aSMatthew Dillon for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
18124a8d46aSMatthew Dillon paylen += hcmd->len[i];
18224a8d46aSMatthew Dillon }
18324a8d46aSMatthew Dillon
18424a8d46aSMatthew Dillon /* if the command wants an answer, busy sc_cmd_resp */
18524a8d46aSMatthew Dillon if (wantresp) {
18624a8d46aSMatthew Dillon KASSERT(!async, ("invalid async parameter"));
187*6acbba79SMatthew Dillon while (sc->sc_wantresp != -1)
188303bb3adSImre Vadász lksleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0);
18924a8d46aSMatthew Dillon sc->sc_wantresp = ring->qid << 16 | ring->cur;
19024a8d46aSMatthew Dillon IWM_DPRINTF(sc, IWM_DEBUG_CMD,
19124a8d46aSMatthew Dillon "wantresp is %x\n", sc->sc_wantresp);
19224a8d46aSMatthew Dillon }
19324a8d46aSMatthew Dillon
19424a8d46aSMatthew Dillon /*
19524a8d46aSMatthew Dillon * Is the hardware still available? (after e.g. above wait).
19624a8d46aSMatthew Dillon */
19724a8d46aSMatthew Dillon if (sc->sc_flags & IWM_FLAG_STOPPED) {
19824a8d46aSMatthew Dillon error = ENXIO;
19924a8d46aSMatthew Dillon goto out;
20024a8d46aSMatthew Dillon }
20124a8d46aSMatthew Dillon
20224a8d46aSMatthew Dillon desc = &ring->desc[ring->cur];
203edfc8a07SImre Vadász txdata = &ring->data[ring->cur];
20424a8d46aSMatthew Dillon
205edfc8a07SImre Vadász group_id = iwm_cmd_groupid(code);
206edfc8a07SImre Vadász if (group_id != 0) {
207edfc8a07SImre Vadász hdrlen = sizeof(cmd->hdr_wide);
208edfc8a07SImre Vadász datasz = sizeof(cmd->data_wide);
209edfc8a07SImre Vadász } else {
210edfc8a07SImre Vadász hdrlen = sizeof(cmd->hdr);
211edfc8a07SImre Vadász datasz = sizeof(cmd->data);
212edfc8a07SImre Vadász }
213edfc8a07SImre Vadász
214edfc8a07SImre Vadász if (paylen > datasz) {
21524a8d46aSMatthew Dillon IWM_DPRINTF(sc, IWM_DEBUG_CMD,
21624a8d46aSMatthew Dillon "large command paylen=%u len0=%u\n",
21724a8d46aSMatthew Dillon paylen, hcmd->len[0]);
21824a8d46aSMatthew Dillon /* Command is too large */
219edfc8a07SImre Vadász size_t totlen = hdrlen + paylen;
220b70c1eaaSImre Vadász if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) {
221b70c1eaaSImre Vadász device_printf(sc->sc_dev,
222b70c1eaaSImre Vadász "firmware command too long (%zd bytes)\n",
223edfc8a07SImre Vadász totlen);
22424a8d46aSMatthew Dillon error = EINVAL;
22524a8d46aSMatthew Dillon goto out;
22624a8d46aSMatthew Dillon }
22765b2e1baSImre Vadász m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, IWM_RBUF_SIZE);
22865b2e1baSImre Vadász if (m == NULL) {
22965b2e1baSImre Vadász error = ENOBUFS;
23024a8d46aSMatthew Dillon goto out;
23165b2e1baSImre Vadász }
23265b2e1baSImre Vadász
23365b2e1baSImre Vadász m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
23465b2e1baSImre Vadász #if defined(__DragonFly__)
23565b2e1baSImre Vadász error = bus_dmamap_load_mbuf_segment(ring->data_dmat,
23665b2e1baSImre Vadász txdata->map, m, &seg, 1, &nsegs, BUS_DMA_NOWAIT);
23765b2e1baSImre Vadász #else
23865b2e1baSImre Vadász error = bus_dmamap_load_mbuf_sg(ring->data_dmat,
23965b2e1baSImre Vadász txdata->map, m, &seg, &nsegs, BUS_DMA_NOWAIT);
24065b2e1baSImre Vadász #endif
24165b2e1baSImre Vadász if (error != 0) {
24265b2e1baSImre Vadász device_printf(sc->sc_dev,
24365b2e1baSImre Vadász "%s: can't map mbuf, error %d\n", __func__, error);
24465b2e1baSImre Vadász m_freem(m);
24524a8d46aSMatthew Dillon goto out;
24665b2e1baSImre Vadász }
24765b2e1baSImre Vadász txdata->m = m; /* mbuf will be freed in iwm_cmd_done() */
24865b2e1baSImre Vadász cmd = mtod(m, struct iwm_device_cmd *);
24965b2e1baSImre Vadász paddr = seg.ds_addr;
25024a8d46aSMatthew Dillon } else {
25124a8d46aSMatthew Dillon cmd = &ring->cmd[ring->cur];
252edfc8a07SImre Vadász paddr = txdata->cmd_paddr;
25324a8d46aSMatthew Dillon }
25424a8d46aSMatthew Dillon
255edfc8a07SImre Vadász if (group_id != 0) {
256edfc8a07SImre Vadász cmd->hdr_wide.opcode = iwm_cmd_opcode(code);
257edfc8a07SImre Vadász cmd->hdr_wide.group_id = group_id;
258edfc8a07SImre Vadász cmd->hdr_wide.qid = ring->qid;
259edfc8a07SImre Vadász cmd->hdr_wide.idx = ring->cur;
260edfc8a07SImre Vadász cmd->hdr_wide.length = htole16(paylen);
261edfc8a07SImre Vadász cmd->hdr_wide.version = iwm_cmd_version(code);
262edfc8a07SImre Vadász data = cmd->data_wide;
263edfc8a07SImre Vadász } else {
26445078908SImre Vadász cmd->hdr.code = iwm_cmd_opcode(code);
26524a8d46aSMatthew Dillon cmd->hdr.flags = 0;
26624a8d46aSMatthew Dillon cmd->hdr.qid = ring->qid;
26724a8d46aSMatthew Dillon cmd->hdr.idx = ring->cur;
268edfc8a07SImre Vadász data = cmd->data;
269edfc8a07SImre Vadász }
27024a8d46aSMatthew Dillon
27124a8d46aSMatthew Dillon for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
27224a8d46aSMatthew Dillon if (hcmd->len[i] == 0)
27324a8d46aSMatthew Dillon continue;
274edfc8a07SImre Vadász memcpy(data + off, hcmd->data[i], hcmd->len[i]);
27524a8d46aSMatthew Dillon off += hcmd->len[i];
27624a8d46aSMatthew Dillon }
27724a8d46aSMatthew Dillon KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
27824a8d46aSMatthew Dillon
27924a8d46aSMatthew Dillon /* lo field is not aligned */
28024a8d46aSMatthew Dillon addr_lo = htole32((uint32_t)paddr);
28124a8d46aSMatthew Dillon memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
28224a8d46aSMatthew Dillon desc->tbs[0].hi_n_len = htole16(iwm_get_dma_hi_addr(paddr)
283edfc8a07SImre Vadász | ((hdrlen + paylen) << 4));
28424a8d46aSMatthew Dillon desc->num_tbs = 1;
28524a8d46aSMatthew Dillon
28624a8d46aSMatthew Dillon IWM_DPRINTF(sc, IWM_DEBUG_CMD,
287edfc8a07SImre Vadász "iwm_send_cmd 0x%x size=%lu %s\n",
28824a8d46aSMatthew Dillon code,
289edfc8a07SImre Vadász (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen),
29024a8d46aSMatthew Dillon async ? " (async)" : "");
29124a8d46aSMatthew Dillon
292edfc8a07SImre Vadász if (paylen > datasz) {
293edfc8a07SImre Vadász bus_dmamap_sync(ring->data_dmat, txdata->map,
29424a8d46aSMatthew Dillon BUS_DMASYNC_PREWRITE);
29524a8d46aSMatthew Dillon } else {
29624a8d46aSMatthew Dillon bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
29724a8d46aSMatthew Dillon BUS_DMASYNC_PREWRITE);
29824a8d46aSMatthew Dillon }
29924a8d46aSMatthew Dillon bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
30024a8d46aSMatthew Dillon BUS_DMASYNC_PREWRITE);
30124a8d46aSMatthew Dillon
302ce43f57fSImre Vadász error = iwm_pcie_set_cmd_in_flight(sc);
303ce43f57fSImre Vadász if (error)
30424a8d46aSMatthew Dillon goto out;
305ce43f57fSImre Vadász ring->queued++;
30624a8d46aSMatthew Dillon
30724a8d46aSMatthew Dillon #if 0
30824a8d46aSMatthew Dillon iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
30924a8d46aSMatthew Dillon #endif
31024a8d46aSMatthew Dillon IWM_DPRINTF(sc, IWM_DEBUG_CMD,
31124a8d46aSMatthew Dillon "sending command 0x%x qid %d, idx %d\n",
31224a8d46aSMatthew Dillon code, ring->qid, ring->cur);
31324a8d46aSMatthew Dillon
31424a8d46aSMatthew Dillon /* Kick command ring. */
31524a8d46aSMatthew Dillon ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
31624a8d46aSMatthew Dillon IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
31724a8d46aSMatthew Dillon
31824a8d46aSMatthew Dillon if (!async) {
31924a8d46aSMatthew Dillon /* m..m-mmyy-mmyyyy-mym-ym m-my generation */
32024a8d46aSMatthew Dillon int generation = sc->sc_generation;
321303bb3adSImre Vadász error = lksleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz);
32224a8d46aSMatthew Dillon if (error == 0) {
32324a8d46aSMatthew Dillon /* if hardware is no longer up, return error */
32424a8d46aSMatthew Dillon if (generation != sc->sc_generation) {
32524a8d46aSMatthew Dillon error = ENXIO;
32624a8d46aSMatthew Dillon } else {
32724a8d46aSMatthew Dillon hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
32824a8d46aSMatthew Dillon }
32924a8d46aSMatthew Dillon }
33024a8d46aSMatthew Dillon }
33124a8d46aSMatthew Dillon out:
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
339*6acbba79SMatthew Dillon /* iwlwifi: mvm/utils.c */
34024a8d46aSMatthew Dillon int
iwm_send_cmd_pdu(struct iwm_softc * sc,uint32_t id,uint32_t flags,uint16_t len,const void * data)341*6acbba79SMatthew Dillon iwm_send_cmd_pdu(struct iwm_softc *sc, uint32_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
354*6acbba79SMatthew Dillon /* iwlwifi: mvm/utils.c */
35524a8d46aSMatthew Dillon int
iwm_send_cmd_status(struct iwm_softc * sc,struct iwm_host_cmd * cmd,uint32_t * status)356*6acbba79SMatthew Dillon iwm_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
395*6acbba79SMatthew Dillon /* iwlwifi/mvm/utils.c */
39624a8d46aSMatthew Dillon int
iwm_send_cmd_pdu_status(struct iwm_softc * sc,uint32_t id,uint16_t len,const void * data,uint32_t * status)397*6acbba79SMatthew Dillon iwm_send_cmd_pdu_status(struct iwm_softc *sc, uint32_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
406*6acbba79SMatthew Dillon return iwm_send_cmd_status(sc, &cmd, status);
40724a8d46aSMatthew Dillon }
40824a8d46aSMatthew Dillon
40924a8d46aSMatthew Dillon void
iwm_free_resp(struct iwm_softc * sc,struct iwm_host_cmd * hcmd)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 }
41883a1185eSImre Vadász
41983a1185eSImre Vadász #if !defined(__DragonFly__)
420*6acbba79SMatthew Dillon
42183a1185eSImre Vadász static void
iwm_dma_map_addr(void * arg,bus_dma_segment_t * segs,int nsegs,int error)42283a1185eSImre Vadász iwm_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
42383a1185eSImre Vadász {
42483a1185eSImre Vadász if (error != 0)
42583a1185eSImre Vadász return;
42683a1185eSImre Vadász KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs));
42783a1185eSImre Vadász *(bus_addr_t *)arg = segs[0].ds_addr;
42883a1185eSImre Vadász }
429*6acbba79SMatthew Dillon
43083a1185eSImre Vadász #endif
43183a1185eSImre Vadász
43283a1185eSImre Vadász int
iwm_dma_contig_alloc(bus_dma_tag_t tag,struct iwm_dma_info * dma,bus_size_t size,bus_size_t alignment)43383a1185eSImre Vadász iwm_dma_contig_alloc(bus_dma_tag_t tag, struct iwm_dma_info *dma,
43483a1185eSImre Vadász bus_size_t size, bus_size_t alignment)
43583a1185eSImre Vadász {
43683a1185eSImre Vadász int error;
43783a1185eSImre Vadász
43883a1185eSImre Vadász dma->tag = NULL;
43983a1185eSImre Vadász dma->map = NULL;
44083a1185eSImre Vadász dma->size = size;
44183a1185eSImre Vadász dma->vaddr = NULL;
44283a1185eSImre Vadász
44383a1185eSImre Vadász #if defined(__DragonFly__)
44483a1185eSImre Vadász bus_dmamem_t dmem;
44583a1185eSImre Vadász error = bus_dmamem_coherent(tag, alignment, 0,
44683a1185eSImre Vadász BUS_SPACE_MAXADDR_32BIT,
44783a1185eSImre Vadász BUS_SPACE_MAXADDR,
44883a1185eSImre Vadász size, BUS_DMA_NOWAIT, &dmem);
44983a1185eSImre Vadász if (error != 0)
45083a1185eSImre Vadász goto fail;
45183a1185eSImre Vadász dma->tag = dmem.dmem_tag;
45283a1185eSImre Vadász dma->map = dmem.dmem_map;
45383a1185eSImre Vadász dma->vaddr = dmem.dmem_addr;
45483a1185eSImre Vadász dma->paddr = dmem.dmem_busaddr;
45583a1185eSImre Vadász #else
45683a1185eSImre Vadász error = bus_dma_tag_create(tag, alignment,
45783a1185eSImre Vadász 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size,
45883a1185eSImre Vadász 1, size, 0, NULL, NULL, &dma->tag);
45983a1185eSImre Vadász if (error != 0)
46083a1185eSImre Vadász goto fail;
46183a1185eSImre Vadász
46283a1185eSImre Vadász error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr,
46383a1185eSImre Vadász BUS_DMA_NOWAIT | BUS_DMA_ZERO | BUS_DMA_COHERENT, &dma->map);
46483a1185eSImre Vadász if (error != 0)
46583a1185eSImre Vadász goto fail;
46683a1185eSImre Vadász
46783a1185eSImre Vadász error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, size,
46883a1185eSImre Vadász iwm_dma_map_addr, &dma->paddr, BUS_DMA_NOWAIT);
46983a1185eSImre Vadász if (error != 0) {
47083a1185eSImre Vadász bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
47183a1185eSImre Vadász dma->vaddr = NULL;
47283a1185eSImre Vadász goto fail;
47383a1185eSImre Vadász }
47483a1185eSImre Vadász #endif
47583a1185eSImre Vadász
47683a1185eSImre Vadász bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE);
47783a1185eSImre Vadász
47883a1185eSImre Vadász return 0;
47983a1185eSImre Vadász
48083a1185eSImre Vadász fail:
48183a1185eSImre Vadász iwm_dma_contig_free(dma);
48283a1185eSImre Vadász
48383a1185eSImre Vadász return error;
48483a1185eSImre Vadász }
48583a1185eSImre Vadász
48683a1185eSImre Vadász void
iwm_dma_contig_free(struct iwm_dma_info * dma)48783a1185eSImre Vadász iwm_dma_contig_free(struct iwm_dma_info *dma)
48883a1185eSImre Vadász {
48983a1185eSImre Vadász if (dma->vaddr != NULL) {
49083a1185eSImre Vadász bus_dmamap_sync(dma->tag, dma->map,
49183a1185eSImre Vadász BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
49283a1185eSImre Vadász bus_dmamap_unload(dma->tag, dma->map);
49383a1185eSImre Vadász bus_dmamem_free(dma->tag, dma->vaddr, dma->map);
49483a1185eSImre Vadász dma->vaddr = NULL;
49583a1185eSImre Vadász }
49683a1185eSImre Vadász if (dma->tag != NULL) {
49783a1185eSImre Vadász bus_dma_tag_destroy(dma->tag);
49883a1185eSImre Vadász dma->tag = NULL;
49983a1185eSImre Vadász }
50083a1185eSImre Vadász }
5013b7fc5aaSImre Vadász
5028a5dd778SImre Vadász /**
503*6acbba79SMatthew Dillon * iwm_send_lq_cmd() - Send link quality command
5048a5dd778SImre Vadász * @init: This command is sent as part of station initialization right
5058a5dd778SImre Vadász * after station has been added.
5068a5dd778SImre Vadász *
5078a5dd778SImre Vadász * The link quality command is sent as the last step of station creation.
5088a5dd778SImre Vadász * This is the special case in which init is set and we call a callback in
5098a5dd778SImre Vadász * this case to clear the state indicating that station creation is in
5108a5dd778SImre Vadász * progress.
5118a5dd778SImre Vadász */
5128a5dd778SImre Vadász int
iwm_send_lq_cmd(struct iwm_softc * sc,struct iwm_lq_cmd * lq,boolean_t init)513*6acbba79SMatthew Dillon iwm_send_lq_cmd(struct iwm_softc *sc, struct iwm_lq_cmd *lq, boolean_t init)
5148a5dd778SImre Vadász {
5158a5dd778SImre Vadász struct iwm_host_cmd cmd = {
5168a5dd778SImre Vadász .id = IWM_LQ_CMD,
5178a5dd778SImre Vadász .len = { sizeof(struct iwm_lq_cmd), },
5188a5dd778SImre Vadász .flags = init ? 0 : IWM_CMD_ASYNC,
5198a5dd778SImre Vadász .data = { lq, },
5208a5dd778SImre Vadász };
5218a5dd778SImre Vadász
522*6acbba79SMatthew Dillon if (lq->sta_id == IWM_STATION_COUNT)
5238a5dd778SImre Vadász return EINVAL;
5248a5dd778SImre Vadász
5258a5dd778SImre Vadász return iwm_send_cmd(sc, &cmd);
5268a5dd778SImre Vadász }
5278a5dd778SImre Vadász
5283b7fc5aaSImre Vadász boolean_t
iwm_rx_diversity_allowed(struct iwm_softc * sc)529*6acbba79SMatthew Dillon iwm_rx_diversity_allowed(struct iwm_softc *sc)
5303b7fc5aaSImre Vadász {
531*6acbba79SMatthew Dillon if (num_of_ant(iwm_get_valid_rx_ant(sc)) == 1)
5323b7fc5aaSImre Vadász return FALSE;
5333b7fc5aaSImre Vadász
5343b7fc5aaSImre Vadász /*
5353b7fc5aaSImre Vadász * XXX Also return FALSE when SMPS (Spatial Multiplexing Powersave)
5363b7fc5aaSImre Vadász * is used on any vap (in the future).
5373b7fc5aaSImre Vadász */
5383b7fc5aaSImre Vadász
5393b7fc5aaSImre Vadász return TRUE;
5403b7fc5aaSImre Vadász }
541