1 /* $OpenBSD: if_iwm.c,v 1.39 2015/03/23 00:35:19 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2014 genua mbh <info@genua.de> 5 * Copyright (c) 2014 Fixup Software Ltd. 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /*- 21 * Based on BSD-licensed source modules in the Linux iwlwifi driver, 22 * which were used as the reference documentation for this implementation. 23 * 24 * Driver version we are currently based off of is 25 * Linux 3.14.3 (tag id a2df521e42b1d9a23f620ac79dbfe8655a8391dd) 26 * 27 *********************************************************************** 28 * 29 * This file is provided under a dual BSD/GPLv2 license. When using or 30 * redistributing this file, you may do so under either license. 31 * 32 * GPL LICENSE SUMMARY 33 * 34 * Copyright(c) 2007 - 2013 Intel Corporation. All rights reserved. 35 * 36 * This program is free software; you can redistribute it and/or modify 37 * it under the terms of version 2 of the GNU General Public License as 38 * published by the Free Software Foundation. 39 * 40 * This program is distributed in the hope that it will be useful, but 41 * WITHOUT ANY WARRANTY; without even the implied warranty of 42 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 43 * General Public License for more details. 44 * 45 * You should have received a copy of the GNU General Public License 46 * along with this program; if not, write to the Free Software 47 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, 48 * USA 49 * 50 * The full GNU General Public License is included in this distribution 51 * in the file called COPYING. 52 * 53 * Contact Information: 54 * Intel Linux Wireless <ilw@linux.intel.com> 55 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 56 * 57 * 58 * BSD LICENSE 59 * 60 * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved. 61 * All rights reserved. 62 * 63 * Redistribution and use in source and binary forms, with or without 64 * modification, are permitted provided that the following conditions 65 * are met: 66 * 67 * * Redistributions of source code must retain the above copyright 68 * notice, this list of conditions and the following disclaimer. 69 * * Redistributions in binary form must reproduce the above copyright 70 * notice, this list of conditions and the following disclaimer in 71 * the documentation and/or other materials provided with the 72 * distribution. 73 * * Neither the name Intel Corporation nor the names of its 74 * contributors may be used to endorse or promote products derived 75 * from this software without specific prior written permission. 76 * 77 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 78 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 79 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 80 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 81 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 82 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 83 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 84 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 85 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 86 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 87 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 88 */ 89 90 /*- 91 * Copyright (c) 2007-2010 Damien Bergamini <damien.bergamini@free.fr> 92 * 93 * Permission to use, copy, modify, and distribute this software for any 94 * purpose with or without fee is hereby granted, provided that the above 95 * copyright notice and this permission notice appear in all copies. 96 * 97 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 98 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 99 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 100 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 101 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 102 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 103 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 104 */ 105 #include <sys/cdefs.h> 106 __FBSDID("$FreeBSD$"); 107 108 #include <sys/param.h> 109 #include <sys/bus.h> 110 #include <sys/endian.h> 111 #include <sys/firmware.h> 112 #include <sys/kernel.h> 113 #include <sys/malloc.h> 114 #include <sys/mbuf.h> 115 #include <sys/mutex.h> 116 #include <sys/module.h> 117 #include <sys/proc.h> 118 #include <sys/rman.h> 119 #include <sys/socket.h> 120 #include <sys/sockio.h> 121 #include <sys/sysctl.h> 122 #include <sys/linker.h> 123 124 #include <machine/endian.h> 125 126 #include <bus/pci/pcivar.h> 127 #include <bus/pci/pcireg.h> 128 129 #include <net/bpf.h> 130 131 #include <net/if.h> 132 #include <net/if_var.h> 133 #include <net/if_arp.h> 134 #include <net/if_dl.h> 135 #include <net/if_media.h> 136 #include <net/if_types.h> 137 138 #include <netinet/in.h> 139 #include <netinet/in_systm.h> 140 #include <netinet/if_ether.h> 141 #include <netinet/ip.h> 142 143 #include <netproto/802_11/ieee80211_var.h> 144 #include <netproto/802_11/ieee80211_regdomain.h> 145 #include <netproto/802_11/ieee80211_ratectl.h> 146 #include <netproto/802_11/ieee80211_radiotap.h> 147 148 #include "if_iwmreg.h" 149 #include "if_iwmvar.h" 150 #include "if_iwm_debug.h" 151 #include "if_iwm_util.h" 152 #include "if_iwm_phy_db.h" 153 154 /* 155 * get phy db section: returns a pointer to a phy db section specified by 156 * type and channel group id. 157 */ 158 static struct iwm_phy_db_entry * 159 iwm_phy_db_get_section(struct iwm_softc *sc, 160 enum iwm_phy_db_section_type type, uint16_t chg_id) 161 { 162 struct iwm_phy_db *phy_db = &sc->sc_phy_db; 163 164 if (type >= IWM_PHY_DB_MAX) 165 return NULL; 166 167 switch (type) { 168 case IWM_PHY_DB_CFG: 169 return &phy_db->cfg; 170 case IWM_PHY_DB_CALIB_NCH: 171 return &phy_db->calib_nch; 172 case IWM_PHY_DB_CALIB_CHG_PAPD: 173 if (chg_id >= IWM_NUM_PAPD_CH_GROUPS) 174 return NULL; 175 return &phy_db->calib_ch_group_papd[chg_id]; 176 case IWM_PHY_DB_CALIB_CHG_TXP: 177 if (chg_id >= IWM_NUM_TXP_CH_GROUPS) 178 return NULL; 179 return &phy_db->calib_ch_group_txp[chg_id]; 180 default: 181 return NULL; 182 } 183 return NULL; 184 } 185 186 int 187 iwm_phy_db_set_section(struct iwm_softc *sc, 188 struct iwm_calib_res_notif_phy_db *phy_db_notif) 189 { 190 enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type); 191 uint16_t size = le16toh(phy_db_notif->length); 192 struct iwm_phy_db_entry *entry; 193 uint16_t chg_id = 0; 194 195 if (type == IWM_PHY_DB_CALIB_CHG_PAPD || 196 type == IWM_PHY_DB_CALIB_CHG_TXP) 197 chg_id = le16toh(*(uint16_t *)phy_db_notif->data); 198 199 entry = iwm_phy_db_get_section(sc, type, chg_id); 200 if (!entry) 201 return EINVAL; 202 203 if (entry->data) 204 kfree(entry->data, M_DEVBUF); 205 entry->data = kmalloc(size, M_DEVBUF, M_INTWAIT); 206 if (!entry->data) { 207 entry->size = 0; 208 return ENOMEM; 209 } 210 memcpy(entry->data, phy_db_notif->data, size); 211 entry->size = size; 212 213 IWM_DPRINTF(sc, IWM_DEBUG_RESET, 214 "%s(%d): [PHYDB]SET: Type %d , Size: %d, data: %p\n", 215 __func__, __LINE__, type, size, entry->data); 216 217 return 0; 218 } 219 220 static int 221 iwm_is_valid_channel(uint16_t ch_id) 222 { 223 if (ch_id <= 14 || 224 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) || 225 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) || 226 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1)) 227 return 1; 228 return 0; 229 } 230 231 static uint8_t 232 iwm_ch_id_to_ch_index(uint16_t ch_id) 233 { 234 if (!iwm_is_valid_channel(ch_id)) 235 return 0xff; 236 237 if (ch_id <= 14) 238 return ch_id - 1; 239 if (ch_id <= 64) 240 return (ch_id + 20) / 4; 241 if (ch_id <= 140) 242 return (ch_id - 12) / 4; 243 return (ch_id - 13) / 4; 244 } 245 246 247 static uint16_t 248 iwm_channel_id_to_papd(uint16_t ch_id) 249 { 250 if (!iwm_is_valid_channel(ch_id)) 251 return 0xff; 252 253 if (1 <= ch_id && ch_id <= 14) 254 return 0; 255 if (36 <= ch_id && ch_id <= 64) 256 return 1; 257 if (100 <= ch_id && ch_id <= 140) 258 return 2; 259 return 3; 260 } 261 262 static uint16_t 263 iwm_channel_id_to_txp(struct iwm_softc *sc, uint16_t ch_id) 264 { 265 struct iwm_phy_db *phy_db = &sc->sc_phy_db; 266 struct iwm_phy_db_chg_txp *txp_chg; 267 int i; 268 uint8_t ch_index = iwm_ch_id_to_ch_index(ch_id); 269 270 if (ch_index == 0xff) 271 return 0xff; 272 273 for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++) { 274 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data; 275 if (!txp_chg) 276 return 0xff; 277 /* 278 * Looking for the first channel group that its max channel is 279 * higher then wanted channel. 280 */ 281 if (le16toh(txp_chg->max_channel_idx) >= ch_index) 282 return i; 283 } 284 return 0xff; 285 } 286 287 static int 288 iwm_phy_db_get_section_data(struct iwm_softc *sc, 289 uint32_t type, uint8_t **data, uint16_t *size, uint16_t ch_id) 290 { 291 struct iwm_phy_db_entry *entry; 292 uint16_t ch_group_id = 0; 293 294 IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__); 295 /* find wanted channel group */ 296 if (type == IWM_PHY_DB_CALIB_CHG_PAPD) 297 ch_group_id = iwm_channel_id_to_papd(ch_id); 298 else if (type == IWM_PHY_DB_CALIB_CHG_TXP) 299 ch_group_id = iwm_channel_id_to_txp(sc, ch_id); 300 301 entry = iwm_phy_db_get_section(sc, type, ch_group_id); 302 if (!entry) 303 return EINVAL; 304 305 *data = entry->data; 306 *size = entry->size; 307 308 IWM_DPRINTF(sc, IWM_DEBUG_RESET, 309 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n", 310 __func__, __LINE__, type, *size); 311 312 return 0; 313 } 314 315 static int 316 iwm_send_phy_db_cmd(struct iwm_softc *sc, uint16_t type, 317 uint16_t length, void *data) 318 { 319 struct iwm_phy_db_cmd phy_db_cmd; 320 struct iwm_host_cmd cmd = { 321 .id = IWM_PHY_DB_CMD, 322 .flags = IWM_CMD_SYNC, 323 }; 324 325 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 326 "Sending PHY-DB hcmd of type %d, of length %d\n", 327 type, length); 328 329 /* Set phy db cmd variables */ 330 phy_db_cmd.type = le16toh(type); 331 phy_db_cmd.length = le16toh(length); 332 333 /* Set hcmd variables */ 334 cmd.data[0] = &phy_db_cmd; 335 cmd.len[0] = sizeof(struct iwm_phy_db_cmd); 336 cmd.data[1] = data; 337 cmd.len[1] = length; 338 339 return iwm_send_cmd(sc, &cmd); 340 } 341 342 static int 343 iwm_phy_db_send_all_channel_groups(struct iwm_softc *sc, 344 enum iwm_phy_db_section_type type, uint8_t max_ch_groups) 345 { 346 uint16_t i; 347 int err; 348 struct iwm_phy_db_entry *entry; 349 350 /* Send all the channel-specific groups to operational fw */ 351 for (i = 0; i < max_ch_groups; i++) { 352 entry = iwm_phy_db_get_section(sc, type, i); 353 if (!entry) 354 return EINVAL; 355 356 if (!entry->size) 357 continue; 358 359 /* Send the requested PHY DB section */ 360 err = iwm_send_phy_db_cmd(sc, type, entry->size, entry->data); 361 if (err) { 362 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 363 "%s: Can't SEND phy_db section %d (%d), " 364 "err %d\n", __func__, type, i, err); 365 return err; 366 } 367 368 DELAY(1000); 369 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 370 "Sent PHY_DB HCMD, type = %d num = %d\n", type, i); 371 } 372 373 return 0; 374 } 375 376 int 377 iwm_send_phy_db_data(struct iwm_softc *sc) 378 { 379 uint8_t *data = NULL; 380 uint16_t size = 0; 381 int err; 382 383 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 384 "%s: Sending phy db data and configuration to runtime image\n", 385 __func__); 386 387 /* Send PHY DB CFG section */ 388 err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CFG, &data, &size, 0); 389 if (err) { 390 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 391 "%s: Cannot get Phy DB cfg section, %d\n", 392 __func__, err); 393 return err; 394 } 395 396 err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CFG, size, data); 397 if (err) { 398 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 399 "%s: Cannot send HCMD of Phy DB cfg section, %d\n", 400 __func__, err); 401 return err; 402 } 403 404 err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CALIB_NCH, 405 &data, &size, 0); 406 if (err) { 407 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 408 "%s: Cannot get Phy DB non specific channel section, " 409 "%d\n", __func__, err); 410 return err; 411 } 412 413 err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CALIB_NCH, size, data); 414 if (err) { 415 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 416 "%s: Cannot send HCMD of Phy DB non specific channel " 417 "sect, %d\n", __func__, err); 418 return err; 419 } 420 421 /* Send all the TXP channel specific data */ 422 err = iwm_phy_db_send_all_channel_groups(sc, 423 IWM_PHY_DB_CALIB_CHG_PAPD, IWM_NUM_PAPD_CH_GROUPS); 424 if (err) { 425 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 426 "%s: Cannot send channel specific PAPD groups, %d\n", 427 __func__, err); 428 return err; 429 } 430 431 /* Send all the TXP channel specific data */ 432 err = iwm_phy_db_send_all_channel_groups(sc, 433 IWM_PHY_DB_CALIB_CHG_TXP, IWM_NUM_TXP_CH_GROUPS); 434 if (err) { 435 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 436 "%s: Cannot send channel specific TX power groups, " 437 "%d\n", __func__, err); 438 return err; 439 } 440 441 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 442 "%s: Finished sending phy db non channel data\n", 443 __func__); 444 return 0; 445 } 446