xref: /freebsd/sys/dev/iwm/if_iwm_fw.c (revision 076ad2f836d5f49dc1375f1677335a48fe0d4b82)
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/cdefs.h>
74 __FBSDID("$FreeBSD$");
75 
76 #include "opt_wlan.h"
77 
78 #include <sys/param.h>
79 #include <sys/bus.h>
80 #include <sys/conf.h>
81 #include <sys/endian.h>
82 #include <sys/firmware.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
85 #include <sys/mbuf.h>
86 #include <sys/mutex.h>
87 #include <sys/module.h>
88 #include <sys/proc.h>
89 #include <sys/rman.h>
90 #include <sys/socket.h>
91 #include <sys/sockio.h>
92 #include <sys/sysctl.h>
93 #include <sys/linker.h>
94 
95 #include <machine/bus.h>
96 #include <machine/endian.h>
97 #include <machine/resource.h>
98 
99 #include <dev/pci/pcivar.h>
100 #include <dev/pci/pcireg.h>
101 
102 #include <net/bpf.h>
103 
104 #include <net/if.h>
105 #include <net/if_var.h>
106 #include <net/if_arp.h>
107 #include <net/if_dl.h>
108 #include <net/if_media.h>
109 #include <net/if_types.h>
110 
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/if_ether.h>
114 #include <netinet/ip.h>
115 
116 #include <net80211/ieee80211_var.h>
117 #include <net80211/ieee80211_regdomain.h>
118 #include <net80211/ieee80211_ratectl.h>
119 #include <net80211/ieee80211_radiotap.h>
120 
121 #include <dev/iwm/if_iwmreg.h>
122 #include <dev/iwm/if_iwmvar.h>
123 #include <dev/iwm/if_iwm_debug.h>
124 #include <dev/iwm/if_iwm_util.h>
125 #include <dev/iwm/if_iwm_fw.h>
126 
127 void
128 iwm_free_fw_paging(struct iwm_softc *sc)
129 {
130 	int i;
131 
132 	if (sc->fw_paging_db[0].fw_paging_block.vaddr == NULL)
133 		return;
134 
135 	for (i = 0; i < IWM_NUM_OF_FW_PAGING_BLOCKS; i++) {
136 		iwm_dma_contig_free(&sc->fw_paging_db[i].fw_paging_block);
137 	}
138 
139 	memset(sc->fw_paging_db, 0, sizeof(sc->fw_paging_db));
140 }
141 
142 static int
143 iwm_fill_paging_mem(struct iwm_softc *sc, const struct iwm_fw_sects *image)
144 {
145 	int sec_idx, idx;
146 	uint32_t offset = 0;
147 
148 	/*
149 	 * find where is the paging image start point:
150 	 * if CPU2 exist and it's in paging format, then the image looks like:
151 	 * CPU1 sections (2 or more)
152 	 * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
153 	 * CPU2 sections (not paged)
154 	 * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
155 	 * non paged to CPU2 paging sec
156 	 * CPU2 paging CSS
157 	 * CPU2 paging image (including instruction and data)
158 	 */
159 	for (sec_idx = 0; sec_idx < IWM_UCODE_SECTION_MAX; sec_idx++) {
160 		if (image->fw_sect[sec_idx].offset == IWM_PAGING_SEPARATOR_SECTION) {
161 			sec_idx++;
162 			break;
163 		}
164 	}
165 
166 	/*
167 	 * If paging is enabled there should be at least 2 more sections left
168 	 * (one for CSS and one for Paging data)
169 	 */
170 	if (sec_idx >= nitems(image->fw_sect) - 1) {
171 		device_printf(sc->sc_dev,
172 		    "Paging: Missing CSS and/or paging sections\n");
173 		iwm_free_fw_paging(sc);
174 		return EINVAL;
175 	}
176 
177 	/* copy the CSS block to the dram */
178 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
179 		    "Paging: load paging CSS to FW, sec = %d\n",
180 		    sec_idx);
181 
182 	memcpy(sc->fw_paging_db[0].fw_paging_block.vaddr,
183 	       image->fw_sect[sec_idx].data,
184 	       sc->fw_paging_db[0].fw_paging_size);
185 
186 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
187 		    "Paging: copied %d CSS bytes to first block\n",
188 		    sc->fw_paging_db[0].fw_paging_size);
189 
190 	sec_idx++;
191 
192 	/*
193 	 * copy the paging blocks to the dram
194 	 * loop index start from 1 since that CSS block already copied to dram
195 	 * and CSS index is 0.
196 	 * loop stop at num_of_paging_blk since that last block is not full.
197 	 */
198 	for (idx = 1; idx < sc->num_of_paging_blk; idx++) {
199 		memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
200 		       (const char *)image->fw_sect[sec_idx].data + offset,
201 		       sc->fw_paging_db[idx].fw_paging_size);
202 
203 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
204 			    "Paging: copied %d paging bytes to block %d\n",
205 			    sc->fw_paging_db[idx].fw_paging_size,
206 			    idx);
207 
208 		offset += sc->fw_paging_db[idx].fw_paging_size;
209 	}
210 
211 	/* copy the last paging block */
212 	if (sc->num_of_pages_in_last_blk > 0) {
213 		memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
214 		       (const char *)image->fw_sect[sec_idx].data + offset,
215 		       IWM_FW_PAGING_SIZE * sc->num_of_pages_in_last_blk);
216 
217 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
218 			    "Paging: copied %d pages in the last block %d\n",
219 			    sc->num_of_pages_in_last_blk, idx);
220 	}
221 
222 	return 0;
223 }
224 
225 static int
226 iwm_alloc_fw_paging_mem(struct iwm_softc *sc, const struct iwm_fw_sects *image)
227 {
228 	int blk_idx = 0;
229 	int error, num_of_pages;
230 
231 	if (sc->fw_paging_db[0].fw_paging_block.vaddr != NULL) {
232 		int i;
233 		/* Device got reset, and we setup firmware paging again */
234 		for (i = 0; i < sc->num_of_paging_blk + 1; i++) {
235 			bus_dmamap_sync(sc->sc_dmat,
236 			    sc->fw_paging_db[i].fw_paging_block.map,
237 			    BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
238 		}
239 		return 0;
240 	}
241 
242 	/* ensure IWM_BLOCK_2_EXP_SIZE is power of 2 of IWM_PAGING_BLOCK_SIZE */
243         _Static_assert((1 << IWM_BLOCK_2_EXP_SIZE) == IWM_PAGING_BLOCK_SIZE,
244 	    "IWM_BLOCK_2_EXP_SIZE must be power of 2 of IWM_PAGING_BLOCK_SIZE");
245 
246 	num_of_pages = image->paging_mem_size / IWM_FW_PAGING_SIZE;
247 	sc->num_of_paging_blk = ((num_of_pages - 1) /
248 				    IWM_NUM_OF_PAGE_PER_GROUP) + 1;
249 
250 	sc->num_of_pages_in_last_blk =
251 		num_of_pages -
252 		IWM_NUM_OF_PAGE_PER_GROUP * (sc->num_of_paging_blk - 1);
253 
254 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
255 		    "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
256 		    sc->num_of_paging_blk,
257 		    sc->num_of_pages_in_last_blk);
258 
259 	/* allocate block of 4Kbytes for paging CSS */
260 	error = iwm_dma_contig_alloc(sc->sc_dmat,
261 	    &sc->fw_paging_db[blk_idx].fw_paging_block, IWM_FW_PAGING_SIZE,
262 	    4096);
263 	if (error) {
264 		/* free all the previous pages since we failed */
265 		iwm_free_fw_paging(sc);
266 		return ENOMEM;
267 	}
268 
269 	sc->fw_paging_db[blk_idx].fw_paging_size = IWM_FW_PAGING_SIZE;
270 
271 	IWM_DPRINTF(sc, IWM_DEBUG_FW,
272 		    "Paging: allocated 4K(CSS) bytes for firmware paging.\n");
273 
274 	/*
275 	 * allocate blocks in dram.
276 	 * since that CSS allocated in fw_paging_db[0] loop start from index 1
277 	 */
278 	for (blk_idx = 1; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
279 		/* allocate block of IWM_PAGING_BLOCK_SIZE (32K) */
280 		/* XXX Use iwm_dma_contig_alloc for allocating */
281 		error = iwm_dma_contig_alloc(sc->sc_dmat,
282 		     &sc->fw_paging_db[blk_idx].fw_paging_block,
283 		    IWM_PAGING_BLOCK_SIZE, 4096);
284 		if (error) {
285 			/* free all the previous pages since we failed */
286 			iwm_free_fw_paging(sc);
287 			return ENOMEM;
288 		}
289 
290 		sc->fw_paging_db[blk_idx].fw_paging_size = IWM_PAGING_BLOCK_SIZE;
291 
292 		IWM_DPRINTF(sc, IWM_DEBUG_FW,
293 			    "Paging: allocated 32K bytes for firmware paging.\n");
294 	}
295 
296 	return 0;
297 }
298 
299 int
300 iwm_save_fw_paging(struct iwm_softc *sc, const struct iwm_fw_sects *fw)
301 {
302 	int ret;
303 
304 	ret = iwm_alloc_fw_paging_mem(sc, fw);
305 	if (ret)
306 		return ret;
307 
308 	return iwm_fill_paging_mem(sc, fw);
309 }
310 
311 /* send paging cmd to FW in case CPU2 has paging image */
312 int
313 iwm_send_paging_cmd(struct iwm_softc *sc, const struct iwm_fw_sects *fw)
314 {
315 	int blk_idx;
316 	uint32_t dev_phy_addr;
317 	struct iwm_fw_paging_cmd fw_paging_cmd = {
318 		.flags =
319 			htole32(IWM_PAGING_CMD_IS_SECURED |
320 				IWM_PAGING_CMD_IS_ENABLED |
321 				(sc->num_of_pages_in_last_blk <<
322 				IWM_PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
323 		.block_size = htole32(IWM_BLOCK_2_EXP_SIZE),
324 		.block_num = htole32(sc->num_of_paging_blk),
325 	};
326 
327 	/* loop for for all paging blocks + CSS block */
328 	for (blk_idx = 0; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
329 		dev_phy_addr = htole32(
330 		    sc->fw_paging_db[blk_idx].fw_paging_block.paddr >>
331 		    IWM_PAGE_2_EXP_SIZE);
332 		fw_paging_cmd.device_phy_addr[blk_idx] = dev_phy_addr;
333 		bus_dmamap_sync(sc->sc_dmat,
334 		    sc->fw_paging_db[blk_idx].fw_paging_block.map,
335 		    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
336 	}
337 
338 	return iwm_mvm_send_cmd_pdu(sc, iwm_cmd_id(IWM_FW_PAGING_BLOCK_CMD,
339 						   IWM_ALWAYS_LONG_GROUP, 0),
340 				    0, sizeof(fw_paging_cmd), &fw_paging_cmd);
341 }
342