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