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/ethernet.h> 135 #include <net/if_dl.h> 136 #include <net/if_media.h> 137 #include <net/if_types.h> 138 #include <net/ifq_var.h> 139 140 #include <netinet/in.h> 141 #include <netinet/in_systm.h> 142 #include <netinet/if_ether.h> 143 #include <netinet/ip.h> 144 145 #include <netproto/802_11/ieee80211_var.h> 146 #include <netproto/802_11/ieee80211_regdomain.h> 147 #include <netproto/802_11/ieee80211_ratectl.h> 148 #include <netproto/802_11/ieee80211_radiotap.h> 149 150 #include "if_iwmreg.h" 151 #include "if_iwmvar.h" 152 #include "if_iwm_debug.h" 153 #include "if_iwm_util.h" 154 #include "if_iwm_phy_db.h" 155 156 /* 157 * get phy db section: returns a pointer to a phy db section specified by 158 * type and channel group id. 159 */ 160 static struct iwm_phy_db_entry * 161 iwm_phy_db_get_section(struct iwm_softc *sc, 162 enum iwm_phy_db_section_type type, uint16_t chg_id) 163 { 164 struct iwm_phy_db *phy_db = &sc->sc_phy_db; 165 166 if (type >= IWM_PHY_DB_MAX) 167 return NULL; 168 169 switch (type) { 170 case IWM_PHY_DB_CFG: 171 return &phy_db->cfg; 172 case IWM_PHY_DB_CALIB_NCH: 173 return &phy_db->calib_nch; 174 case IWM_PHY_DB_CALIB_CHG_PAPD: 175 if (chg_id >= IWM_NUM_PAPD_CH_GROUPS) 176 return NULL; 177 return &phy_db->calib_ch_group_papd[chg_id]; 178 case IWM_PHY_DB_CALIB_CHG_TXP: 179 if (chg_id >= IWM_NUM_TXP_CH_GROUPS) 180 return NULL; 181 return &phy_db->calib_ch_group_txp[chg_id]; 182 default: 183 return NULL; 184 } 185 return NULL; 186 } 187 188 int 189 iwm_phy_db_set_section(struct iwm_softc *sc, 190 struct iwm_calib_res_notif_phy_db *phy_db_notif) 191 { 192 enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type); 193 uint16_t size = le16toh(phy_db_notif->length); 194 struct iwm_phy_db_entry *entry; 195 uint16_t chg_id = 0; 196 197 if (type == IWM_PHY_DB_CALIB_CHG_PAPD || 198 type == IWM_PHY_DB_CALIB_CHG_TXP) 199 chg_id = le16toh(*(uint16_t *)phy_db_notif->data); 200 201 entry = iwm_phy_db_get_section(sc, type, chg_id); 202 if (!entry) 203 return EINVAL; 204 205 if (entry->data) 206 kfree(entry->data, M_DEVBUF); 207 entry->data = kmalloc(size, M_DEVBUF, M_INTWAIT); 208 if (!entry->data) { 209 entry->size = 0; 210 return ENOMEM; 211 } 212 memcpy(entry->data, phy_db_notif->data, size); 213 entry->size = size; 214 215 IWM_DPRINTF(sc, IWM_DEBUG_RESET, 216 "%s(%d): [PHYDB]SET: Type %d , Size: %d, data: %p\n", 217 __func__, __LINE__, type, size, entry->data); 218 219 return 0; 220 } 221 222 static int 223 iwm_is_valid_channel(uint16_t ch_id) 224 { 225 if (ch_id <= 14 || 226 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) || 227 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) || 228 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1)) 229 return 1; 230 return 0; 231 } 232 233 static uint8_t 234 iwm_ch_id_to_ch_index(uint16_t ch_id) 235 { 236 if (!iwm_is_valid_channel(ch_id)) 237 return 0xff; 238 239 if (ch_id <= 14) 240 return ch_id - 1; 241 if (ch_id <= 64) 242 return (ch_id + 20) / 4; 243 if (ch_id <= 140) 244 return (ch_id - 12) / 4; 245 return (ch_id - 13) / 4; 246 } 247 248 249 static uint16_t 250 iwm_channel_id_to_papd(uint16_t ch_id) 251 { 252 if (!iwm_is_valid_channel(ch_id)) 253 return 0xff; 254 255 if (1 <= ch_id && ch_id <= 14) 256 return 0; 257 if (36 <= ch_id && ch_id <= 64) 258 return 1; 259 if (100 <= ch_id && ch_id <= 140) 260 return 2; 261 return 3; 262 } 263 264 static uint16_t 265 iwm_channel_id_to_txp(struct iwm_softc *sc, uint16_t ch_id) 266 { 267 struct iwm_phy_db *phy_db = &sc->sc_phy_db; 268 struct iwm_phy_db_chg_txp *txp_chg; 269 int i; 270 uint8_t ch_index = iwm_ch_id_to_ch_index(ch_id); 271 272 if (ch_index == 0xff) 273 return 0xff; 274 275 for (i = 0; i < IWM_NUM_TXP_CH_GROUPS; i++) { 276 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data; 277 if (!txp_chg) 278 return 0xff; 279 /* 280 * Looking for the first channel group that its max channel is 281 * higher then wanted channel. 282 */ 283 if (le16toh(txp_chg->max_channel_idx) >= ch_index) 284 return i; 285 } 286 return 0xff; 287 } 288 289 static int 290 iwm_phy_db_get_section_data(struct iwm_softc *sc, 291 uint32_t type, uint8_t **data, uint16_t *size, uint16_t ch_id) 292 { 293 struct iwm_phy_db_entry *entry; 294 uint16_t ch_group_id = 0; 295 296 IWM_DPRINTF(sc, IWM_DEBUG_RESET, "->%s\n", __func__); 297 /* find wanted channel group */ 298 if (type == IWM_PHY_DB_CALIB_CHG_PAPD) 299 ch_group_id = iwm_channel_id_to_papd(ch_id); 300 else if (type == IWM_PHY_DB_CALIB_CHG_TXP) 301 ch_group_id = iwm_channel_id_to_txp(sc, ch_id); 302 303 entry = iwm_phy_db_get_section(sc, type, ch_group_id); 304 if (!entry) 305 return EINVAL; 306 307 *data = entry->data; 308 *size = entry->size; 309 310 IWM_DPRINTF(sc, IWM_DEBUG_RESET, 311 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n", 312 __func__, __LINE__, type, *size); 313 314 return 0; 315 } 316 317 static int 318 iwm_send_phy_db_cmd(struct iwm_softc *sc, uint16_t type, 319 uint16_t length, void *data) 320 { 321 struct iwm_phy_db_cmd phy_db_cmd; 322 struct iwm_host_cmd cmd = { 323 .id = IWM_PHY_DB_CMD, 324 .flags = IWM_CMD_SYNC, 325 }; 326 327 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 328 "Sending PHY-DB hcmd of type %d, of length %d\n", 329 type, length); 330 331 /* Set phy db cmd variables */ 332 phy_db_cmd.type = le16toh(type); 333 phy_db_cmd.length = le16toh(length); 334 335 /* Set hcmd variables */ 336 cmd.data[0] = &phy_db_cmd; 337 cmd.len[0] = sizeof(struct iwm_phy_db_cmd); 338 cmd.data[1] = data; 339 cmd.len[1] = length; 340 cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY; 341 342 return iwm_send_cmd(sc, &cmd); 343 } 344 345 static int 346 iwm_phy_db_send_all_channel_groups(struct iwm_softc *sc, 347 enum iwm_phy_db_section_type type, uint8_t max_ch_groups) 348 { 349 uint16_t i; 350 int err; 351 struct iwm_phy_db_entry *entry; 352 353 /* Send all the channel-specific groups to operational fw */ 354 for (i = 0; i < max_ch_groups; i++) { 355 entry = iwm_phy_db_get_section(sc, type, i); 356 if (!entry) 357 return EINVAL; 358 359 if (!entry->size) 360 continue; 361 362 /* Send the requested PHY DB section */ 363 err = iwm_send_phy_db_cmd(sc, type, entry->size, entry->data); 364 if (err) { 365 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 366 "%s: Can't SEND phy_db section %d (%d), " 367 "err %d\n", __func__, type, i, err); 368 return err; 369 } 370 371 IWM_DPRINTF(sc, IWM_DEBUG_CMD, 372 "Sent PHY_DB HCMD, type = %d num = %d\n", type, i); 373 } 374 375 return 0; 376 } 377 378 int 379 iwm_send_phy_db_data(struct iwm_softc *sc) 380 { 381 uint8_t *data = NULL; 382 uint16_t size = 0; 383 int err; 384 385 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 386 "%s: Sending phy db data and configuration to runtime image\n", 387 __func__); 388 389 /* Send PHY DB CFG section */ 390 err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CFG, &data, &size, 0); 391 if (err) { 392 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 393 "%s: Cannot get Phy DB cfg section, %d\n", 394 __func__, err); 395 return err; 396 } 397 398 err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CFG, size, data); 399 if (err) { 400 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 401 "%s: Cannot send HCMD of Phy DB cfg section, %d\n", 402 __func__, err); 403 return err; 404 } 405 406 err = iwm_phy_db_get_section_data(sc, IWM_PHY_DB_CALIB_NCH, 407 &data, &size, 0); 408 if (err) { 409 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 410 "%s: Cannot get Phy DB non specific channel section, " 411 "%d\n", __func__, err); 412 return err; 413 } 414 415 err = iwm_send_phy_db_cmd(sc, IWM_PHY_DB_CALIB_NCH, size, data); 416 if (err) { 417 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 418 "%s: Cannot send HCMD of Phy DB non specific channel " 419 "sect, %d\n", __func__, err); 420 return err; 421 } 422 423 /* Send all the TXP channel specific data */ 424 err = iwm_phy_db_send_all_channel_groups(sc, 425 IWM_PHY_DB_CALIB_CHG_PAPD, IWM_NUM_PAPD_CH_GROUPS); 426 if (err) { 427 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 428 "%s: Cannot send channel specific PAPD groups, %d\n", 429 __func__, err); 430 return err; 431 } 432 433 /* Send all the TXP channel specific data */ 434 err = iwm_phy_db_send_all_channel_groups(sc, 435 IWM_PHY_DB_CALIB_CHG_TXP, IWM_NUM_TXP_CH_GROUPS); 436 if (err) { 437 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 438 "%s: Cannot send channel specific TX power groups, " 439 "%d\n", __func__, err); 440 return err; 441 } 442 443 IWM_DPRINTF(sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 444 "%s: Finished sending phy db non channel data\n", 445 __func__); 446 return 0; 447 } 448