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