2 * Based on BSD-licensed source modules in the Linux iwlwifi driver,
3 * which were used as the reference documentation for this implementation.
5 * Driver version we are currently based off of is
6 * Linux 4.7.3 (tag id d7f6728f57e3ecbb7ef34eb7d9f564d514775d75)
8 ***********************************************************************
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.
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
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.
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.
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,
33 * The full GNU General Public License is included in this distribution
34 * in the file called COPYING.
36 * Contact Information:
37 * Intel Linux Wireless <linuxwifi@intel.com>
38 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
42 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
43 * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
44 * All rights reserved.
46 * Redistribution and use in source and binary forms, with or without
47 * modification, are permitted provided that the following conditions
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
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.
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.
73 #include <sys/cdefs.h>
74 __FBSDID("$FreeBSD$");
78 #include <sys/param.h>
81 #include <sys/endian.h>
82 #include <sys/firmware.h>
83 #include <sys/kernel.h>
84 #include <sys/malloc.h>
86 #include <sys/mutex.h>
87 #include <sys/module.h>
90 #include <sys/socket.h>
91 #include <sys/sockio.h>
92 #include <sys/sysctl.h>
93 #include <sys/linker.h>
95 #include <machine/bus.h>
96 #include <machine/endian.h>
97 #include <machine/resource.h>
99 #include <dev/pci/pcivar.h>
100 #include <dev/pci/pcireg.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>
111 #include <netinet/in.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/if_ether.h>
114 #include <netinet/ip.h>
116 #include <net80211/ieee80211_var.h>
117 #include <net80211/ieee80211_regdomain.h>
118 #include <net80211/ieee80211_ratectl.h>
119 #include <net80211/ieee80211_radiotap.h>
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>
128 iwm_free_fw_paging(struct iwm_softc *sc)
132 if (sc->fw_paging_db[0].fw_paging_block.vaddr == NULL)
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);
139 memset(sc->fw_paging_db, 0, sizeof(sc->fw_paging_db));
143 iwm_fill_paging_mem(struct iwm_softc *sc, const struct iwm_fw_sects *image)
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
157 * CPU2 paging image (including instruction and data)
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) {
167 * If paging is enabled there should be at least 2 more sections left
168 * (one for CSS and one for Paging data)
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);
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",
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);
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);
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.
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);
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,
208 offset += sc->fw_paging_db[idx].fw_paging_size;
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);
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);
226 iwm_alloc_fw_paging_mem(struct iwm_softc *sc, const struct iwm_fw_sects *image)
229 int error, num_of_pages;
231 if (sc->fw_paging_db[0].fw_paging_block.vaddr != NULL) {
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);
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");
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;
250 sc->num_of_pages_in_last_blk =
252 IWM_NUM_OF_PAGE_PER_GROUP * (sc->num_of_paging_blk - 1);
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);
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,
264 /* free all the previous pages since we failed */
265 iwm_free_fw_paging(sc);
269 sc->fw_paging_db[blk_idx].fw_paging_size = IWM_FW_PAGING_SIZE;
271 IWM_DPRINTF(sc, IWM_DEBUG_FW,
272 "Paging: allocated 4K(CSS) bytes for firmware paging.\n");
275 * allocate blocks in dram.
276 * since that CSS allocated in fw_paging_db[0] loop start from index 1
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);
285 /* free all the previous pages since we failed */
286 iwm_free_fw_paging(sc);
290 sc->fw_paging_db[blk_idx].fw_paging_size = IWM_PAGING_BLOCK_SIZE;
292 IWM_DPRINTF(sc, IWM_DEBUG_FW,
293 "Paging: allocated 32K bytes for firmware paging.\n");
300 iwm_save_fw_paging(struct iwm_softc *sc, const struct iwm_fw_sects *fw)
304 ret = iwm_alloc_fw_paging_mem(sc, fw);
308 return iwm_fill_paging_mem(sc, fw);
311 /* send paging cmd to FW in case CPU2 has paging image */
313 iwm_send_paging_cmd(struct iwm_softc *sc, const struct iwm_fw_sects *fw)
316 uint32_t dev_phy_addr;
317 struct iwm_fw_paging_cmd fw_paging_cmd = {
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),
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);
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);