18a5b1999SImre Vadász /*-
28a5b1999SImre Vadász * Based on BSD-licensed source modules in the Linux iwlwifi driver,
38a5b1999SImre Vadász * which were used as the reference documentation for this implementation.
48a5b1999SImre Vadász *
58a5b1999SImre Vadász * Driver version we are currently based off of is
68a5b1999SImre Vadász * Linux 4.7.3 (tag id d7f6728f57e3ecbb7ef34eb7d9f564d514775d75)
78a5b1999SImre Vadász *
88a5b1999SImre Vadász ***********************************************************************
98a5b1999SImre Vadász *
108a5b1999SImre Vadász * This file is provided under a dual BSD/GPLv2 license. When using or
118a5b1999SImre Vadász * redistributing this file, you may do so under either license.
128a5b1999SImre Vadász *
138a5b1999SImre Vadász * GPL LICENSE SUMMARY
148a5b1999SImre Vadász *
158a5b1999SImre Vadász * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
168a5b1999SImre Vadász * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
178a5b1999SImre Vadász * Copyright(c) 2016 Intel Deutschland GmbH
188a5b1999SImre Vadász *
198a5b1999SImre Vadász * This program is free software; you can redistribute it and/or modify
208a5b1999SImre Vadász * it under the terms of version 2 of the GNU General Public License as
218a5b1999SImre Vadász * published by the Free Software Foundation.
228a5b1999SImre Vadász *
238a5b1999SImre Vadász * This program is distributed in the hope that it will be useful, but
248a5b1999SImre Vadász * WITHOUT ANY WARRANTY; without even the implied warranty of
258a5b1999SImre Vadász * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
268a5b1999SImre Vadász * General Public License for more details.
278a5b1999SImre Vadász *
288a5b1999SImre Vadász * You should have received a copy of the GNU General Public License
298a5b1999SImre Vadász * along with this program; if not, write to the Free Software
308a5b1999SImre Vadász * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
318a5b1999SImre Vadász * USA
328a5b1999SImre Vadász *
338a5b1999SImre Vadász * The full GNU General Public License is included in this distribution
348a5b1999SImre Vadász * in the file called COPYING.
358a5b1999SImre Vadász *
368a5b1999SImre Vadász * Contact Information:
378a5b1999SImre Vadász * Intel Linux Wireless <linuxwifi@intel.com>
388a5b1999SImre Vadász * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
398a5b1999SImre Vadász *
408a5b1999SImre Vadász * BSD LICENSE
418a5b1999SImre Vadász *
428a5b1999SImre Vadász * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
438a5b1999SImre Vadász * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
448a5b1999SImre Vadász * All rights reserved.
458a5b1999SImre Vadász *
468a5b1999SImre Vadász * Redistribution and use in source and binary forms, with or without
478a5b1999SImre Vadász * modification, are permitted provided that the following conditions
488a5b1999SImre Vadász * are met:
498a5b1999SImre Vadász *
508a5b1999SImre Vadász * * Redistributions of source code must retain the above copyright
518a5b1999SImre Vadász * notice, this list of conditions and the following disclaimer.
528a5b1999SImre Vadász * * Redistributions in binary form must reproduce the above copyright
538a5b1999SImre Vadász * notice, this list of conditions and the following disclaimer in
548a5b1999SImre Vadász * the documentation and/or other materials provided with the
558a5b1999SImre Vadász * distribution.
568a5b1999SImre Vadász * * Neither the name Intel Corporation nor the names of its
578a5b1999SImre Vadász * contributors may be used to endorse or promote products derived
588a5b1999SImre Vadász * from this software without specific prior written permission.
598a5b1999SImre Vadász *
608a5b1999SImre Vadász * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
618a5b1999SImre Vadász * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
628a5b1999SImre Vadász * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
638a5b1999SImre Vadász * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
648a5b1999SImre Vadász * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
658a5b1999SImre Vadász * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
668a5b1999SImre Vadász * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
678a5b1999SImre Vadász * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
688a5b1999SImre Vadász * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
698a5b1999SImre Vadász * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
708a5b1999SImre Vadász * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
718a5b1999SImre Vadász */
728a5b1999SImre Vadász
738a5b1999SImre Vadász #include <sys/param.h>
748a5b1999SImre Vadász #include <sys/bus.h>
756acbba79SMatthew Dillon #include <sys/conf.h>
768a5b1999SImre Vadász #include <sys/endian.h>
778a5b1999SImre Vadász #include <sys/firmware.h>
788a5b1999SImre Vadász #include <sys/kernel.h>
798a5b1999SImre Vadász #include <sys/malloc.h>
808a5b1999SImre Vadász #include <sys/mbuf.h>
818a5b1999SImre Vadász #include <sys/rman.h>
828a5b1999SImre Vadász #include <sys/sysctl.h>
838a5b1999SImre Vadász #include <sys/linker.h>
848a5b1999SImre Vadász
858a5b1999SImre Vadász #include <machine/endian.h>
868a5b1999SImre Vadász
878a5b1999SImre Vadász #include <bus/pci/pcivar.h>
888a5b1999SImre Vadász #include <bus/pci/pcireg.h>
898a5b1999SImre Vadász
908a5b1999SImre Vadász #include <net/bpf.h>
918a5b1999SImre Vadász
928a5b1999SImre Vadász #include <net/if.h>
938a5b1999SImre Vadász #include <net/if_var.h>
948a5b1999SImre Vadász #include <net/if_arp.h>
958a5b1999SImre Vadász #include <net/if_dl.h>
968a5b1999SImre Vadász #include <net/if_media.h>
978a5b1999SImre Vadász #include <net/if_types.h>
988a5b1999SImre Vadász
998a5b1999SImre Vadász #include <netinet/in.h>
1008a5b1999SImre Vadász #include <netinet/in_systm.h>
1018a5b1999SImre Vadász #include <netinet/if_ether.h>
1028a5b1999SImre Vadász #include <netinet/ip.h>
1038a5b1999SImre Vadász
1048a5b1999SImre Vadász #include <netproto/802_11/ieee80211_var.h>
1058a5b1999SImre Vadász #include <netproto/802_11/ieee80211_regdomain.h>
1068a5b1999SImre Vadász #include <netproto/802_11/ieee80211_ratectl.h>
1078a5b1999SImre Vadász #include <netproto/802_11/ieee80211_radiotap.h>
1088a5b1999SImre Vadász
1098a5b1999SImre Vadász #include "if_iwmreg.h"
1108a5b1999SImre Vadász #include "if_iwmvar.h"
1118a5b1999SImre Vadász #include "if_iwm_debug.h"
1128a5b1999SImre Vadász #include "if_iwm_util.h"
1138a5b1999SImre Vadász #include "if_iwm_fw.h"
1148a5b1999SImre Vadász
1158a5b1999SImre Vadász void
iwm_free_fw_paging(struct iwm_softc * sc)1168a5b1999SImre Vadász iwm_free_fw_paging(struct iwm_softc *sc)
1178a5b1999SImre Vadász {
1188a5b1999SImre Vadász int i;
1198a5b1999SImre Vadász
1208a5b1999SImre Vadász if (sc->fw_paging_db[0].fw_paging_block.vaddr == NULL)
1218a5b1999SImre Vadász return;
1228a5b1999SImre Vadász
1238a5b1999SImre Vadász for (i = 0; i < IWM_NUM_OF_FW_PAGING_BLOCKS; i++) {
1248a5b1999SImre Vadász iwm_dma_contig_free(&sc->fw_paging_db[i].fw_paging_block);
1258a5b1999SImre Vadász }
1268a5b1999SImre Vadász
1278a5b1999SImre Vadász memset(sc->fw_paging_db, 0, sizeof(sc->fw_paging_db));
1288a5b1999SImre Vadász }
1298a5b1999SImre Vadász
1308a5b1999SImre Vadász static int
iwm_fill_paging_mem(struct iwm_softc * sc,const struct iwm_fw_img * image)1314b1006a6SImre Vadász iwm_fill_paging_mem(struct iwm_softc *sc, const struct iwm_fw_img *image)
1328a5b1999SImre Vadász {
1338a5b1999SImre Vadász int sec_idx, idx;
1348a5b1999SImre Vadász uint32_t offset = 0;
1358a5b1999SImre Vadász
1368a5b1999SImre Vadász /*
1378a5b1999SImre Vadász * find where is the paging image start point:
1388a5b1999SImre Vadász * if CPU2 exist and it's in paging format, then the image looks like:
1398a5b1999SImre Vadász * CPU1 sections (2 or more)
1408a5b1999SImre Vadász * CPU1_CPU2_SEPARATOR_SECTION delimiter - separate between CPU1 to CPU2
1418a5b1999SImre Vadász * CPU2 sections (not paged)
1428a5b1999SImre Vadász * PAGING_SEPARATOR_SECTION delimiter - separate between CPU2
1438a5b1999SImre Vadász * non paged to CPU2 paging sec
1448a5b1999SImre Vadász * CPU2 paging CSS
1458a5b1999SImre Vadász * CPU2 paging image (including instruction and data)
1468a5b1999SImre Vadász */
1478a5b1999SImre Vadász for (sec_idx = 0; sec_idx < IWM_UCODE_SECTION_MAX; sec_idx++) {
1484b1006a6SImre Vadász if (image->sec[sec_idx].offset == IWM_PAGING_SEPARATOR_SECTION) {
1498a5b1999SImre Vadász sec_idx++;
1508a5b1999SImre Vadász break;
1518a5b1999SImre Vadász }
1528a5b1999SImre Vadász }
1538a5b1999SImre Vadász
1548a5b1999SImre Vadász /*
1558a5b1999SImre Vadász * If paging is enabled there should be at least 2 more sections left
1568a5b1999SImre Vadász * (one for CSS and one for Paging data)
1578a5b1999SImre Vadász */
1586acbba79SMatthew Dillon if (sec_idx >= nitems(image->sec) - 1) {
1598a5b1999SImre Vadász device_printf(sc->sc_dev,
1608a5b1999SImre Vadász "Paging: Missing CSS and/or paging sections\n");
1618a5b1999SImre Vadász iwm_free_fw_paging(sc);
1628a5b1999SImre Vadász return EINVAL;
1638a5b1999SImre Vadász }
1648a5b1999SImre Vadász
1658a5b1999SImre Vadász /* copy the CSS block to the dram */
1668a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
1678a5b1999SImre Vadász "Paging: load paging CSS to FW, sec = %d\n",
1688a5b1999SImre Vadász sec_idx);
1698a5b1999SImre Vadász
1708a5b1999SImre Vadász memcpy(sc->fw_paging_db[0].fw_paging_block.vaddr,
1714b1006a6SImre Vadász image->sec[sec_idx].data,
1728a5b1999SImre Vadász sc->fw_paging_db[0].fw_paging_size);
1738a5b1999SImre Vadász
1748a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
1758a5b1999SImre Vadász "Paging: copied %d CSS bytes to first block\n",
1768a5b1999SImre Vadász sc->fw_paging_db[0].fw_paging_size);
1778a5b1999SImre Vadász
1788a5b1999SImre Vadász sec_idx++;
1798a5b1999SImre Vadász
1808a5b1999SImre Vadász /*
1818a5b1999SImre Vadász * copy the paging blocks to the dram
1828a5b1999SImre Vadász * loop index start from 1 since that CSS block already copied to dram
1838a5b1999SImre Vadász * and CSS index is 0.
1848a5b1999SImre Vadász * loop stop at num_of_paging_blk since that last block is not full.
1858a5b1999SImre Vadász */
1868a5b1999SImre Vadász for (idx = 1; idx < sc->num_of_paging_blk; idx++) {
1878a5b1999SImre Vadász memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
1884b1006a6SImre Vadász (const char *)image->sec[sec_idx].data + offset,
1898a5b1999SImre Vadász sc->fw_paging_db[idx].fw_paging_size);
1908a5b1999SImre Vadász
1918a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
1928a5b1999SImre Vadász "Paging: copied %d paging bytes to block %d\n",
1938a5b1999SImre Vadász sc->fw_paging_db[idx].fw_paging_size,
1948a5b1999SImre Vadász idx);
1958a5b1999SImre Vadász
1968a5b1999SImre Vadász offset += sc->fw_paging_db[idx].fw_paging_size;
1978a5b1999SImre Vadász }
1988a5b1999SImre Vadász
1998a5b1999SImre Vadász /* copy the last paging block */
2008a5b1999SImre Vadász if (sc->num_of_pages_in_last_blk > 0) {
2018a5b1999SImre Vadász memcpy(sc->fw_paging_db[idx].fw_paging_block.vaddr,
2024b1006a6SImre Vadász (const char *)image->sec[sec_idx].data + offset,
2038a5b1999SImre Vadász IWM_FW_PAGING_SIZE * sc->num_of_pages_in_last_blk);
2048a5b1999SImre Vadász
2058a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
2068a5b1999SImre Vadász "Paging: copied %d pages in the last block %d\n",
2078a5b1999SImre Vadász sc->num_of_pages_in_last_blk, idx);
2088a5b1999SImre Vadász }
2098a5b1999SImre Vadász
2108a5b1999SImre Vadász return 0;
2118a5b1999SImre Vadász }
2128a5b1999SImre Vadász
2138a5b1999SImre Vadász static int
iwm_alloc_fw_paging_mem(struct iwm_softc * sc,const struct iwm_fw_img * image)2144b1006a6SImre Vadász iwm_alloc_fw_paging_mem(struct iwm_softc *sc, const struct iwm_fw_img *image)
2158a5b1999SImre Vadász {
2168a5b1999SImre Vadász int blk_idx = 0;
2178a5b1999SImre Vadász int error, num_of_pages;
2188a5b1999SImre Vadász
2198a5b1999SImre Vadász if (sc->fw_paging_db[0].fw_paging_block.vaddr != NULL) {
2208a5b1999SImre Vadász int i;
2218a5b1999SImre Vadász /* Device got reset, and we setup firmware paging again */
2228a5b1999SImre Vadász for (i = 0; i < sc->num_of_paging_blk + 1; i++) {
2238a5b1999SImre Vadász bus_dmamap_sync(sc->sc_dmat,
2248a5b1999SImre Vadász sc->fw_paging_db[i].fw_paging_block.map,
2258a5b1999SImre Vadász BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
2268a5b1999SImre Vadász }
2278a5b1999SImre Vadász return 0;
2288a5b1999SImre Vadász }
2298a5b1999SImre Vadász
2308a5b1999SImre Vadász /* ensure IWM_BLOCK_2_EXP_SIZE is power of 2 of IWM_PAGING_BLOCK_SIZE */
2318a5b1999SImre Vadász _Static_assert((1 << IWM_BLOCK_2_EXP_SIZE) == IWM_PAGING_BLOCK_SIZE,
2328a5b1999SImre Vadász "IWM_BLOCK_2_EXP_SIZE must be power of 2 of IWM_PAGING_BLOCK_SIZE");
2338a5b1999SImre Vadász
2348a5b1999SImre Vadász num_of_pages = image->paging_mem_size / IWM_FW_PAGING_SIZE;
2358a5b1999SImre Vadász sc->num_of_paging_blk = ((num_of_pages - 1) /
2368a5b1999SImre Vadász IWM_NUM_OF_PAGE_PER_GROUP) + 1;
2378a5b1999SImre Vadász
2388a5b1999SImre Vadász sc->num_of_pages_in_last_blk =
2398a5b1999SImre Vadász num_of_pages -
2408a5b1999SImre Vadász IWM_NUM_OF_PAGE_PER_GROUP * (sc->num_of_paging_blk - 1);
2418a5b1999SImre Vadász
2428a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
2438a5b1999SImre Vadász "Paging: allocating mem for %d paging blocks, each block holds 8 pages, last block holds %d pages\n",
2448a5b1999SImre Vadász sc->num_of_paging_blk,
2458a5b1999SImre Vadász sc->num_of_pages_in_last_blk);
2468a5b1999SImre Vadász
2478a5b1999SImre Vadász /* allocate block of 4Kbytes for paging CSS */
2488a5b1999SImre Vadász error = iwm_dma_contig_alloc(sc->sc_dmat,
2498a5b1999SImre Vadász &sc->fw_paging_db[blk_idx].fw_paging_block, IWM_FW_PAGING_SIZE,
2508a5b1999SImre Vadász 4096);
2518a5b1999SImre Vadász if (error) {
2528a5b1999SImre Vadász /* free all the previous pages since we failed */
2538a5b1999SImre Vadász iwm_free_fw_paging(sc);
2548a5b1999SImre Vadász return ENOMEM;
2558a5b1999SImre Vadász }
2568a5b1999SImre Vadász
2578a5b1999SImre Vadász sc->fw_paging_db[blk_idx].fw_paging_size = IWM_FW_PAGING_SIZE;
2588a5b1999SImre Vadász
2598a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
2608a5b1999SImre Vadász "Paging: allocated 4K(CSS) bytes for firmware paging.\n");
2618a5b1999SImre Vadász
2628a5b1999SImre Vadász /*
2638a5b1999SImre Vadász * allocate blocks in dram.
2648a5b1999SImre Vadász * since that CSS allocated in fw_paging_db[0] loop start from index 1
2658a5b1999SImre Vadász */
2668a5b1999SImre Vadász for (blk_idx = 1; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
2678a5b1999SImre Vadász /* allocate block of IWM_PAGING_BLOCK_SIZE (32K) */
2688a5b1999SImre Vadász /* XXX Use iwm_dma_contig_alloc for allocating */
2698a5b1999SImre Vadász error = iwm_dma_contig_alloc(sc->sc_dmat,
2708a5b1999SImre Vadász &sc->fw_paging_db[blk_idx].fw_paging_block,
2718a5b1999SImre Vadász IWM_PAGING_BLOCK_SIZE, 4096);
2728a5b1999SImre Vadász if (error) {
2738a5b1999SImre Vadász /* free all the previous pages since we failed */
2748a5b1999SImre Vadász iwm_free_fw_paging(sc);
2758a5b1999SImre Vadász return ENOMEM;
2768a5b1999SImre Vadász }
2778a5b1999SImre Vadász
2788a5b1999SImre Vadász sc->fw_paging_db[blk_idx].fw_paging_size = IWM_PAGING_BLOCK_SIZE;
2798a5b1999SImre Vadász
2808a5b1999SImre Vadász IWM_DPRINTF(sc, IWM_DEBUG_FW,
2818a5b1999SImre Vadász "Paging: allocated 32K bytes for firmware paging.\n");
2828a5b1999SImre Vadász }
2838a5b1999SImre Vadász
2848a5b1999SImre Vadász return 0;
2858a5b1999SImre Vadász }
2868a5b1999SImre Vadász
2878a5b1999SImre Vadász int
iwm_save_fw_paging(struct iwm_softc * sc,const struct iwm_fw_img * fw)2884b1006a6SImre Vadász iwm_save_fw_paging(struct iwm_softc *sc, const struct iwm_fw_img *fw)
2898a5b1999SImre Vadász {
2908a5b1999SImre Vadász int ret;
2918a5b1999SImre Vadász
2928a5b1999SImre Vadász ret = iwm_alloc_fw_paging_mem(sc, fw);
2938a5b1999SImre Vadász if (ret)
2948a5b1999SImre Vadász return ret;
2958a5b1999SImre Vadász
2968a5b1999SImre Vadász return iwm_fill_paging_mem(sc, fw);
2978a5b1999SImre Vadász }
2988a5b1999SImre Vadász
2998a5b1999SImre Vadász /* send paging cmd to FW in case CPU2 has paging image */
3008a5b1999SImre Vadász int
iwm_send_paging_cmd(struct iwm_softc * sc,const struct iwm_fw_img * fw)3014b1006a6SImre Vadász iwm_send_paging_cmd(struct iwm_softc *sc, const struct iwm_fw_img *fw)
3028a5b1999SImre Vadász {
3038a5b1999SImre Vadász int blk_idx;
3048a5b1999SImre Vadász uint32_t dev_phy_addr;
3058a5b1999SImre Vadász struct iwm_fw_paging_cmd fw_paging_cmd = {
3068a5b1999SImre Vadász .flags =
3078a5b1999SImre Vadász htole32(IWM_PAGING_CMD_IS_SECURED |
3088a5b1999SImre Vadász IWM_PAGING_CMD_IS_ENABLED |
3098a5b1999SImre Vadász (sc->num_of_pages_in_last_blk <<
3108a5b1999SImre Vadász IWM_PAGING_CMD_NUM_OF_PAGES_IN_LAST_GRP_POS)),
3118a5b1999SImre Vadász .block_size = htole32(IWM_BLOCK_2_EXP_SIZE),
3128a5b1999SImre Vadász .block_num = htole32(sc->num_of_paging_blk),
3138a5b1999SImre Vadász };
3148a5b1999SImre Vadász
315*4cbc7cf9SMichael Neumann /* loop for all paging blocks + CSS block */
3168a5b1999SImre Vadász for (blk_idx = 0; blk_idx < sc->num_of_paging_blk + 1; blk_idx++) {
3178a5b1999SImre Vadász dev_phy_addr = htole32(
3188a5b1999SImre Vadász sc->fw_paging_db[blk_idx].fw_paging_block.paddr >>
3198a5b1999SImre Vadász IWM_PAGE_2_EXP_SIZE);
3208a5b1999SImre Vadász fw_paging_cmd.device_phy_addr[blk_idx] = dev_phy_addr;
3218a5b1999SImre Vadász bus_dmamap_sync(sc->sc_dmat,
3228a5b1999SImre Vadász sc->fw_paging_db[blk_idx].fw_paging_block.map,
3238a5b1999SImre Vadász BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
3248a5b1999SImre Vadász }
3258a5b1999SImre Vadász
3266acbba79SMatthew Dillon return iwm_send_cmd_pdu(sc, iwm_cmd_id(IWM_FW_PAGING_BLOCK_CMD,
3278a5b1999SImre Vadász IWM_ALWAYS_LONG_GROUP, 0),
3288a5b1999SImre Vadász 0, sizeof(fw_paging_cmd), &fw_paging_cmd);
3298a5b1999SImre Vadász }
330