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