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