if_iwm - Compare paylen to datasz instead of sizeof(cmd->data).
[dragonfly.git] / sys / dev / netif / iwm / if_iwm_util.c
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$");
107
108 #include <sys/param.h>
109 #include <sys/bus.h>
110 #include <sys/endian.h>
111 #include <sys/firmware.h>
112 #include <sys/kernel.h>
113 #include <sys/malloc.h>
114 #include <sys/mbuf.h>
115 #include <sys/mutex.h>
116 #include <sys/module.h>
117 #include <sys/proc.h>
118 #include <sys/rman.h>
119 #include <sys/socket.h>
120 #include <sys/sockio.h>
121 #include <sys/sysctl.h>
122 #include <sys/linker.h>
123
124 #include <machine/endian.h>
125
126 #include <bus/pci/pcivar.h>
127 #include <bus/pci/pcireg.h>
128
129 #include <net/bpf.h>
130
131 #include <net/if.h>
132 #include <net/if_var.h>
133 #include <net/if_arp.h>
134 #include <net/if_dl.h>
135 #include <net/if_media.h>
136 #include <net/if_types.h>
137
138 #include <netinet/in.h>
139 #include <netinet/in_systm.h>
140 #include <netinet/if_ether.h>
141 #include <netinet/ip.h>
142
143 #include <netproto/802_11/ieee80211_var.h>
144 #include <netproto/802_11/ieee80211_regdomain.h>
145 #include <netproto/802_11/ieee80211_ratectl.h>
146 #include <netproto/802_11/ieee80211_radiotap.h>
147
148 #include "if_iwmreg.h"
149 #include "if_iwmvar.h"
150 #include "if_iwm_debug.h"
151 #include "if_iwm_binding.h"
152 #include "if_iwm_util.h"
153 #include "if_iwm_pcie_trans.h"
154
155 #if defined(__DragonFly__)
156 int iwmsleep(void *chan, struct lock *lk, int flags, const char *wmesg, int to);
157 #endif
158
159 static void
160 iwm_dma_map_mem(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
161 {
162         if (error != 0)
163                 return;
164         KASSERT(nsegs <= 2, ("too many DMA segments, %d should be <= 2",
165             nsegs));
166         if (nsegs > 1)
167                 KASSERT(segs[1].ds_addr == segs[0].ds_addr + segs[0].ds_len,
168                     ("fragmented DMA memory"));
169         *(bus_addr_t *)arg = segs[0].ds_addr;
170 }
171
172 /*
173  * Send a command to the firmware.  We try to implement the Linux
174  * driver interface for the routine.
175  * mostly from if_iwn (iwn_cmd()).
176  *
177  * For now, we always copy the first part and map the second one (if it exists).
178  */
179 int
180 iwm_send_cmd(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
181 {
182         struct iwm_tx_ring *ring = &sc->txq[IWM_MVM_CMD_QUEUE];
183         struct iwm_tfd *desc;
184         struct iwm_tx_data *txdata = NULL;
185         struct iwm_device_cmd *cmd;
186         bus_addr_t paddr;
187         uint32_t addr_lo;
188         int error = 0, i, paylen, off;
189         int code;
190         int async, wantresp;
191         int group_id;
192         size_t hdrlen, datasz;
193         uint8_t *data;
194
195         code = hcmd->id;
196         async = hcmd->flags & IWM_CMD_ASYNC;
197         wantresp = hcmd->flags & IWM_CMD_WANT_SKB;
198         data = NULL;
199
200         for (i = 0, paylen = 0; i < nitems(hcmd->len); i++) {
201                 paylen += hcmd->len[i];
202         }
203
204         /* if the command wants an answer, busy sc_cmd_resp */
205         if (wantresp) {
206                 KASSERT(!async, ("invalid async parameter"));
207                 while (sc->sc_wantresp != -1) {
208 #if defined(__DragonFly__)
209                         iwmsleep(&sc->sc_wantresp, &sc->sc_lk, 0, "iwmcmdsl", 0);
210 #else
211                         msleep(&sc->sc_wantresp, &sc->sc_mtx, 0, "iwmcmdsl", 0);
212 #endif
213                 }
214                 sc->sc_wantresp = ring->qid << 16 | ring->cur;
215                 IWM_DPRINTF(sc, IWM_DEBUG_CMD,
216                     "wantresp is %x\n", sc->sc_wantresp);
217         }
218
219         /*
220          * Is the hardware still available?  (after e.g. above wait).
221          */
222         if (sc->sc_flags & IWM_FLAG_STOPPED) {
223                 error = ENXIO;
224                 goto out;
225         }
226
227         desc = &ring->desc[ring->cur];
228         txdata = &ring->data[ring->cur];
229
230         group_id = iwm_cmd_groupid(code);
231         if (group_id != 0) {
232                 hdrlen = sizeof(cmd->hdr_wide);
233                 datasz = sizeof(cmd->data_wide);
234         } else {
235                 hdrlen = sizeof(cmd->hdr);
236                 datasz = sizeof(cmd->data);
237         }
238
239         if (paylen > datasz) {
240                 IWM_DPRINTF(sc, IWM_DEBUG_CMD,
241                     "large command paylen=%u len0=%u\n",
242                         paylen, hcmd->len[0]);
243                 /* Command is too large */
244                 size_t totlen = hdrlen + paylen;
245                 if (paylen > IWM_MAX_CMD_PAYLOAD_SIZE) {
246                         device_printf(sc->sc_dev,
247                             "firmware command too long (%zd bytes)\n",
248                             totlen);
249                         error = EINVAL;
250                         goto out;
251                 }
252                 cmd = NULL;
253                 error = bus_dmamem_alloc(ring->data_dmat, (void **)&cmd,
254                     BUS_DMA_NOWAIT | BUS_DMA_COHERENT, &txdata->map);
255                 if (error != 0)
256                         goto out;
257                 KKASSERT(cmd != NULL);
258                 error = bus_dmamap_load(ring->data_dmat, txdata->map,
259                     cmd, totlen, iwm_dma_map_mem, &paddr, BUS_DMA_NOWAIT);
260                 if (error != 0)
261                         goto out;
262         } else {
263                 cmd = &ring->cmd[ring->cur];
264                 paddr = txdata->cmd_paddr;
265         }
266
267         if (group_id != 0) {
268                 cmd->hdr_wide.opcode = iwm_cmd_opcode(code);
269                 cmd->hdr_wide.group_id = group_id;
270                 cmd->hdr_wide.qid = ring->qid;
271                 cmd->hdr_wide.idx = ring->cur;
272                 cmd->hdr_wide.length = htole16(paylen);
273                 cmd->hdr_wide.version = iwm_cmd_version(code);
274                 data = cmd->data_wide;
275         } else {
276                 cmd->hdr.code = code;
277                 cmd->hdr.flags = 0;
278                 cmd->hdr.qid = ring->qid;
279                 cmd->hdr.idx = ring->cur;
280                 data = cmd->data;
281         }
282
283         for (i = 0, off = 0; i < nitems(hcmd->data); i++) {
284                 if (hcmd->len[i] == 0)
285                         continue;
286                 memcpy(data + off, hcmd->data[i], hcmd->len[i]);
287                 off += hcmd->len[i];
288         }
289         KASSERT(off == paylen, ("off %d != paylen %d", off, paylen));
290
291         /* lo field is not aligned */
292         addr_lo = htole32((uint32_t)paddr);
293         memcpy(&desc->tbs[0].lo, &addr_lo, sizeof(uint32_t));
294         desc->tbs[0].hi_n_len  = htole16(iwm_get_dma_hi_addr(paddr)
295             | ((hdrlen + paylen) << 4));
296         desc->num_tbs = 1;
297
298         IWM_DPRINTF(sc, IWM_DEBUG_CMD,
299             "iwm_send_cmd 0x%x size=%lu %s\n",
300             code,
301             (unsigned long) (hcmd->len[0] + hcmd->len[1] + hdrlen),
302             async ? " (async)" : "");
303
304         if (paylen > datasz) {
305                 bus_dmamap_sync(ring->data_dmat, txdata->map,
306                     BUS_DMASYNC_PREWRITE);
307         } else {
308                 bus_dmamap_sync(ring->cmd_dma.tag, ring->cmd_dma.map,
309                     BUS_DMASYNC_PREWRITE);
310         }
311         bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map,
312             BUS_DMASYNC_PREWRITE);
313
314         IWM_SETBITS(sc, IWM_CSR_GP_CNTRL,
315             IWM_CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
316         if (!iwm_poll_bit(sc, IWM_CSR_GP_CNTRL,
317             IWM_CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
318             (IWM_CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
319              IWM_CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP), 15000)) {
320                 device_printf(sc->sc_dev,
321                     "%s: acquiring device failed\n", __func__);
322                 error = EBUSY;
323                 goto out;
324         }
325
326 #if 0
327         iwm_update_sched(sc, ring->qid, ring->cur, 0, 0);
328 #endif
329         IWM_DPRINTF(sc, IWM_DEBUG_CMD,
330             "sending command 0x%x qid %d, idx %d\n",
331             code, ring->qid, ring->cur);
332
333         /* Kick command ring. */
334         ring->cur = (ring->cur + 1) % IWM_TX_RING_COUNT;
335         IWM_WRITE(sc, IWM_HBUS_TARG_WRPTR, ring->qid << 8 | ring->cur);
336
337         if (!async) {
338                 /* m..m-mmyy-mmyyyy-mym-ym m-my generation */
339                 int generation = sc->sc_generation;
340 #if defined(__DragonFly__)
341                 error = iwmsleep(desc, &sc->sc_lk, PCATCH, "iwmcmd", hz);
342 #else
343                 error = msleep(desc, &sc->sc_mtx, PCATCH, "iwmcmd", hz);
344 #endif
345                 if (error == 0) {
346                         /* if hardware is no longer up, return error */
347                         if (generation != sc->sc_generation) {
348                                 error = ENXIO;
349                         } else {
350                                 hcmd->resp_pkt = (void *)sc->sc_cmd_resp;
351                         }
352                 }
353         }
354  out:
355         if (cmd && paylen > datasz && txdata != NULL)
356                 bus_dmamem_free(ring->data_dmat, cmd, txdata->map);
357         if (wantresp && error != 0) {
358                 iwm_free_resp(sc, hcmd);
359         }
360
361         return error;
362 }
363
364 int
365 iwm_mvm_send_cmd_pdu(struct iwm_softc *sc, uint8_t id,
366         uint32_t flags, uint16_t len, const void *data)
367 {
368         struct iwm_host_cmd cmd = {
369                 .id = id,
370                 .len = { len, },
371                 .data = { data, },
372                 .flags = flags,
373         };
374
375         return iwm_send_cmd(sc, &cmd);
376 }
377
378 int
379 iwm_mvm_send_cmd_status(struct iwm_softc *sc,
380         struct iwm_host_cmd *cmd, uint32_t *status)
381 {
382         struct iwm_rx_packet *pkt;
383         struct iwm_cmd_response *resp;
384         int error, resp_len;
385
386         KASSERT((cmd->flags & IWM_CMD_WANT_SKB) == 0,
387             ("invalid command"));
388         cmd->flags |= IWM_CMD_SYNC | IWM_CMD_WANT_SKB;
389
390         if ((error = iwm_send_cmd(sc, cmd)) != 0)
391                 return error;
392         pkt = cmd->resp_pkt;
393
394         /* Can happen if RFKILL is asserted */
395         if (!pkt) {
396                 error = 0;
397                 goto out_free_resp;
398         }
399
400         if (pkt->hdr.flags & IWM_CMD_FAILED_MSK) {
401                 error = EIO;
402                 goto out_free_resp;
403         }
404
405         resp_len = iwm_rx_packet_payload_len(pkt);
406         if (resp_len != sizeof(*resp)) {
407                 error = EIO;
408                 goto out_free_resp;
409         }
410
411         resp = (void *)pkt->data;
412         *status = le32toh(resp->status);
413  out_free_resp:
414         iwm_free_resp(sc, cmd);
415         return error;
416 }
417
418 int
419 iwm_mvm_send_cmd_pdu_status(struct iwm_softc *sc, uint8_t id,
420         uint16_t len, const void *data, uint32_t *status)
421 {
422         struct iwm_host_cmd cmd = {
423                 .id = id,
424                 .len = { len, },
425                 .data = { data, },
426         };
427
428         return iwm_mvm_send_cmd_status(sc, &cmd, status);
429 }
430
431 void
432 iwm_free_resp(struct iwm_softc *sc, struct iwm_host_cmd *hcmd)
433 {
434         KASSERT(sc->sc_wantresp != -1, ("already freed"));
435         KASSERT((hcmd->flags & (IWM_CMD_WANT_SKB|IWM_CMD_SYNC))
436             == (IWM_CMD_WANT_SKB|IWM_CMD_SYNC), ("invalid flags"));
437         sc->sc_wantresp = -1;
438         wakeup(&sc->sc_wantresp);
439 }
440
441 uint8_t
442 iwm_fw_valid_tx_ant(struct iwm_softc *sc)
443 {
444         uint8_t tx_ant;
445
446         tx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_TX_CHAIN)
447             >> IWM_FW_PHY_CFG_TX_CHAIN_POS);
448
449         if (sc->sc_nvm.valid_tx_ant)
450                 tx_ant &= sc->sc_nvm.valid_tx_ant;
451
452         return tx_ant;
453 }
454
455 uint8_t
456 iwm_fw_valid_rx_ant(struct iwm_softc *sc)
457 {
458         uint8_t rx_ant;
459
460         rx_ant = ((sc->sc_fw_phy_config & IWM_FW_PHY_CFG_RX_CHAIN)
461             >> IWM_FW_PHY_CFG_RX_CHAIN_POS);
462
463         if (sc->sc_nvm.valid_rx_ant)
464                 rx_ant &= sc->sc_nvm.valid_rx_ant;
465
466         return rx_ant;
467 }