if_iwm_phy_db.c revision 330163
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: stable/11/sys/dev/iwm/if_iwm_phy_db.c 330163 2018-03-01 05:09:49Z eadler $"); 107 108#include "opt_wlan.h" 109 110#include <sys/param.h> 111#include <sys/bus.h> 112#include <sys/conf.h> 113#include <sys/endian.h> 114#include <sys/firmware.h> 115#include <sys/kernel.h> 116#include <sys/malloc.h> 117#include <sys/mbuf.h> 118#include <sys/mutex.h> 119#include <sys/module.h> 120#include <sys/proc.h> 121#include <sys/rman.h> 122#include <sys/socket.h> 123#include <sys/sockio.h> 124#include <sys/sysctl.h> 125#include <sys/linker.h> 126 127#include <machine/bus.h> 128#include <machine/endian.h> 129#include <machine/resource.h> 130 131#include <dev/pci/pcivar.h> 132#include <dev/pci/pcireg.h> 133 134#include <net/bpf.h> 135 136#include <net/if.h> 137#include <net/if_var.h> 138#include <net/if_arp.h> 139#include <net/if_dl.h> 140#include <net/if_media.h> 141#include <net/if_types.h> 142 143#include <netinet/in.h> 144#include <netinet/in_systm.h> 145#include <netinet/if_ether.h> 146#include <netinet/ip.h> 147 148#include <net80211/ieee80211_var.h> 149#include <net80211/ieee80211_regdomain.h> 150#include <net80211/ieee80211_ratectl.h> 151#include <net80211/ieee80211_radiotap.h> 152 153#include "if_iwmreg.h" 154#include "if_iwmvar.h" 155#include "if_iwm_debug.h" 156#include "if_iwm_util.h" 157#include "if_iwm_phy_db.h" 158 159#define CHANNEL_NUM_SIZE 4 /* num of channels in calib_ch size */ 160 161struct iwm_phy_db_entry { 162 uint16_t size; 163 uint8_t *data; 164}; 165 166/** 167 * struct iwm_phy_db - stores phy configuration and calibration data. 168 * 169 * @cfg: phy configuration. 170 * @calib_nch: non channel specific calibration data. 171 * @calib_ch: channel specific calibration data. 172 * @n_group_papd: number of entries in papd channel group. 173 * @calib_ch_group_papd: calibration data related to papd channel group. 174 * @n_group_txp: number of entries in tx power channel group. 175 * @calib_ch_group_txp: calibration data related to tx power chanel group. 176 */ 177struct iwm_phy_db { 178 struct iwm_phy_db_entry cfg; 179 struct iwm_phy_db_entry calib_nch; 180 int n_group_papd; 181 struct iwm_phy_db_entry *calib_ch_group_papd; 182 int n_group_txp; 183 struct iwm_phy_db_entry *calib_ch_group_txp; 184 185 struct iwm_softc *sc; 186}; 187 188enum iwm_phy_db_section_type { 189 IWM_PHY_DB_CFG = 1, 190 IWM_PHY_DB_CALIB_NCH, 191 IWM_PHY_DB_UNUSED, 192 IWM_PHY_DB_CALIB_CHG_PAPD, 193 IWM_PHY_DB_CALIB_CHG_TXP, 194 IWM_PHY_DB_MAX 195}; 196 197#define PHY_DB_CMD 0x6c /* TEMP API - The actual is 0x8c */ 198 199/* 200 * phy db - configure operational ucode 201 */ 202struct iwm_phy_db_cmd { 203 uint16_t type; 204 uint16_t length; 205 uint8_t data[]; 206} __packed; 207 208/* for parsing of tx power channel group data that comes from the firmware*/ 209struct iwm_phy_db_chg_txp { 210 uint32_t space; 211 uint16_t max_channel_idx; 212} __packed; 213 214/* 215 * phy db - Receive phy db chunk after calibrations 216 */ 217struct iwm_calib_res_notif_phy_db { 218 uint16_t type; 219 uint16_t length; 220 uint8_t data[]; 221} __packed; 222 223struct iwm_phy_db * 224iwm_phy_db_init(struct iwm_softc *sc) 225{ 226 struct iwm_phy_db *phy_db = malloc(sizeof(struct iwm_phy_db), 227 M_DEVBUF, M_NOWAIT | M_ZERO); 228 229 if (!phy_db) 230 return phy_db; 231 232 phy_db->sc = sc; 233 234 phy_db->n_group_txp = -1; 235 phy_db->n_group_papd = -1; 236 237 /* TODO: add default values of the phy db. */ 238 return phy_db; 239} 240 241/* 242 * get phy db section: returns a pointer to a phy db section specified by 243 * type and channel group id. 244 */ 245static struct iwm_phy_db_entry * 246iwm_phy_db_get_section(struct iwm_phy_db *phy_db, 247 enum iwm_phy_db_section_type type, 248 uint16_t chg_id) 249{ 250 if (!phy_db || type >= IWM_PHY_DB_MAX) 251 return NULL; 252 253 switch (type) { 254 case IWM_PHY_DB_CFG: 255 return &phy_db->cfg; 256 case IWM_PHY_DB_CALIB_NCH: 257 return &phy_db->calib_nch; 258 case IWM_PHY_DB_CALIB_CHG_PAPD: 259 if (chg_id >= phy_db->n_group_papd) 260 return NULL; 261 return &phy_db->calib_ch_group_papd[chg_id]; 262 case IWM_PHY_DB_CALIB_CHG_TXP: 263 if (chg_id >= phy_db->n_group_txp) 264 return NULL; 265 return &phy_db->calib_ch_group_txp[chg_id]; 266 default: 267 return NULL; 268 } 269 return NULL; 270} 271 272static void 273iwm_phy_db_free_section(struct iwm_phy_db *phy_db, 274 enum iwm_phy_db_section_type type, uint16_t chg_id) 275{ 276 struct iwm_phy_db_entry *entry = 277 iwm_phy_db_get_section(phy_db, type, chg_id); 278 if (!entry) 279 return; 280 281 if (entry->data != NULL) 282 free(entry->data, M_DEVBUF); 283 entry->data = NULL; 284 entry->size = 0; 285} 286 287void 288iwm_phy_db_free(struct iwm_phy_db *phy_db) 289{ 290 int i; 291 292 if (!phy_db) 293 return; 294 295 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CFG, 0); 296 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_NCH, 0); 297 298 for (i = 0; i < phy_db->n_group_papd; i++) 299 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_PAPD, i); 300 if (phy_db->calib_ch_group_papd != NULL) 301 free(phy_db->calib_ch_group_papd, M_DEVBUF); 302 303 for (i = 0; i < phy_db->n_group_txp; i++) 304 iwm_phy_db_free_section(phy_db, IWM_PHY_DB_CALIB_CHG_TXP, i); 305 if (phy_db->calib_ch_group_txp != NULL) 306 free(phy_db->calib_ch_group_txp, M_DEVBUF); 307 308 free(phy_db, M_DEVBUF); 309} 310 311int 312iwm_phy_db_set_section(struct iwm_phy_db *phy_db, 313 struct iwm_calib_res_notif_phy_db *phy_db_notif) 314{ 315 enum iwm_phy_db_section_type type = le16toh(phy_db_notif->type); 316 uint16_t size = le16toh(phy_db_notif->length); 317 struct iwm_phy_db_entry *entry; 318 uint16_t chg_id = 0; 319 320 if (!phy_db) 321 return EINVAL; 322 323 if (type == IWM_PHY_DB_CALIB_CHG_PAPD) { 324 chg_id = le16toh(*(uint16_t *)phy_db_notif->data); 325 if (phy_db && !phy_db->calib_ch_group_papd) { 326 /* 327 * Firmware sends the largest index first, so we can use 328 * it to know how much we should allocate. 329 */ 330 phy_db->calib_ch_group_papd = malloc( 331 (chg_id + 1) * sizeof(struct iwm_phy_db_entry), 332 M_DEVBUF, M_NOWAIT | M_ZERO); 333 if (!phy_db->calib_ch_group_papd) 334 return ENOMEM; 335 phy_db->n_group_papd = chg_id + 1; 336 } 337 } else if (type == IWM_PHY_DB_CALIB_CHG_TXP) { 338 chg_id = le16toh(*(uint16_t *)phy_db_notif->data); 339 if (phy_db && !phy_db->calib_ch_group_txp) { 340 /* 341 * Firmware sends the largest index first, so we can use 342 * it to know how much we should allocate. 343 */ 344 phy_db->calib_ch_group_txp = malloc( 345 (chg_id + 1) * sizeof(struct iwm_phy_db_entry), 346 M_DEVBUF, M_NOWAIT | M_ZERO); 347 if (!phy_db->calib_ch_group_txp) 348 return ENOMEM; 349 phy_db->n_group_txp = chg_id + 1; 350 } 351 } 352 353 entry = iwm_phy_db_get_section(phy_db, type, chg_id); 354 if (!entry) 355 return EINVAL; 356 357 if (entry->data != NULL) 358 free(entry->data, M_DEVBUF); 359 entry->data = malloc(size, M_DEVBUF, M_NOWAIT); 360 if (!entry->data) { 361 entry->size = 0; 362 return ENOMEM; 363 } 364 memcpy(entry->data, phy_db_notif->data, size); 365 366 entry->size = size; 367 368 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET, 369 "%s(%d): [PHYDB]SET: Type %d , Size: %d\n", 370 __func__, __LINE__, type, size); 371 372 return 0; 373} 374 375static int 376is_valid_channel(uint16_t ch_id) 377{ 378 if (ch_id <= 14 || 379 (36 <= ch_id && ch_id <= 64 && ch_id % 4 == 0) || 380 (100 <= ch_id && ch_id <= 140 && ch_id % 4 == 0) || 381 (145 <= ch_id && ch_id <= 165 && ch_id % 4 == 1)) 382 return 1; 383 return 0; 384} 385 386static uint8_t 387ch_id_to_ch_index(uint16_t ch_id) 388{ 389 if (!is_valid_channel(ch_id)) 390 return 0xff; 391 392 if (ch_id <= 14) 393 return ch_id - 1; 394 if (ch_id <= 64) 395 return (ch_id + 20) / 4; 396 if (ch_id <= 140) 397 return (ch_id - 12) / 4; 398 return (ch_id - 13) / 4; 399} 400 401 402static uint16_t 403channel_id_to_papd(uint16_t ch_id) 404{ 405 if (!is_valid_channel(ch_id)) 406 return 0xff; 407 408 if (1 <= ch_id && ch_id <= 14) 409 return 0; 410 if (36 <= ch_id && ch_id <= 64) 411 return 1; 412 if (100 <= ch_id && ch_id <= 140) 413 return 2; 414 return 3; 415} 416 417static uint16_t 418channel_id_to_txp(struct iwm_phy_db *phy_db, uint16_t ch_id) 419{ 420 struct iwm_phy_db_chg_txp *txp_chg; 421 int i; 422 uint8_t ch_index = ch_id_to_ch_index(ch_id); 423 if (ch_index == 0xff) 424 return 0xff; 425 426 for (i = 0; i < phy_db->n_group_txp; i++) { 427 txp_chg = (void *)phy_db->calib_ch_group_txp[i].data; 428 if (!txp_chg) 429 return 0xff; 430 /* 431 * Looking for the first channel group that its max channel is 432 * higher then wanted channel. 433 */ 434 if (le16toh(txp_chg->max_channel_idx) >= ch_index) 435 return i; 436 } 437 return 0xff; 438} 439 440static int 441iwm_phy_db_get_section_data(struct iwm_phy_db *phy_db, 442 uint32_t type, uint8_t **data, uint16_t *size, 443 uint16_t ch_id) 444{ 445 struct iwm_phy_db_entry *entry; 446 uint16_t ch_group_id = 0; 447 448 if (!phy_db) 449 return EINVAL; 450 451 /* find wanted channel group */ 452 if (type == IWM_PHY_DB_CALIB_CHG_PAPD) 453 ch_group_id = channel_id_to_papd(ch_id); 454 else if (type == IWM_PHY_DB_CALIB_CHG_TXP) 455 ch_group_id = channel_id_to_txp(phy_db, ch_id); 456 457 entry = iwm_phy_db_get_section(phy_db, type, ch_group_id); 458 if (!entry) 459 return EINVAL; 460 461 *data = entry->data; 462 *size = entry->size; 463 464 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET, 465 "%s(%d): [PHYDB] GET: Type %d , Size: %d\n", 466 __func__, __LINE__, type, *size); 467 468 return 0; 469} 470 471static int 472iwm_send_phy_db_cmd(struct iwm_phy_db *phy_db, uint16_t type, 473 uint16_t length, void *data) 474{ 475 struct iwm_phy_db_cmd phy_db_cmd; 476 struct iwm_host_cmd cmd = { 477 .id = PHY_DB_CMD, 478 }; 479 480 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_RESET, 481 "Sending PHY-DB hcmd of type %d, of length %d\n", 482 type, length); 483 484 /* Set phy db cmd variables */ 485 phy_db_cmd.type = htole16(type); 486 phy_db_cmd.length = htole16(length); 487 488 /* Set hcmd variables */ 489 cmd.data[0] = &phy_db_cmd; 490 cmd.len[0] = sizeof(struct iwm_phy_db_cmd); 491 cmd.data[1] = data; 492 cmd.len[1] = length; 493#ifdef notyet 494 cmd.dataflags[1] = IWM_HCMD_DFL_NOCOPY; 495#endif 496 497 return iwm_send_cmd(phy_db->sc, &cmd); 498} 499 500static int 501iwm_phy_db_send_all_channel_groups(struct iwm_phy_db *phy_db, 502 enum iwm_phy_db_section_type type, 503 uint8_t max_ch_groups) 504{ 505 uint16_t i; 506 int err; 507 struct iwm_phy_db_entry *entry; 508 509 /* Send all the channel specific groups to operational fw */ 510 for (i = 0; i < max_ch_groups; i++) { 511 entry = iwm_phy_db_get_section(phy_db, 512 type, 513 i); 514 if (!entry) 515 return EINVAL; 516 517 if (!entry->size) 518 continue; 519 520 /* Send the requested PHY DB section */ 521 err = iwm_send_phy_db_cmd(phy_db, 522 type, 523 entry->size, 524 entry->data); 525 if (err) { 526 device_printf(phy_db->sc->sc_dev, 527 "Can't SEND phy_db section %d (%d), err %d\n", 528 type, i, err); 529 return err; 530 } 531 532 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD, 533 "Sent PHY_DB HCMD, type = %d num = %d\n", type, i); 534 } 535 536 return 0; 537} 538 539int 540iwm_send_phy_db_data(struct iwm_phy_db *phy_db) 541{ 542 uint8_t *data = NULL; 543 uint16_t size = 0; 544 int err; 545 546 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 547 "%s: Sending phy db data and configuration to runtime image\n", 548 __func__); 549 550 /* Send PHY DB CFG section */ 551 err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CFG, 552 &data, &size, 0); 553 if (err) { 554 device_printf(phy_db->sc->sc_dev, 555 "%s: Cannot get Phy DB cfg section, %d\n", 556 __func__, err); 557 return err; 558 } 559 560 err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CFG, size, data); 561 if (err) { 562 device_printf(phy_db->sc->sc_dev, 563 "%s: Cannot send HCMD of Phy DB cfg section, %d\n", 564 __func__, err); 565 return err; 566 } 567 568 err = iwm_phy_db_get_section_data(phy_db, IWM_PHY_DB_CALIB_NCH, 569 &data, &size, 0); 570 if (err) { 571 device_printf(phy_db->sc->sc_dev, 572 "%s: Cannot get Phy DB non specific channel section, " 573 "%d\n", __func__, err); 574 return err; 575 } 576 577 err = iwm_send_phy_db_cmd(phy_db, IWM_PHY_DB_CALIB_NCH, size, data); 578 if (err) { 579 device_printf(phy_db->sc->sc_dev, 580 "%s: Cannot send HCMD of Phy DB non specific channel " 581 "sect, %d\n", __func__, err); 582 return err; 583 } 584 585 /* Send all the TXP channel specific data */ 586 err = iwm_phy_db_send_all_channel_groups(phy_db, 587 IWM_PHY_DB_CALIB_CHG_PAPD, phy_db->n_group_papd); 588 if (err) { 589 device_printf(phy_db->sc->sc_dev, 590 "%s: Cannot send channel specific PAPD groups, %d\n", 591 __func__, err); 592 return err; 593 } 594 595 /* Send all the TXP channel specific data */ 596 err = iwm_phy_db_send_all_channel_groups(phy_db, 597 IWM_PHY_DB_CALIB_CHG_TXP, phy_db->n_group_txp); 598 if (err) { 599 device_printf(phy_db->sc->sc_dev, 600 "%s: Cannot send channel specific TX power groups, " 601 "%d\n", __func__, err); 602 return err; 603 } 604 605 IWM_DPRINTF(phy_db->sc, IWM_DEBUG_CMD | IWM_DEBUG_RESET, 606 "%s: Finished sending phy db non channel data\n", 607 __func__); 608 return 0; 609} 610