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