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