xref: /dragonfly/sys/dev/netif/iwm/if_iwm_fw.c (revision dbeb2594)
1 /*-
2  * Based on BSD-licensed source modules in the Linux iwlwifi driver,
3  * which were used as the reference documentation for this implementation.
4  *
5  * Driver version we are currently based off of is
6  * Linux 4.7.3 (tag id d7f6728f57e3ecbb7ef34eb7d9f564d514775d75)
7  *
8  ***********************************************************************
9  *
10  * This file is provided under a dual BSD/GPLv2 license.  When using or
11  * redistributing this file, you may do so under either license.
12  *
13  * GPL LICENSE SUMMARY
14  *
15  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
16  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
17  * Copyright(c) 2016 Intel Deutschland GmbH
18  *
19  * This program is free software; you can redistribute it and/or modify
20  * it under the terms of version 2 of the GNU General Public License as
21  * published by the Free Software Foundation.
22  *
23  * This program is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
26  * General Public License for more details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
31  * USA
32  *
33  * The full GNU General Public License is included in this distribution
34  * in the file called COPYING.
35  *
36  * Contact Information:
37  *  Intel Linux Wireless <linuxwifi@intel.com>
38  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
39  *
40  * BSD LICENSE
41  *
42  * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
43  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
44  * All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions
48  * are met:
49  *
50  *  * Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  *  * Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in
54  *    the documentation and/or other materials provided with the
55  *    distribution.
56  *  * Neither the name Intel Corporation nor the names of its
57  *    contributors may be used to endorse or promote products derived
58  *    from this software without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
61  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
62  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
63  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
64  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
65  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
66  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
67  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
68  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
69  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
70  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
71  */
72 
73 #include <sys/param.h>
74 #include <sys/bus.h>
75 #include <sys/endian.h>
76 #include <sys/firmware.h>
77 #include <sys/kernel.h>
78 #include <sys/malloc.h>
79 #include <sys/mbuf.h>
80 #include <sys/rman.h>
81 #include <sys/sysctl.h>
82 #include <sys/linker.h>
83 
84 #include <machine/endian.h>
85 
86 #include <bus/pci/pcivar.h>
87 #include <bus/pci/pcireg.h>
88 
89 #include <net/bpf.h>
90 
91 #include <net/if.h>
92 #include <net/if_var.h>
93 #include <net/if_arp.h>
94 #include <net/ethernet.h>
95 #include <net/if_dl.h>
96 #include <net/if_media.h>
97 #include <net/if_types.h>
98 #include <net/ifq_var.h>
99 
100 #include <netinet/in.h>
101 #include <netinet/in_systm.h>
102 #include <netinet/if_ether.h>
103 #include <netinet/ip.h>
104 
105 #include <netproto/802_11/ieee80211_var.h>
106 #include <netproto/802_11/ieee80211_regdomain.h>
107 #include <netproto/802_11/ieee80211_ratectl.h>
108 #include <netproto/802_11/ieee80211_radiotap.h>
109 
110 #include "if_iwmreg.h"
111 #include "if_iwmvar.h"
112 #include "if_iwm_debug.h"
113 #include "if_iwm_util.h"
114 #include "if_iwm_fw.h"
115 
116 void
117 iwm_free_fw_paging(struct iwm_softc *sc)
118 {
119 	int i;
120 
121 	if (sc->fw_paging_db[0].fw_paging_block.vaddr == NULL)
122 		return;
123 
124 	for (i = 0; i < IWM_NUM_OF_FW_PAGING_BLOCKS; i++) {
125 		iwm_dma_contig_free(&sc->fw_paging_db[i].fw_paging_block);
126 	}
127 
128 	memset(sc->fw_paging_db, 0, sizeof(sc->fw_paging_db));
129 }
130 
131 static int
132 iwm_fill_paging_mem(struct iwm_softc *sc, const struct iwm_fw_img *image)
133 {
134 	int sec_idx, idx;
135 	uint32_t offset = 0;
136 
137 	/*
138 	 * find where is the paging image start point:
139 	 * if CPU2 exist and it's in paging format, then the image looks like:
140 	 * CPU1 sections (2 or more)
141 	 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
142 	 * CPU2 sections (not paged)
143 	 * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
144 	 * non paged to CPU2 paging sec
145 	 * CPU2 paging CSS
146 	 * CPU2 paging image (including instruction and data)
147 	 */
148 	for (sec_idx = 0; sec_idx < IWM_UCODE_SECTION_MAX; sec_idx++) {
149 		if (image->sec[sec_idx].offset == IWM_PAGING_SEPARATOR_SECTION) {
150 			sec_idx++;
151 			break;
152 		}
153 	}
154 
155 	/*
156 	 * If paging is enabled there should be at least 2 more sections left
157 	 * (one for CSS and one for Paging data)
158 	 */
159 	if (sec_idx >= NELEM(image->sec) - 1) {
160 		device_printf(sc->sc_dev,
161 		    "Paging: Missing CSS and/or paging sections\n");
162 		iwm_free_fw_paging(sc);
163 		return EINVAL;
164 	}
165 
166 	/* copy the CSS block to the dram */
167 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
168 		    "Paging: load paging CSS to FW, sec = %d\n",
169 		    sec_idx);
170 
171 	memcpy(sc->fw_paging_db[0].fw_paging_block.vaddr,
172 	       image->sec[sec_idx].data,
173 	       sc->fw_paging_db[0].fw_paging_size);
174 
175 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
176 		    "Paging: copied %d CSS bytes to first block\n",
177 		    sc->fw_paging_db[0].fw_paging_size);
178 
179 	sec_idx++;
180 
181 	/*
182 	 * copy the paging blocks to the dram
183 	 * loop index start from 1 since that CSS block already copied to dram
184 	 * and CSS index is 0.
185 	 * loop stop at num_of_paging_blk since that last block is not full.
186 	 */
187 	for (idx = 1; idx < sc->num_of_paging_blk; idx++) {
188 		memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
189 		       (const char *)image->sec[sec_idx].data + offset,
190 		       sc->fw_paging_db[idx].fw_paging_size);
191 
192 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
193 			    "Paging: copied %d paging bytes to block %d\n",
194 			    sc->fw_paging_db[idx].fw_paging_size,
195 			    idx);
196 
197 		offset += sc->fw_paging_db[idx].fw_paging_size;
198 	}
199 
200 	/* copy the last paging block */
201 	if (sc->num_of_pages_in_last_blk > 0) {
202 		memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
203 		       (const char *)image->sec[sec_idx].data + offset,
204 		       IWM_FW_PAGING_SIZE * sc->num_of_pages_in_last_blk);
205 
206 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
207 			    "Paging: copied %d pages in the last block %d\n",
208 			    sc->num_of_pages_in_last_blk, idx);
209 	}
210 
211 	return 0;
212 }
213 
214 static int
215 iwm_alloc_fw_paging_mem(struct iwm_softc *sc, const struct iwm_fw_img *image)
216 {
217 	int blk_idx = 0;
218 	int error, num_of_pages;
219 
220 	if (sc->fw_paging_db[0].fw_paging_block.vaddr != NULL) {
221 		int i;
222 		/* Device got reset, and we setup firmware paging again */
223 		for (i = 0; i < sc->num_of_paging_blk + 1; i++) {
224 			bus_dmamap_sync(sc->sc_dmat,
225 			    sc->fw_paging_db[i].fw_paging_block.map,
226 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
227 		}
228 		return 0;
229 	}
230 
231 	/* ensure IWM_BLOCK_2_EXP_SIZE is power of 2 of IWM_PAGING_BLOCK_SIZE */
232         _Static_assert((1 << IWM_BLOCK_2_EXP_SIZE) == IWM_PAGING_BLOCK_SIZE,
233 	    "IWM_BLOCK_2_EXP_SIZE must be power of 2 of IWM_PAGING_BLOCK_SIZE");
234 
235 	num_of_pages = image->paging_mem_size / IWM_FW_PAGING_SIZE;
236 	sc->num_of_paging_blk = ((num_of_pages - 1) /
237 				    IWM_NUM_OF_PAGE_PER_GROUP) + 1;
238 
239 	sc->num_of_pages_in_last_blk =
240 		num_of_pages -
241 		IWM_NUM_OF_PAGE_PER_GROUP * (sc->num_of_paging_blk - 1);
242 
243 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
244 		    "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
245 		    sc->num_of_paging_blk,
246 		    sc->num_of_pages_in_last_blk);
247 
248 	/* allocate block of 4Kbytes for paging CSS */
249 	error = iwm_dma_contig_alloc(sc->sc_dmat,
250 	    &sc->fw_paging_db[blk_idx].fw_paging_block, IWM_FW_PAGING_SIZE,
251 	    4096);
252 	if (error) {
253 		/* free all the previous pages since we failed */
254 		iwm_free_fw_paging(sc);
255 		return ENOMEM;
256 	}
257 
258 	sc->fw_paging_db[blk_idx].fw_paging_size = IWM_FW_PAGING_SIZE;
259 
260 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
261 		    "Paging: allocated 4K(CSS) bytes for firmware paging.\n");
262 
263 	/*
264 	 * allocate blocks in dram.
265 	 * since that CSS allocated in fw_paging_db[0] loop start from index 1
266 	 */
267 	for (blk_idx = 1; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
268 		/* allocate block of IWM_PAGING_BLOCK_SIZE (32K) */
269 		/* XXX Use iwm_dma_contig_alloc for allocating */
270 		error = iwm_dma_contig_alloc(sc->sc_dmat,
271 		     &sc->fw_paging_db[blk_idx].fw_paging_block,
272 		    IWM_PAGING_BLOCK_SIZE, 4096);
273 		if (error) {
274 			/* free all the previous pages since we failed */
275 			iwm_free_fw_paging(sc);
276 			return ENOMEM;
277 		}
278 
279 		sc->fw_paging_db[blk_idx].fw_paging_size = IWM_PAGING_BLOCK_SIZE;
280 
281 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
282 			    "Paging: allocated 32K bytes for firmware paging.\n");
283 	}
284 
285 	return 0;
286 }
287 
288 int
289 iwm_save_fw_paging(struct iwm_softc *sc, const struct iwm_fw_img *fw)
290 {
291 	int ret;
292 
293 	ret = iwm_alloc_fw_paging_mem(sc, fw);
294 	if (ret)
295 		return ret;
296 
297 	return iwm_fill_paging_mem(sc, fw);
298 }
299 
300 /* send paging cmd to FW in case CPU2 has paging image */
301 int
302 iwm_send_paging_cmd(struct iwm_softc *sc, const struct iwm_fw_img *fw)
303 {
304 	int blk_idx;
305 	uint32_t dev_phy_addr;
306 	struct iwm_fw_paging_cmd fw_paging_cmd = {
307 		.flags =
308 			htole32(IWM_PAGING_CMD_IS_SECURED |
309 				IWM_PAGING_CMD_IS_ENABLED |
310 				(sc->num_of_pages_in_last_blk <<
311 				IWM_PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
312 		.block_size = htole32(IWM_BLOCK_2_EXP_SIZE),
313 		.block_num = htole32(sc->num_of_paging_blk),
314 	};
315 
316 	/* loop for for all paging blocks + CSS block */
317 	for (blk_idx = 0; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
318 		dev_phy_addr = htole32(
319 		    sc->fw_paging_db[blk_idx].fw_paging_block.paddr >>
320 		    IWM_PAGE_2_EXP_SIZE);
321 		fw_paging_cmd.device_phy_addr[blk_idx] = dev_phy_addr;
322 		bus_dmamap_sync(sc->sc_dmat,
323 		    sc->fw_paging_db[blk_idx].fw_paging_block.map,
324 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
325 	}
326 
327 	return iwm_mvm_send_cmd_pdu(sc, iwm_cmd_id(IWM_FW_PAGING_BLOCK_CMD,
328 						   IWM_ALWAYS_LONG_GROUP, 0),
329 				    0, sizeof(fw_paging_cmd), &fw_paging_cmd);
330 }
331