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