1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2005-2014, 2018-2026 Intel Corporation 4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH 5 * Copyright (C) 2016-2017 Intel Deutschland GmbH 6 */ 7 #include <linux/completion.h> 8 #include <linux/dma-mapping.h> 9 #include <linux/firmware.h> 10 #include <linux/module.h> 11 #include <linux/vmalloc.h> 12 13 #include "iwl-drv.h" 14 #include "iwl-csr.h" 15 #include "iwl-debug.h" 16 #include "iwl-trans.h" 17 #include "iwl-op-mode.h" 18 #include "iwl-agn-hw.h" 19 #include "fw/img.h" 20 #include "iwl-dbg-tlv.h" 21 #include "iwl-config.h" 22 #include "iwl-modparams.h" 23 #include "fw/api/alive.h" 24 #include "fw/api/mac.h" 25 #include "fw/api/mac-cfg.h" 26 27 /****************************************************************************** 28 * 29 * module boiler plate 30 * 31 ******************************************************************************/ 32 33 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi driver for Linux" 34 MODULE_DESCRIPTION(DRV_DESCRIPTION); 35 MODULE_LICENSE("GPL"); 36 37 #ifdef CONFIG_IWLWIFI_DEBUGFS 38 static struct dentry *iwl_dbgfs_root; 39 #endif 40 41 /** 42 * struct iwl_drv - drv common data 43 * @list: list of drv structures using this opmode 44 * @fw: the iwl_fw structure 45 * @op_mode: the running op_mode 46 * @trans: transport layer 47 * @dev: for debug prints only 48 * @fw_index: firmware revision to try loading 49 * @firmware_name: composite filename of ucode file to load 50 * @request_firmware_complete: the firmware has been obtained from user space 51 * @dbgfs_drv: debugfs root directory entry 52 * @dbgfs_trans: debugfs transport directory entry 53 * @dbgfs_op_mode: debugfs op_mode directory entry 54 */ 55 struct iwl_drv { 56 struct list_head list; 57 struct iwl_fw fw; 58 59 struct iwl_op_mode *op_mode; 60 struct iwl_trans *trans; 61 struct device *dev; 62 63 int fw_index; /* firmware we're trying to load */ 64 char firmware_name[64]; /* name of firmware file to load */ 65 66 struct completion request_firmware_complete; 67 68 #ifdef CONFIG_IWLWIFI_DEBUGFS 69 struct dentry *dbgfs_drv; 70 struct dentry *dbgfs_trans; 71 struct dentry *dbgfs_op_mode; 72 #endif 73 }; 74 75 enum { 76 DVM_OP_MODE, 77 MVM_OP_MODE, 78 #if IS_ENABLED(CONFIG_IWLMLD) 79 MLD_OP_MODE, 80 #endif 81 }; 82 83 /* Protects the table contents, i.e. the ops pointer & drv list */ 84 static DEFINE_MUTEX(iwlwifi_opmode_table_mtx); 85 static struct iwlwifi_opmode_table { 86 const char *name; /* name: iwldvm, iwlmvm, etc */ 87 const struct iwl_op_mode_ops *ops; /* pointer to op_mode ops */ 88 struct list_head drv; /* list of devices using this op_mode */ 89 } iwlwifi_opmode_table[] = { /* ops set when driver is initialized */ 90 [DVM_OP_MODE] = { .name = "iwldvm", .ops = NULL }, 91 [MVM_OP_MODE] = { .name = "iwlmvm", .ops = NULL }, 92 #if IS_ENABLED(CONFIG_IWLMLD) 93 [MLD_OP_MODE] = { .name = "iwlmld", .ops = NULL }, 94 #endif 95 }; 96 97 #define IWL_DEFAULT_SCAN_CHANNELS 40 98 99 /* 100 * struct fw_sec: Just for the image parsing process. 101 * For the fw storage we are using struct fw_desc. 102 */ 103 struct fw_sec { 104 const void *data; /* the sec data */ 105 size_t size; /* section size */ 106 u32 offset; /* offset of writing in the device */ 107 }; 108 109 static void iwl_free_fw_desc(struct iwl_drv *drv, struct fw_desc *desc) 110 { 111 vfree(desc->data); 112 desc->data = NULL; 113 desc->len = 0; 114 } 115 116 static void iwl_free_fw_img(struct iwl_drv *drv, struct fw_img *img) 117 { 118 int i; 119 for (i = 0; i < img->num_sec; i++) 120 iwl_free_fw_desc(drv, &img->sec[i]); 121 kfree(img->sec); 122 } 123 124 static void iwl_dealloc_ucode(struct iwl_drv *drv) 125 { 126 int i; 127 128 kfree(drv->fw.dbg.dest_tlv); 129 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++) 130 kfree(drv->fw.dbg.conf_tlv[i]); 131 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++) 132 kfree(drv->fw.dbg.trigger_tlv[i]); 133 kfree(drv->fw.dbg.mem_tlv); 134 kfree(drv->fw.iml); 135 kfree(drv->fw.ucode_capa.cmd_versions); 136 kfree(drv->fw.ucode_capa.cmd_bios_tables); 137 kfree(drv->fw.phy_integration_ver); 138 kfree(drv->trans->dbg.pc_data); 139 drv->trans->dbg.pc_data = NULL; 140 kvfree(drv->fw.pnvm_data); 141 drv->fw.pnvm_data = NULL; 142 drv->fw.pnvm_size = 0; 143 144 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++) 145 iwl_free_fw_img(drv, drv->fw.img + i); 146 147 /* clear the data for the aborted load case */ 148 memset(&drv->fw, 0, sizeof(drv->fw)); 149 } 150 151 static int iwl_alloc_fw_desc(struct fw_desc *desc, struct fw_sec *sec) 152 { 153 void *data; 154 155 desc->data = NULL; 156 157 if (!sec || !sec->size) 158 return -EINVAL; 159 160 data = vmalloc(sec->size); 161 if (!data) 162 return -ENOMEM; 163 164 desc->len = sec->size; 165 desc->offset = sec->offset; 166 memcpy(data, sec->data, desc->len); 167 desc->data = data; 168 169 return 0; 170 } 171 172 static inline char iwl_drv_get_step(int step) 173 { 174 if (step == SILICON_Z_STEP) 175 return 'z'; 176 if (step == SILICON_TC_STEP) 177 return 'a'; 178 return 'a' + step; 179 } 180 181 bool iwl_drv_is_wifi7_supported(struct iwl_trans *trans) 182 { 183 return trans->mac_cfg->device_family >= IWL_DEVICE_FAMILY_BZ && 184 CSR_HW_RFID_TYPE(trans->info.hw_rf_id) >= IWL_CFG_RF_TYPE_FM; 185 } 186 187 const char *iwl_drv_get_fwname_pre(struct iwl_trans *trans, char *buf) 188 { 189 char mac_step, rf_step; 190 const char *mac, *rf, *cdb; 191 192 if (trans->cfg->fw_name_pre) 193 return trans->cfg->fw_name_pre; 194 195 mac_step = iwl_drv_get_step(trans->info.hw_rev_step); 196 197 switch (CSR_HW_REV_TYPE(trans->info.hw_rev)) { 198 case IWL_CFG_MAC_TYPE_PU: 199 mac = "9000-pu"; 200 mac_step = 'b'; 201 break; 202 case IWL_CFG_MAC_TYPE_TH: 203 mac = "9260-th"; 204 mac_step = 'b'; 205 break; 206 case IWL_CFG_MAC_TYPE_QU: 207 mac = "Qu"; 208 break; 209 case IWL_CFG_MAC_TYPE_CC: 210 /* special case - no RF since it's fixed (discrete) */ 211 scnprintf(buf, FW_NAME_PRE_BUFSIZE, "iwlwifi-cc-a0"); 212 return buf; 213 case IWL_CFG_MAC_TYPE_QUZ: 214 mac = "QuZ"; 215 /* all QuZ use A0 firmware */ 216 mac_step = 'a'; 217 break; 218 case IWL_CFG_MAC_TYPE_SO: 219 case IWL_CFG_MAC_TYPE_SOF: 220 mac = "so"; 221 mac_step = 'a'; 222 break; 223 case IWL_CFG_MAC_TYPE_TY: 224 mac = "ty"; 225 mac_step = 'a'; 226 break; 227 case IWL_CFG_MAC_TYPE_MA: 228 mac = "ma"; 229 break; 230 case IWL_CFG_MAC_TYPE_BZ: 231 case IWL_CFG_MAC_TYPE_BZ_W: 232 mac = "bz"; 233 break; 234 case IWL_CFG_MAC_TYPE_GL: 235 mac = "gl"; 236 break; 237 case IWL_CFG_MAC_TYPE_SC: 238 mac = "sc"; 239 break; 240 case IWL_CFG_MAC_TYPE_SC2: 241 /* Uses the same firmware as SC2 */ 242 case IWL_CFG_MAC_TYPE_SC2F: 243 mac = "sc2"; 244 break; 245 case IWL_CFG_MAC_TYPE_BR: 246 mac = "br"; 247 break; 248 case IWL_CFG_MAC_TYPE_DR: 249 mac = "dr"; 250 break; 251 default: 252 return "unknown-mac"; 253 } 254 255 rf_step = iwl_drv_get_step(CSR_HW_RFID_STEP(trans->info.hw_rf_id)); 256 257 switch (CSR_HW_RFID_TYPE(trans->info.hw_rf_id)) { 258 case IWL_CFG_RF_TYPE_JF1: 259 case IWL_CFG_RF_TYPE_JF2: 260 rf = "jf"; 261 rf_step = 'b'; 262 break; 263 case IWL_CFG_RF_TYPE_HR1: 264 case IWL_CFG_RF_TYPE_HR2: 265 rf = "hr"; 266 rf_step = 'b'; 267 break; 268 case IWL_CFG_RF_TYPE_GF: 269 rf = "gf"; 270 rf_step = 'a'; 271 break; 272 case IWL_CFG_RF_TYPE_FM: 273 rf = "fm"; 274 break; 275 case IWL_CFG_RF_TYPE_WH: 276 rf = "wh"; 277 break; 278 case IWL_CFG_RF_TYPE_PE: 279 rf = "pe"; 280 break; 281 default: 282 return "unknown-rf"; 283 } 284 285 cdb = CSR_HW_RFID_IS_CDB(trans->info.hw_rf_id) ? "4" : ""; 286 287 scnprintf(buf, FW_NAME_PRE_BUFSIZE, 288 "iwlwifi-%s-%c0-%s%s-%c0", 289 mac, mac_step, rf, cdb, rf_step); 290 291 return buf; 292 } 293 IWL_EXPORT_SYMBOL(iwl_drv_get_fwname_pre); 294 295 static void iwl_req_fw_callback(const struct firmware *ucode_raw, 296 void *context); 297 298 static void iwl_get_ucode_api_versions(struct iwl_trans *trans, 299 unsigned int *api_min, 300 unsigned int *api_max) 301 { 302 const struct iwl_family_base_params *base = trans->mac_cfg->base; 303 const struct iwl_rf_cfg *cfg = trans->cfg; 304 305 /* if the MAC doesn't have range or if its range it higher than the RF's */ 306 if (!base->ucode_api_max || 307 (cfg->ucode_api_max && base->ucode_api_min > cfg->ucode_api_max)) { 308 *api_min = cfg->ucode_api_min; 309 *api_max = cfg->ucode_api_max; 310 return; 311 } 312 313 /* if the RF doesn't have range or if its range it higher than the MAC's */ 314 if (!cfg->ucode_api_max || 315 (base->ucode_api_max && cfg->ucode_api_min > base->ucode_api_max)) { 316 *api_min = base->ucode_api_min; 317 *api_max = base->ucode_api_max; 318 return; 319 } 320 321 *api_min = max(cfg->ucode_api_min, base->ucode_api_min); 322 *api_max = min(cfg->ucode_api_max, base->ucode_api_max); 323 } 324 325 static int iwl_request_firmware(struct iwl_drv *drv, bool first) 326 { 327 char _fw_name_pre[FW_NAME_PRE_BUFSIZE]; 328 unsigned int ucode_api_max, ucode_api_min; 329 const char *fw_name_pre; 330 331 iwl_get_ucode_api_versions(drv->trans, &ucode_api_min, &ucode_api_max); 332 333 if (drv->trans->mac_cfg->device_family == IWL_DEVICE_FAMILY_9000 && 334 (drv->trans->info.hw_rev_step != SILICON_B_STEP && 335 drv->trans->info.hw_rev_step != SILICON_C_STEP)) { 336 IWL_ERR(drv, 337 "Only HW steps B and C are currently supported (0x%0x)\n", 338 drv->trans->info.hw_rev); 339 return -EINVAL; 340 } 341 342 if (CSR_HW_RFID_TYPE(drv->trans->info.hw_rf_id) == IWL_CFG_RF_TYPE_WH && 343 CSR_HW_RFID_STEP(drv->trans->info.hw_rf_id) == SILICON_A_STEP) { 344 IWL_ERR(drv, "WH A step is not supported\n"); 345 return -EINVAL; 346 } 347 348 fw_name_pre = iwl_drv_get_fwname_pre(drv->trans, _fw_name_pre); 349 350 if (first) 351 drv->fw_index = ucode_api_max; 352 else if (drv->fw_index == ENCODE_CORE_AS_API(100)) 353 drv->fw_index = 102; /* last API-scheme number below core 100 */ 354 else 355 drv->fw_index--; 356 357 if (drv->fw_index < ucode_api_min) { 358 IWL_ERR(drv, "no suitable firmware found!\n"); 359 360 if (ucode_api_min == ucode_api_max) { 361 IWL_ERR(drv, "%s-" FW_API_FMT " is required\n", 362 fw_name_pre, FW_API_ARG(ucode_api_max)); 363 } else { 364 IWL_ERR(drv, 365 "minimum version required: %s-" FW_API_FMT "\n", 366 fw_name_pre, FW_API_ARG(ucode_api_min)); 367 IWL_ERR(drv, 368 "maximum version supported: %s-" FW_API_FMT "\n", 369 fw_name_pre, FW_API_ARG(ucode_api_max)); 370 } 371 372 IWL_ERR(drv, 373 "check git://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git\n"); 374 return -ENOENT; 375 } 376 377 snprintf(drv->firmware_name, sizeof(drv->firmware_name), 378 "%s-" FW_API_FMT ".ucode", 379 fw_name_pre, FW_API_ARG(drv->fw_index)); 380 381 IWL_DEBUG_FW_INFO(drv, "attempting to load firmware '%s'\n", 382 drv->firmware_name); 383 384 return request_firmware_nowait(THIS_MODULE, 1, drv->firmware_name, 385 drv->trans->dev, 386 GFP_KERNEL, drv, iwl_req_fw_callback); 387 } 388 389 struct fw_img_parsing { 390 struct fw_sec *sec; 391 int sec_counter; 392 }; 393 394 /* 395 * struct fw_sec_parsing: to extract fw section and it's offset from tlv 396 */ 397 struct fw_sec_parsing { 398 __le32 offset; 399 const u8 data[]; 400 } __packed; 401 402 /** 403 * struct iwl_tlv_calib_data - parse the default calib data from TLV 404 * 405 * @ucode_type: the uCode to which the following default calib relates. 406 * @calib: default calibrations. 407 */ 408 struct iwl_tlv_calib_data { 409 __le32 ucode_type; 410 struct iwl_tlv_calib_ctrl calib; 411 } __packed; 412 413 struct iwl_firmware_pieces { 414 struct fw_img_parsing img[IWL_UCODE_TYPE_MAX]; 415 416 u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr; 417 u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr; 418 419 /* FW debug data parsed for driver usage */ 420 bool dbg_dest_tlv_init; 421 const u8 *dbg_dest_ver; 422 union { 423 const struct iwl_fw_dbg_dest_tlv *dbg_dest_tlv; 424 const struct iwl_fw_dbg_dest_tlv_v1 *dbg_dest_tlv_v1; 425 }; 426 const struct iwl_fw_dbg_conf_tlv *dbg_conf_tlv[FW_DBG_CONF_MAX]; 427 size_t dbg_conf_tlv_len[FW_DBG_CONF_MAX]; 428 const struct iwl_fw_dbg_trigger_tlv *dbg_trigger_tlv[FW_DBG_TRIGGER_MAX]; 429 size_t dbg_trigger_tlv_len[FW_DBG_TRIGGER_MAX]; 430 struct iwl_fw_dbg_mem_seg_tlv *dbg_mem_tlv; 431 size_t n_mem_tlv; 432 }; 433 434 static void alloc_sec_data(struct iwl_firmware_pieces *pieces, 435 enum iwl_ucode_type type, 436 int sec) 437 { 438 struct fw_img_parsing *img = &pieces->img[type]; 439 struct fw_sec *sec_memory; 440 int size = sec + 1; 441 size_t alloc_size = sizeof(*img->sec) * size; 442 443 if (img->sec && img->sec_counter >= size) 444 return; 445 446 sec_memory = krealloc(img->sec, alloc_size, GFP_KERNEL); 447 if (!sec_memory) 448 return; 449 450 img->sec = sec_memory; 451 img->sec_counter = size; 452 } 453 454 static void set_sec_data(struct iwl_firmware_pieces *pieces, 455 enum iwl_ucode_type type, 456 int sec, 457 const void *data) 458 { 459 alloc_sec_data(pieces, type, sec); 460 461 pieces->img[type].sec[sec].data = data; 462 } 463 464 static void set_sec_size(struct iwl_firmware_pieces *pieces, 465 enum iwl_ucode_type type, 466 int sec, 467 size_t size) 468 { 469 alloc_sec_data(pieces, type, sec); 470 471 pieces->img[type].sec[sec].size = size; 472 } 473 474 static size_t get_sec_size(struct iwl_firmware_pieces *pieces, 475 enum iwl_ucode_type type, 476 int sec) 477 { 478 return pieces->img[type].sec[sec].size; 479 } 480 481 static void set_sec_offset(struct iwl_firmware_pieces *pieces, 482 enum iwl_ucode_type type, 483 int sec, 484 u32 offset) 485 { 486 alloc_sec_data(pieces, type, sec); 487 488 pieces->img[type].sec[sec].offset = offset; 489 } 490 491 /* 492 * Gets uCode section from tlv. 493 */ 494 static int iwl_store_ucode_sec(struct fw_img_parsing *img, 495 const void *data, size_t size) 496 { 497 struct fw_sec *sec; 498 const struct fw_sec_parsing *sec_parse; 499 size_t alloc_size; 500 501 if (WARN_ON(!img || !data)) 502 return -EINVAL; 503 504 if (size < sizeof(sec_parse->offset)) 505 return -EINVAL; 506 507 sec_parse = (const struct fw_sec_parsing *)data; 508 509 alloc_size = sizeof(*img->sec) * (img->sec_counter + 1); 510 sec = krealloc(img->sec, alloc_size, GFP_KERNEL); 511 if (!sec) 512 return -ENOMEM; 513 img->sec = sec; 514 515 sec = &img->sec[img->sec_counter]; 516 517 sec->offset = le32_to_cpu(sec_parse->offset); 518 sec->data = sec_parse->data; 519 sec->size = size - sizeof(sec_parse->offset); 520 521 ++img->sec_counter; 522 523 return 0; 524 } 525 526 static int iwl_set_default_calib(struct iwl_drv *drv, const u8 *data) 527 { 528 const struct iwl_tlv_calib_data *def_calib = 529 (const struct iwl_tlv_calib_data *)data; 530 u32 ucode_type = le32_to_cpu(def_calib->ucode_type); 531 if (ucode_type >= IWL_UCODE_TYPE_MAX) { 532 IWL_ERR(drv, "Wrong ucode_type %u for default calibration.\n", 533 ucode_type); 534 return -EINVAL; 535 } 536 drv->fw.default_calib[ucode_type].flow_trigger = 537 def_calib->calib.flow_trigger; 538 drv->fw.default_calib[ucode_type].event_trigger = 539 def_calib->calib.event_trigger; 540 541 return 0; 542 } 543 544 static void iwl_set_ucode_api_flags(struct iwl_drv *drv, const u8 *data, 545 struct iwl_ucode_capabilities *capa) 546 { 547 const struct iwl_ucode_api *ucode_api = (const void *)data; 548 u32 api_index = le32_to_cpu(ucode_api->api_index); 549 u32 api_flags = le32_to_cpu(ucode_api->api_flags); 550 int i; 551 552 if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_API, 32)) { 553 IWL_WARN(drv, 554 "api flags index %d larger than supported by driver\n", 555 api_index); 556 return; 557 } 558 559 for (i = 0; i < 32; i++) { 560 if (api_flags & BIT(i)) 561 __set_bit(i + 32 * api_index, capa->_api); 562 } 563 } 564 565 static void iwl_set_ucode_capabilities(struct iwl_drv *drv, const u8 *data, 566 struct iwl_ucode_capabilities *capa) 567 { 568 const struct iwl_ucode_capa *ucode_capa = (const void *)data; 569 u32 api_index = le32_to_cpu(ucode_capa->api_index); 570 u32 api_flags = le32_to_cpu(ucode_capa->api_capa); 571 int i; 572 573 if (api_index >= DIV_ROUND_UP(NUM_IWL_UCODE_TLV_CAPA, 32)) { 574 IWL_WARN(drv, 575 "capa flags index %d larger than supported by driver\n", 576 api_index); 577 return; 578 } 579 580 for (i = 0; i < 32; i++) { 581 if (api_flags & BIT(i)) 582 __set_bit(i + 32 * api_index, capa->_capa); 583 } 584 } 585 586 static const char *iwl_reduced_fw_name(struct iwl_drv *drv) 587 { 588 const char *name = drv->firmware_name; 589 590 if (strncmp(name, "iwlwifi-", 8) == 0) 591 name += 8; 592 593 return name; 594 } 595 596 static int iwl_parse_v1_v2_firmware(struct iwl_drv *drv, 597 const struct firmware *ucode_raw, 598 struct iwl_firmware_pieces *pieces) 599 { 600 const struct iwl_ucode_header *ucode = (const void *)ucode_raw->data; 601 u32 api_ver, hdr_size, build; 602 char buildstr[25]; 603 const u8 *src; 604 605 drv->fw.ucode_ver = le32_to_cpu(ucode->ver); 606 api_ver = IWL_UCODE_API(drv->fw.ucode_ver); 607 608 switch (api_ver) { 609 default: 610 hdr_size = 28; 611 if (ucode_raw->size < hdr_size) { 612 IWL_ERR(drv, "File size too small!\n"); 613 return -EINVAL; 614 } 615 build = le32_to_cpu(ucode->u.v2.build); 616 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, 617 le32_to_cpu(ucode->u.v2.inst_size)); 618 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, 619 le32_to_cpu(ucode->u.v2.data_size)); 620 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, 621 le32_to_cpu(ucode->u.v2.init_size)); 622 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, 623 le32_to_cpu(ucode->u.v2.init_data_size)); 624 src = ucode->u.v2.data; 625 break; 626 case 0: 627 case 1: 628 case 2: 629 hdr_size = 24; 630 if (ucode_raw->size < hdr_size) { 631 IWL_ERR(drv, "File size too small!\n"); 632 return -EINVAL; 633 } 634 build = 0; 635 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, 636 le32_to_cpu(ucode->u.v1.inst_size)); 637 set_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, 638 le32_to_cpu(ucode->u.v1.data_size)); 639 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, 640 le32_to_cpu(ucode->u.v1.init_size)); 641 set_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, 642 le32_to_cpu(ucode->u.v1.init_data_size)); 643 src = ucode->u.v1.data; 644 break; 645 } 646 647 if (build) 648 sprintf(buildstr, " build %u", build); 649 else 650 buildstr[0] = '\0'; 651 652 snprintf(drv->fw.fw_version, 653 sizeof(drv->fw.fw_version), 654 "%u.%u.%u.%u%s %s", 655 IWL_UCODE_MAJOR(drv->fw.ucode_ver), 656 IWL_UCODE_MINOR(drv->fw.ucode_ver), 657 IWL_UCODE_API(drv->fw.ucode_ver), 658 IWL_UCODE_SERIAL(drv->fw.ucode_ver), 659 buildstr, iwl_reduced_fw_name(drv)); 660 661 /* Verify size of file vs. image size info in file's header */ 662 663 if (ucode_raw->size != hdr_size + 664 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) + 665 get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) + 666 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) + 667 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)) { 668 669 IWL_ERR(drv, 670 "uCode file size %d does not match expected size\n", 671 (int)ucode_raw->size); 672 return -EINVAL; 673 } 674 675 676 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, src); 677 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST); 678 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST, 679 IWLAGN_RTC_INST_LOWER_BOUND); 680 set_sec_data(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, src); 681 src += get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA); 682 set_sec_offset(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA, 683 IWLAGN_RTC_DATA_LOWER_BOUND); 684 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, src); 685 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST); 686 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST, 687 IWLAGN_RTC_INST_LOWER_BOUND); 688 set_sec_data(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, src); 689 src += get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA); 690 set_sec_offset(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA, 691 IWLAGN_RTC_DATA_LOWER_BOUND); 692 return 0; 693 } 694 695 static void iwl_drv_set_dump_exclude(struct iwl_drv *drv, 696 enum iwl_ucode_tlv_type tlv_type, 697 const void *tlv_data, u32 tlv_len) 698 { 699 const struct iwl_fw_dump_exclude *fw = tlv_data; 700 struct iwl_dump_exclude *excl; 701 702 if (tlv_len < sizeof(*fw)) 703 return; 704 705 if (tlv_type == IWL_UCODE_TLV_SEC_TABLE_ADDR) { 706 excl = &drv->fw.dump_excl[0]; 707 708 /* second time we find this, it's for WoWLAN */ 709 if (excl->addr) 710 excl = &drv->fw.dump_excl_wowlan[0]; 711 } else if (fw_has_capa(&drv->fw.ucode_capa, 712 IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG)) { 713 /* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is regular image */ 714 excl = &drv->fw.dump_excl[0]; 715 } else { 716 /* IWL_UCODE_TLV_D3_KEK_KCK_ADDR is WoWLAN image */ 717 excl = &drv->fw.dump_excl_wowlan[0]; 718 } 719 720 if (excl->addr) 721 excl++; 722 723 if (excl->addr) { 724 IWL_DEBUG_FW_INFO(drv, "found too many excludes in fw file\n"); 725 return; 726 } 727 728 excl->addr = le32_to_cpu(fw->addr) & ~FW_ADDR_CACHE_CONTROL; 729 excl->size = le32_to_cpu(fw->size); 730 } 731 732 static void iwl_parse_dbg_tlv_assert_tables(struct iwl_drv *drv, 733 const struct iwl_ucode_tlv *tlv) 734 { 735 const struct iwl_fw_ini_region_tlv *region; 736 u32 length = le32_to_cpu(tlv->length); 737 u32 addr; 738 739 if (length < offsetof(typeof(*region), special_mem) + 740 sizeof(region->special_mem)) 741 return; 742 743 region = (const void *)tlv->data; 744 addr = le32_to_cpu(region->special_mem.base_addr); 745 addr += le32_to_cpu(region->special_mem.offset); 746 addr &= ~FW_ADDR_CACHE_CONTROL; 747 748 if (region->type != IWL_FW_INI_REGION_SPECIAL_DEVICE_MEMORY) 749 return; 750 751 switch (region->sub_type) { 752 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_UMAC_ERROR_TABLE: 753 drv->trans->dbg.umac_error_event_table = addr; 754 drv->trans->dbg.error_event_table_tlv_status |= 755 IWL_ERROR_EVENT_TABLE_UMAC; 756 break; 757 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_1_ERROR_TABLE: 758 drv->trans->dbg.lmac_error_event_table[0] = addr; 759 drv->trans->dbg.error_event_table_tlv_status |= 760 IWL_ERROR_EVENT_TABLE_LMAC1; 761 break; 762 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_LMAC_2_ERROR_TABLE: 763 drv->trans->dbg.lmac_error_event_table[1] = addr; 764 drv->trans->dbg.error_event_table_tlv_status |= 765 IWL_ERROR_EVENT_TABLE_LMAC2; 766 break; 767 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_TCM_1_ERROR_TABLE: 768 drv->trans->dbg.tcm_error_event_table[0] = addr; 769 drv->trans->dbg.error_event_table_tlv_status |= 770 IWL_ERROR_EVENT_TABLE_TCM1; 771 break; 772 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_TCM_2_ERROR_TABLE: 773 drv->trans->dbg.tcm_error_event_table[1] = addr; 774 drv->trans->dbg.error_event_table_tlv_status |= 775 IWL_ERROR_EVENT_TABLE_TCM2; 776 break; 777 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_RCM_1_ERROR_TABLE: 778 drv->trans->dbg.rcm_error_event_table[0] = addr; 779 drv->trans->dbg.error_event_table_tlv_status |= 780 IWL_ERROR_EVENT_TABLE_RCM1; 781 break; 782 case IWL_FW_INI_REGION_DEVICE_MEMORY_SUBTYPE_RCM_2_ERROR_TABLE: 783 drv->trans->dbg.rcm_error_event_table[1] = addr; 784 drv->trans->dbg.error_event_table_tlv_status |= 785 IWL_ERROR_EVENT_TABLE_RCM2; 786 break; 787 default: 788 break; 789 } 790 } 791 792 static int iwl_parse_tlv_firmware(struct iwl_drv *drv, 793 const struct firmware *ucode_raw, 794 struct iwl_firmware_pieces *pieces, 795 struct iwl_ucode_capabilities *capa, 796 bool *usniffer_images) 797 { 798 const struct iwl_tlv_ucode_header *ucode = (const void *)ucode_raw->data; 799 const struct iwl_ucode_tlv *tlv; 800 size_t len = ucode_raw->size; 801 const u8 *data; 802 u32 tlv_len; 803 u32 usniffer_img; 804 enum iwl_ucode_tlv_type tlv_type; 805 const u8 *tlv_data; 806 char buildstr[25]; 807 u32 build, paging_mem_size; 808 int num_of_cpus; 809 bool usniffer_req = false; 810 size_t aligned_tlv_len; 811 812 if (len < sizeof(*ucode)) { 813 IWL_ERR(drv, "uCode has invalid length: %zd\n", len); 814 return -EINVAL; 815 } 816 817 if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) { 818 IWL_ERR(drv, "invalid uCode magic: 0X%x\n", 819 le32_to_cpu(ucode->magic)); 820 return -EINVAL; 821 } 822 823 drv->fw.ucode_ver = le32_to_cpu(ucode->ver); 824 memcpy(drv->fw.human_readable, ucode->human_readable, 825 sizeof(drv->fw.human_readable)); 826 build = le32_to_cpu(ucode->build); 827 828 if (build) 829 sprintf(buildstr, " build %u", build); 830 else 831 buildstr[0] = '\0'; 832 833 snprintf(drv->fw.fw_version, 834 sizeof(drv->fw.fw_version), 835 "%u.%u.%u.%u%s %s", 836 IWL_UCODE_MAJOR(drv->fw.ucode_ver), 837 IWL_UCODE_MINOR(drv->fw.ucode_ver), 838 IWL_UCODE_API(drv->fw.ucode_ver), 839 IWL_UCODE_SERIAL(drv->fw.ucode_ver), 840 buildstr, iwl_reduced_fw_name(drv)); 841 842 data = ucode->data; 843 844 len -= sizeof(*ucode); 845 846 while (len >= sizeof(*tlv)) { 847 len -= sizeof(*tlv); 848 849 tlv = (const void *)data; 850 tlv_len = le32_to_cpu(tlv->length); 851 tlv_type = le32_to_cpu(tlv->type); 852 tlv_data = tlv->data; 853 854 if (len < tlv_len) { 855 IWL_ERR(drv, "invalid TLV len: %zd/%u\n", 856 len, tlv_len); 857 return -EINVAL; 858 } 859 860 aligned_tlv_len = ALIGN(tlv_len, 4); 861 if (len < aligned_tlv_len) { 862 IWL_ERR(drv, "invalid aligned TLV len: %zd/%zu\n", 863 len, aligned_tlv_len); 864 return -EINVAL; 865 } 866 867 len -= aligned_tlv_len; 868 data += sizeof(*tlv) + aligned_tlv_len; 869 870 switch (tlv_type) { 871 case IWL_UCODE_TLV_INST: 872 set_sec_data(pieces, IWL_UCODE_REGULAR, 873 IWL_UCODE_SECTION_INST, tlv_data); 874 set_sec_size(pieces, IWL_UCODE_REGULAR, 875 IWL_UCODE_SECTION_INST, tlv_len); 876 set_sec_offset(pieces, IWL_UCODE_REGULAR, 877 IWL_UCODE_SECTION_INST, 878 IWLAGN_RTC_INST_LOWER_BOUND); 879 break; 880 case IWL_UCODE_TLV_DATA: 881 set_sec_data(pieces, IWL_UCODE_REGULAR, 882 IWL_UCODE_SECTION_DATA, tlv_data); 883 set_sec_size(pieces, IWL_UCODE_REGULAR, 884 IWL_UCODE_SECTION_DATA, tlv_len); 885 set_sec_offset(pieces, IWL_UCODE_REGULAR, 886 IWL_UCODE_SECTION_DATA, 887 IWLAGN_RTC_DATA_LOWER_BOUND); 888 break; 889 case IWL_UCODE_TLV_INIT: 890 set_sec_data(pieces, IWL_UCODE_INIT, 891 IWL_UCODE_SECTION_INST, tlv_data); 892 set_sec_size(pieces, IWL_UCODE_INIT, 893 IWL_UCODE_SECTION_INST, tlv_len); 894 set_sec_offset(pieces, IWL_UCODE_INIT, 895 IWL_UCODE_SECTION_INST, 896 IWLAGN_RTC_INST_LOWER_BOUND); 897 break; 898 case IWL_UCODE_TLV_INIT_DATA: 899 set_sec_data(pieces, IWL_UCODE_INIT, 900 IWL_UCODE_SECTION_DATA, tlv_data); 901 set_sec_size(pieces, IWL_UCODE_INIT, 902 IWL_UCODE_SECTION_DATA, tlv_len); 903 set_sec_offset(pieces, IWL_UCODE_INIT, 904 IWL_UCODE_SECTION_DATA, 905 IWLAGN_RTC_DATA_LOWER_BOUND); 906 break; 907 case IWL_UCODE_TLV_BOOT: 908 IWL_ERR(drv, "Found unexpected BOOT ucode\n"); 909 break; 910 case IWL_UCODE_TLV_PROBE_MAX_LEN: 911 if (tlv_len != sizeof(u32)) 912 goto invalid_tlv_len; 913 capa->max_probe_length = 914 le32_to_cpup((const __le32 *)tlv_data); 915 break; 916 case IWL_UCODE_TLV_PAN: 917 if (tlv_len) 918 goto invalid_tlv_len; 919 capa->flags |= IWL_UCODE_TLV_FLAGS_PAN; 920 break; 921 case IWL_UCODE_TLV_FLAGS: 922 /* must be at least one u32 */ 923 if (tlv_len < sizeof(u32)) 924 goto invalid_tlv_len; 925 /* and a proper number of u32s */ 926 if (tlv_len % sizeof(u32)) 927 goto invalid_tlv_len; 928 /* 929 * This driver only reads the first u32 as 930 * right now no more features are defined, 931 * if that changes then either the driver 932 * will not work with the new firmware, or 933 * it'll not take advantage of new features. 934 */ 935 capa->flags = le32_to_cpup((const __le32 *)tlv_data); 936 break; 937 case IWL_UCODE_TLV_API_CHANGES_SET: 938 if (tlv_len != sizeof(struct iwl_ucode_api)) 939 goto invalid_tlv_len; 940 iwl_set_ucode_api_flags(drv, tlv_data, capa); 941 break; 942 case IWL_UCODE_TLV_ENABLED_CAPABILITIES: 943 if (tlv_len != sizeof(struct iwl_ucode_capa)) 944 goto invalid_tlv_len; 945 iwl_set_ucode_capabilities(drv, tlv_data, capa); 946 break; 947 case IWL_UCODE_TLV_INIT_EVTLOG_PTR: 948 if (tlv_len != sizeof(u32)) 949 goto invalid_tlv_len; 950 pieces->init_evtlog_ptr = 951 le32_to_cpup((const __le32 *)tlv_data); 952 break; 953 case IWL_UCODE_TLV_INIT_EVTLOG_SIZE: 954 if (tlv_len != sizeof(u32)) 955 goto invalid_tlv_len; 956 pieces->init_evtlog_size = 957 le32_to_cpup((const __le32 *)tlv_data); 958 break; 959 case IWL_UCODE_TLV_INIT_ERRLOG_PTR: 960 if (tlv_len != sizeof(u32)) 961 goto invalid_tlv_len; 962 pieces->init_errlog_ptr = 963 le32_to_cpup((const __le32 *)tlv_data); 964 break; 965 case IWL_UCODE_TLV_RUNT_EVTLOG_PTR: 966 if (tlv_len != sizeof(u32)) 967 goto invalid_tlv_len; 968 pieces->inst_evtlog_ptr = 969 le32_to_cpup((const __le32 *)tlv_data); 970 break; 971 case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE: 972 if (tlv_len != sizeof(u32)) 973 goto invalid_tlv_len; 974 pieces->inst_evtlog_size = 975 le32_to_cpup((const __le32 *)tlv_data); 976 break; 977 case IWL_UCODE_TLV_RUNT_ERRLOG_PTR: 978 if (tlv_len != sizeof(u32)) 979 goto invalid_tlv_len; 980 pieces->inst_errlog_ptr = 981 le32_to_cpup((const __le32 *)tlv_data); 982 break; 983 case IWL_UCODE_TLV_ENHANCE_SENS_TBL: 984 if (tlv_len) 985 goto invalid_tlv_len; 986 drv->fw.enhance_sensitivity_table = true; 987 break; 988 case IWL_UCODE_TLV_WOWLAN_INST: 989 set_sec_data(pieces, IWL_UCODE_WOWLAN, 990 IWL_UCODE_SECTION_INST, tlv_data); 991 set_sec_size(pieces, IWL_UCODE_WOWLAN, 992 IWL_UCODE_SECTION_INST, tlv_len); 993 set_sec_offset(pieces, IWL_UCODE_WOWLAN, 994 IWL_UCODE_SECTION_INST, 995 IWLAGN_RTC_INST_LOWER_BOUND); 996 break; 997 case IWL_UCODE_TLV_WOWLAN_DATA: 998 set_sec_data(pieces, IWL_UCODE_WOWLAN, 999 IWL_UCODE_SECTION_DATA, tlv_data); 1000 set_sec_size(pieces, IWL_UCODE_WOWLAN, 1001 IWL_UCODE_SECTION_DATA, tlv_len); 1002 set_sec_offset(pieces, IWL_UCODE_WOWLAN, 1003 IWL_UCODE_SECTION_DATA, 1004 IWLAGN_RTC_DATA_LOWER_BOUND); 1005 break; 1006 case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE: 1007 if (tlv_len != sizeof(u32)) 1008 goto invalid_tlv_len; 1009 capa->standard_phy_calibration_size = 1010 le32_to_cpup((const __le32 *)tlv_data); 1011 break; 1012 case IWL_UCODE_TLV_SEC_RT: 1013 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_REGULAR], 1014 tlv_data, tlv_len); 1015 drv->fw.type = IWL_FW_MVM; 1016 break; 1017 case IWL_UCODE_TLV_SEC_INIT: 1018 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_INIT], 1019 tlv_data, tlv_len); 1020 drv->fw.type = IWL_FW_MVM; 1021 break; 1022 case IWL_UCODE_TLV_SEC_WOWLAN: 1023 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_WOWLAN], 1024 tlv_data, tlv_len); 1025 drv->fw.type = IWL_FW_MVM; 1026 break; 1027 case IWL_UCODE_TLV_DEF_CALIB: 1028 if (tlv_len != sizeof(struct iwl_tlv_calib_data)) 1029 goto invalid_tlv_len; 1030 if (iwl_set_default_calib(drv, tlv_data)) 1031 goto tlv_error; 1032 break; 1033 case IWL_UCODE_TLV_PHY_SKU: 1034 if (tlv_len != sizeof(u32)) 1035 goto invalid_tlv_len; 1036 drv->fw.phy_config = le32_to_cpup((const __le32 *)tlv_data); 1037 drv->fw.valid_tx_ant = (drv->fw.phy_config & 1038 FW_PHY_CFG_TX_CHAIN) >> 1039 FW_PHY_CFG_TX_CHAIN_POS; 1040 drv->fw.valid_rx_ant = (drv->fw.phy_config & 1041 FW_PHY_CFG_RX_CHAIN) >> 1042 FW_PHY_CFG_RX_CHAIN_POS; 1043 break; 1044 case IWL_UCODE_TLV_SECURE_SEC_RT: 1045 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_REGULAR], 1046 tlv_data, tlv_len); 1047 drv->fw.type = IWL_FW_MVM; 1048 break; 1049 case IWL_UCODE_TLV_SECURE_SEC_INIT: 1050 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_INIT], 1051 tlv_data, tlv_len); 1052 drv->fw.type = IWL_FW_MVM; 1053 break; 1054 case IWL_UCODE_TLV_SECURE_SEC_WOWLAN: 1055 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_WOWLAN], 1056 tlv_data, tlv_len); 1057 drv->fw.type = IWL_FW_MVM; 1058 break; 1059 case IWL_UCODE_TLV_NUM_OF_CPU: 1060 if (tlv_len != sizeof(u32)) 1061 goto invalid_tlv_len; 1062 num_of_cpus = 1063 le32_to_cpup((const __le32 *)tlv_data); 1064 1065 if (num_of_cpus == 2) { 1066 drv->fw.img[IWL_UCODE_REGULAR].is_dual_cpus = 1067 true; 1068 drv->fw.img[IWL_UCODE_INIT].is_dual_cpus = 1069 true; 1070 drv->fw.img[IWL_UCODE_WOWLAN].is_dual_cpus = 1071 true; 1072 } else if ((num_of_cpus > 2) || (num_of_cpus < 1)) { 1073 IWL_ERR(drv, "Driver support up to 2 CPUs\n"); 1074 return -EINVAL; 1075 } 1076 break; 1077 case IWL_UCODE_TLV_N_SCAN_CHANNELS: 1078 if (tlv_len != sizeof(u32)) 1079 goto invalid_tlv_len; 1080 capa->n_scan_channels = 1081 le32_to_cpup((const __le32 *)tlv_data); 1082 break; 1083 case IWL_UCODE_TLV_FW_VERSION: { 1084 const __le32 *ptr = (const void *)tlv_data; 1085 u32 major, minor; 1086 u8 local_comp; 1087 1088 if (tlv_len != sizeof(u32) * 3) 1089 goto invalid_tlv_len; 1090 1091 major = le32_to_cpup(ptr++); 1092 minor = le32_to_cpup(ptr++); 1093 local_comp = le32_to_cpup(ptr); 1094 1095 snprintf(drv->fw.fw_version, 1096 sizeof(drv->fw.fw_version), 1097 "%u.%08x.%u %s", major, minor, 1098 local_comp, iwl_reduced_fw_name(drv)); 1099 break; 1100 } 1101 case IWL_UCODE_TLV_FW_DBG_DEST: { 1102 const struct iwl_fw_dbg_dest_tlv *dest = NULL; 1103 const struct iwl_fw_dbg_dest_tlv_v1 *dest_v1 = NULL; 1104 u8 mon_mode; 1105 1106 if (tlv_len < sizeof(*pieces->dbg_dest_ver)) 1107 goto invalid_tlv_len; 1108 1109 pieces->dbg_dest_ver = (const u8 *)tlv_data; 1110 if (*pieces->dbg_dest_ver == 1) { 1111 if (tlv_len < sizeof(*dest)) 1112 goto invalid_tlv_len; 1113 dest = (const void *)tlv_data; 1114 } else if (*pieces->dbg_dest_ver == 0) { 1115 if (tlv_len < sizeof(*dest_v1)) 1116 goto invalid_tlv_len; 1117 dest_v1 = (const void *)tlv_data; 1118 } else { 1119 IWL_ERR(drv, 1120 "The version is %d, and it is invalid\n", 1121 *pieces->dbg_dest_ver); 1122 break; 1123 } 1124 1125 if (pieces->dbg_dest_tlv_init) { 1126 IWL_ERR(drv, 1127 "dbg destination ignored, already exists\n"); 1128 break; 1129 } 1130 1131 pieces->dbg_dest_tlv_init = true; 1132 1133 if (dest_v1) { 1134 pieces->dbg_dest_tlv_v1 = dest_v1; 1135 mon_mode = dest_v1->monitor_mode; 1136 } else { 1137 pieces->dbg_dest_tlv = dest; 1138 mon_mode = dest->monitor_mode; 1139 } 1140 1141 IWL_INFO(drv, "Found debug destination: %s\n", 1142 get_fw_dbg_mode_string(mon_mode)); 1143 1144 drv->fw.dbg.n_dest_reg = (dest_v1) ? 1145 tlv_len - 1146 offsetof(struct iwl_fw_dbg_dest_tlv_v1, 1147 reg_ops) : 1148 tlv_len - 1149 offsetof(struct iwl_fw_dbg_dest_tlv, 1150 reg_ops); 1151 1152 drv->fw.dbg.n_dest_reg /= 1153 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]); 1154 1155 break; 1156 } 1157 case IWL_UCODE_TLV_FW_DBG_CONF: { 1158 const struct iwl_fw_dbg_conf_tlv *conf = 1159 (const void *)tlv_data; 1160 1161 if (!pieces->dbg_dest_tlv_init) { 1162 IWL_ERR(drv, 1163 "Ignore dbg config %d - no destination configured\n", 1164 conf->id); 1165 break; 1166 } 1167 1168 if (conf->id >= ARRAY_SIZE(drv->fw.dbg.conf_tlv)) { 1169 IWL_ERR(drv, 1170 "Skip unknown configuration: %d\n", 1171 conf->id); 1172 break; 1173 } 1174 1175 if (pieces->dbg_conf_tlv[conf->id]) { 1176 IWL_ERR(drv, 1177 "Ignore duplicate dbg config %d\n", 1178 conf->id); 1179 break; 1180 } 1181 1182 if (conf->usniffer) 1183 usniffer_req = true; 1184 1185 IWL_INFO(drv, "Found debug configuration: %d\n", 1186 conf->id); 1187 1188 pieces->dbg_conf_tlv[conf->id] = conf; 1189 pieces->dbg_conf_tlv_len[conf->id] = tlv_len; 1190 break; 1191 } 1192 case IWL_UCODE_TLV_FW_DBG_TRIGGER: { 1193 const struct iwl_fw_dbg_trigger_tlv *trigger = 1194 (const void *)tlv_data; 1195 u32 trigger_id = le32_to_cpu(trigger->id); 1196 1197 if (trigger_id >= ARRAY_SIZE(drv->fw.dbg.trigger_tlv)) { 1198 IWL_ERR(drv, 1199 "Skip unknown trigger: %u\n", 1200 trigger->id); 1201 break; 1202 } 1203 1204 if (pieces->dbg_trigger_tlv[trigger_id]) { 1205 IWL_ERR(drv, 1206 "Ignore duplicate dbg trigger %u\n", 1207 trigger->id); 1208 break; 1209 } 1210 1211 IWL_INFO(drv, "Found debug trigger: %u\n", trigger->id); 1212 1213 pieces->dbg_trigger_tlv[trigger_id] = trigger; 1214 pieces->dbg_trigger_tlv_len[trigger_id] = tlv_len; 1215 break; 1216 } 1217 case IWL_UCODE_TLV_FW_DBG_DUMP_LST: { 1218 if (tlv_len != sizeof(u32)) { 1219 IWL_ERR(drv, 1220 "dbg lst mask size incorrect, skip\n"); 1221 break; 1222 } 1223 1224 drv->fw.dbg.dump_mask = 1225 le32_to_cpup((const __le32 *)tlv_data); 1226 break; 1227 } 1228 case IWL_UCODE_TLV_SEC_RT_USNIFFER: 1229 *usniffer_images = true; 1230 iwl_store_ucode_sec(&pieces->img[IWL_UCODE_REGULAR_USNIFFER], 1231 tlv_data, tlv_len); 1232 break; 1233 case IWL_UCODE_TLV_PAGING: 1234 if (tlv_len != sizeof(u32)) 1235 goto invalid_tlv_len; 1236 paging_mem_size = le32_to_cpup((const __le32 *)tlv_data); 1237 1238 IWL_DEBUG_FW(drv, 1239 "Paging: paging enabled (size = %u bytes)\n", 1240 paging_mem_size); 1241 1242 if (paging_mem_size > MAX_PAGING_IMAGE_SIZE) { 1243 IWL_ERR(drv, 1244 "Paging: driver supports up to %lu bytes for paging image\n", 1245 MAX_PAGING_IMAGE_SIZE); 1246 return -EINVAL; 1247 } 1248 1249 if (paging_mem_size & (FW_PAGING_SIZE - 1)) { 1250 IWL_ERR(drv, 1251 "Paging: image isn't multiple %lu\n", 1252 FW_PAGING_SIZE); 1253 return -EINVAL; 1254 } 1255 1256 drv->fw.img[IWL_UCODE_REGULAR].paging_mem_size = 1257 paging_mem_size; 1258 usniffer_img = IWL_UCODE_REGULAR_USNIFFER; 1259 drv->fw.img[usniffer_img].paging_mem_size = 1260 paging_mem_size; 1261 break; 1262 case IWL_UCODE_TLV_FW_GSCAN_CAPA: 1263 /* ignored */ 1264 break; 1265 case IWL_UCODE_TLV_FW_MEM_SEG: { 1266 const struct iwl_fw_dbg_mem_seg_tlv *dbg_mem = 1267 (const void *)tlv_data; 1268 size_t size; 1269 struct iwl_fw_dbg_mem_seg_tlv *n; 1270 1271 if (tlv_len != (sizeof(*dbg_mem))) 1272 goto invalid_tlv_len; 1273 1274 IWL_DEBUG_INFO(drv, "Found debug memory segment: %u\n", 1275 dbg_mem->data_type); 1276 1277 size = sizeof(*pieces->dbg_mem_tlv) * 1278 (pieces->n_mem_tlv + 1); 1279 n = krealloc(pieces->dbg_mem_tlv, size, GFP_KERNEL); 1280 if (!n) 1281 return -ENOMEM; 1282 pieces->dbg_mem_tlv = n; 1283 pieces->dbg_mem_tlv[pieces->n_mem_tlv] = *dbg_mem; 1284 pieces->n_mem_tlv++; 1285 break; 1286 } 1287 case IWL_UCODE_TLV_IML: { 1288 drv->fw.iml_len = tlv_len; 1289 drv->fw.iml = kmemdup(tlv_data, tlv_len, GFP_KERNEL); 1290 if (!drv->fw.iml) 1291 return -ENOMEM; 1292 break; 1293 } 1294 case IWL_UCODE_TLV_FW_RECOVERY_INFO: { 1295 const struct { 1296 __le32 buf_addr; 1297 __le32 buf_size; 1298 } *recov_info = (const void *)tlv_data; 1299 1300 if (tlv_len != sizeof(*recov_info)) 1301 goto invalid_tlv_len; 1302 capa->error_log_addr = 1303 le32_to_cpu(recov_info->buf_addr); 1304 capa->error_log_size = 1305 le32_to_cpu(recov_info->buf_size); 1306 } 1307 break; 1308 case IWL_UCODE_TLV_FW_FSEQ_VERSION: { 1309 const struct { 1310 u8 version[32]; 1311 u8 sha1[20]; 1312 } *fseq_ver = (const void *)tlv_data; 1313 1314 if (tlv_len != sizeof(*fseq_ver)) 1315 goto invalid_tlv_len; 1316 IWL_DEBUG_INFO(drv, 1317 "TLV_FW_FSEQ_VERSION: %.32s (sha1: %.20s)\n", 1318 fseq_ver->version, fseq_ver->sha1); 1319 } 1320 break; 1321 case IWL_UCODE_TLV_FW_NUM_STATIONS: 1322 if (tlv_len != sizeof(u32)) 1323 goto invalid_tlv_len; 1324 if (le32_to_cpup((const __le32 *)tlv_data) > 1325 IWL_STATION_COUNT_MAX) { 1326 IWL_ERR(drv, 1327 "%d is an invalid number of station\n", 1328 le32_to_cpup((const __le32 *)tlv_data)); 1329 goto tlv_error; 1330 } 1331 capa->num_stations = 1332 le32_to_cpup((const __le32 *)tlv_data); 1333 break; 1334 case IWL_UCODE_TLV_FW_NUM_LINKS: 1335 if (tlv_len != sizeof(u32)) 1336 goto invalid_tlv_len; 1337 if (le32_to_cpup((const __le32 *)tlv_data) > 1338 IWL_FW_MAX_LINKS) { 1339 IWL_ERR(drv, 1340 "%d is an invalid number of links\n", 1341 le32_to_cpup((const __le32 *)tlv_data)); 1342 goto tlv_error; 1343 } 1344 capa->num_links = 1345 le32_to_cpup((const __le32 *)tlv_data); 1346 break; 1347 case IWL_UCODE_TLV_FW_NUM_BEACONS: 1348 if (tlv_len != sizeof(u32)) 1349 goto invalid_tlv_len; 1350 capa->num_beacons = 1351 le32_to_cpup((const __le32 *)tlv_data); 1352 break; 1353 case IWL_UCODE_TLV_FW_NUM_MCAST_KEY_ENTRIES: 1354 if (tlv_len != sizeof(u32)) 1355 goto invalid_tlv_len; 1356 capa->num_mcast_key_entries = 1357 le32_to_cpup((const __le32 *)tlv_data); 1358 break; 1359 case IWL_UCODE_TLV_FW_NAN_MAX_CHAN_SWITCH_TIME: 1360 if (tlv_len != sizeof(u32)) 1361 goto invalid_tlv_len; 1362 capa->nan_max_chan_switch_time = 1363 le32_to_cpup((const __le32 *)tlv_data); 1364 break; 1365 case IWL_UCODE_TLV_UMAC_DEBUG_ADDRS: { 1366 const struct iwl_umac_debug_addrs *dbg_ptrs = 1367 (const void *)tlv_data; 1368 1369 if (tlv_len != sizeof(*dbg_ptrs)) 1370 goto invalid_tlv_len; 1371 if (drv->trans->mac_cfg->device_family < 1372 IWL_DEVICE_FAMILY_22000) 1373 break; 1374 drv->trans->dbg.umac_error_event_table = 1375 le32_to_cpu(dbg_ptrs->error_info_addr) & 1376 ~FW_ADDR_CACHE_CONTROL; 1377 drv->trans->dbg.error_event_table_tlv_status |= 1378 IWL_ERROR_EVENT_TABLE_UMAC; 1379 break; 1380 } 1381 case IWL_UCODE_TLV_LMAC_DEBUG_ADDRS: { 1382 const struct iwl_lmac_debug_addrs *dbg_ptrs = 1383 (const void *)tlv_data; 1384 1385 if (tlv_len != sizeof(*dbg_ptrs)) 1386 goto invalid_tlv_len; 1387 if (drv->trans->mac_cfg->device_family < 1388 IWL_DEVICE_FAMILY_22000) 1389 break; 1390 drv->trans->dbg.lmac_error_event_table[0] = 1391 le32_to_cpu(dbg_ptrs->error_event_table_ptr) & 1392 ~FW_ADDR_CACHE_CONTROL; 1393 drv->trans->dbg.error_event_table_tlv_status |= 1394 IWL_ERROR_EVENT_TABLE_LMAC1; 1395 break; 1396 } 1397 case IWL_UCODE_TLV_TYPE_REGIONS: 1398 iwl_parse_dbg_tlv_assert_tables(drv, tlv); 1399 fallthrough; 1400 case IWL_UCODE_TLV_TYPE_DEBUG_INFO: 1401 case IWL_UCODE_TLV_TYPE_BUFFER_ALLOCATION: 1402 case IWL_UCODE_TLV_TYPE_HCMD: 1403 case IWL_UCODE_TLV_TYPE_TRIGGERS: 1404 case IWL_UCODE_TLV_TYPE_CONF_SET: 1405 if (iwlwifi_mod_params.enable_ini) 1406 iwl_dbg_tlv_alloc(drv->trans, tlv, false); 1407 break; 1408 case IWL_UCODE_TLV_CMD_VERSIONS: 1409 if (tlv_len % sizeof(struct iwl_fw_cmd_version)) { 1410 IWL_ERR(drv, 1411 "Invalid length for command versions: %u\n", 1412 tlv_len); 1413 tlv_len /= sizeof(struct iwl_fw_cmd_version); 1414 tlv_len *= sizeof(struct iwl_fw_cmd_version); 1415 } 1416 if (WARN_ON(capa->cmd_versions)) 1417 return -EINVAL; 1418 capa->cmd_versions = kmemdup(tlv_data, tlv_len, 1419 GFP_KERNEL); 1420 if (!capa->cmd_versions) 1421 return -ENOMEM; 1422 capa->n_cmd_versions = 1423 tlv_len / sizeof(struct iwl_fw_cmd_version); 1424 break; 1425 case IWL_UCODE_TLV_PHY_INTEGRATION_VERSION: 1426 if (drv->fw.phy_integration_ver) { 1427 IWL_ERR(drv, 1428 "phy integration str ignored, already exists\n"); 1429 break; 1430 } 1431 1432 drv->fw.phy_integration_ver = 1433 kmemdup(tlv_data, tlv_len, GFP_KERNEL); 1434 if (!drv->fw.phy_integration_ver) 1435 return -ENOMEM; 1436 drv->fw.phy_integration_ver_len = tlv_len; 1437 break; 1438 case IWL_UCODE_TLV_SEC_TABLE_ADDR: 1439 case IWL_UCODE_TLV_D3_KEK_KCK_ADDR: 1440 iwl_drv_set_dump_exclude(drv, tlv_type, 1441 tlv_data, tlv_len); 1442 break; 1443 case IWL_UCODE_TLV_CURRENT_PC: 1444 if (tlv_len < sizeof(struct iwl_pc_data)) 1445 goto invalid_tlv_len; 1446 drv->trans->dbg.pc_data = 1447 kmemdup(tlv_data, tlv_len, GFP_KERNEL); 1448 if (!drv->trans->dbg.pc_data) 1449 return -ENOMEM; 1450 drv->trans->dbg.num_pc = 1451 tlv_len / sizeof(struct iwl_pc_data); 1452 break; 1453 case IWL_UCODE_TLV_PNVM_DATA: 1454 if (drv->fw.pnvm_data) 1455 break; 1456 drv->fw.pnvm_data = 1457 kvmemdup(tlv_data, tlv_len, GFP_KERNEL); 1458 if (!drv->fw.pnvm_data) 1459 return -ENOMEM; 1460 drv->fw.pnvm_size = tlv_len; 1461 break; 1462 case IWL_UCODE_TLV_CMD_BIOS_TABLE: 1463 if (tlv_len % sizeof(struct iwl_fw_cmd_bios_table)) { 1464 IWL_ERR(drv, 1465 "Invalid length for command bios table: %u\n", 1466 tlv_len); 1467 return -EINVAL; 1468 } 1469 1470 if (capa->cmd_bios_tables) { 1471 IWL_ERR(drv, "Duplicate TLV type 0x%02X detected\n", 1472 tlv_type); 1473 return -EINVAL; 1474 } 1475 capa->cmd_bios_tables = kmemdup(tlv_data, tlv_len, 1476 GFP_KERNEL); 1477 if (!capa->cmd_bios_tables) 1478 return -ENOMEM; 1479 capa->n_cmd_bios_tables = 1480 tlv_len / sizeof(struct iwl_fw_cmd_bios_table); 1481 break; 1482 default: 1483 IWL_DEBUG_INFO(drv, "unknown TLV: %d\n", tlv_type); 1484 break; 1485 } 1486 } 1487 1488 if (!fw_has_capa(capa, IWL_UCODE_TLV_CAPA_USNIFFER_UNIFIED) && 1489 usniffer_req && !*usniffer_images) { 1490 IWL_ERR(drv, 1491 "user selected to work with usniffer but usniffer image isn't available in ucode package\n"); 1492 return -EINVAL; 1493 } 1494 1495 if (len) { 1496 IWL_ERR(drv, "invalid TLV after parsing: %zd\n", len); 1497 iwl_print_hex_dump(drv, IWL_DL_FW, data, len); 1498 return -EINVAL; 1499 } 1500 1501 return 0; 1502 1503 invalid_tlv_len: 1504 IWL_ERR(drv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len); 1505 tlv_error: 1506 iwl_print_hex_dump(drv, IWL_DL_FW, tlv_data, tlv_len); 1507 1508 return -EINVAL; 1509 } 1510 1511 static int iwl_alloc_ucode_mem(struct fw_img *out, struct fw_img_parsing *img) 1512 { 1513 struct fw_desc *sec; 1514 1515 sec = kzalloc_objs(*sec, img->sec_counter); 1516 if (!sec) 1517 return -ENOMEM; 1518 1519 out->sec = sec; 1520 out->num_sec = img->sec_counter; 1521 1522 for (int i = 0; i < out->num_sec; i++) 1523 if (iwl_alloc_fw_desc(&sec[i], &img->sec[i])) 1524 return -ENOMEM; 1525 1526 return 0; 1527 } 1528 1529 static int iwl_alloc_ucode(struct iwl_drv *drv, 1530 struct iwl_firmware_pieces *pieces, 1531 enum iwl_ucode_type type) 1532 { 1533 return iwl_alloc_ucode_mem(&drv->fw.img[type], &pieces->img[type]); 1534 } 1535 1536 static int validate_sec_sizes(struct iwl_drv *drv, 1537 struct iwl_firmware_pieces *pieces, 1538 const struct iwl_rf_cfg *cfg) 1539 { 1540 IWL_DEBUG_INFO(drv, "f/w package hdr runtime inst size = %zd\n", 1541 get_sec_size(pieces, IWL_UCODE_REGULAR, 1542 IWL_UCODE_SECTION_INST)); 1543 IWL_DEBUG_INFO(drv, "f/w package hdr runtime data size = %zd\n", 1544 get_sec_size(pieces, IWL_UCODE_REGULAR, 1545 IWL_UCODE_SECTION_DATA)); 1546 IWL_DEBUG_INFO(drv, "f/w package hdr init inst size = %zd\n", 1547 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST)); 1548 IWL_DEBUG_INFO(drv, "f/w package hdr init data size = %zd\n", 1549 get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA)); 1550 1551 /* Verify that uCode images will fit in card's SRAM. */ 1552 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_INST) > 1553 cfg->max_inst_size) { 1554 IWL_ERR(drv, "uCode instr len %zd too large to fit in\n", 1555 get_sec_size(pieces, IWL_UCODE_REGULAR, 1556 IWL_UCODE_SECTION_INST)); 1557 return -1; 1558 } 1559 1560 if (get_sec_size(pieces, IWL_UCODE_REGULAR, IWL_UCODE_SECTION_DATA) > 1561 cfg->max_data_size) { 1562 IWL_ERR(drv, "uCode data len %zd too large to fit in\n", 1563 get_sec_size(pieces, IWL_UCODE_REGULAR, 1564 IWL_UCODE_SECTION_DATA)); 1565 return -1; 1566 } 1567 1568 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_INST) > 1569 cfg->max_inst_size) { 1570 IWL_ERR(drv, "uCode init instr len %zd too large to fit in\n", 1571 get_sec_size(pieces, IWL_UCODE_INIT, 1572 IWL_UCODE_SECTION_INST)); 1573 return -1; 1574 } 1575 1576 if (get_sec_size(pieces, IWL_UCODE_INIT, IWL_UCODE_SECTION_DATA) > 1577 cfg->max_data_size) { 1578 IWL_ERR(drv, "uCode init data len %zd too large to fit in\n", 1579 get_sec_size(pieces, IWL_UCODE_REGULAR, 1580 IWL_UCODE_SECTION_DATA)); 1581 return -1; 1582 } 1583 return 0; 1584 } 1585 1586 static struct iwl_op_mode * 1587 _iwl_op_mode_start(struct iwl_drv *drv, struct iwlwifi_opmode_table *op) 1588 { 1589 const struct iwl_op_mode_ops *ops = op->ops; 1590 struct dentry *dbgfs_dir = NULL; 1591 struct iwl_op_mode *op_mode = NULL; 1592 int retry, max_retry = !!iwlwifi_mod_params.fw_restart * IWL_MAX_INIT_RETRY; 1593 1594 /* also protects start/stop from racing against each other */ 1595 lockdep_assert_held(&iwlwifi_opmode_table_mtx); 1596 1597 for (retry = 0; retry <= max_retry; retry++) { 1598 1599 #ifdef CONFIG_IWLWIFI_DEBUGFS 1600 drv->dbgfs_op_mode = debugfs_create_dir(op->name, 1601 drv->dbgfs_drv); 1602 dbgfs_dir = drv->dbgfs_op_mode; 1603 #endif 1604 1605 op_mode = ops->start(drv->trans, drv->trans->cfg, 1606 &drv->fw, dbgfs_dir); 1607 1608 if (!IS_ERR(op_mode)) 1609 return op_mode; 1610 1611 if (iwl_trans_is_dead(drv->trans)) 1612 break; 1613 1614 #ifdef CONFIG_IWLWIFI_DEBUGFS 1615 debugfs_remove_recursive(drv->dbgfs_op_mode); 1616 drv->dbgfs_op_mode = NULL; 1617 #endif 1618 1619 if (PTR_ERR(op_mode) != -ETIMEDOUT) 1620 break; 1621 1622 IWL_ERR(drv, "retry init count %d\n", retry); 1623 } 1624 1625 return NULL; 1626 } 1627 1628 static void _iwl_op_mode_stop(struct iwl_drv *drv) 1629 { 1630 /* also protects start/stop from racing against each other */ 1631 lockdep_assert_held(&iwlwifi_opmode_table_mtx); 1632 1633 /* op_mode can be NULL if its start failed */ 1634 if (drv->op_mode) { 1635 iwl_op_mode_stop(drv->op_mode); 1636 drv->op_mode = NULL; 1637 1638 #ifdef CONFIG_IWLWIFI_DEBUGFS 1639 debugfs_remove_recursive(drv->dbgfs_op_mode); 1640 drv->dbgfs_op_mode = NULL; 1641 #endif 1642 } 1643 } 1644 1645 /* 1646 * iwl_req_fw_callback - callback when firmware was loaded 1647 * 1648 * If loaded successfully, copies the firmware into buffers 1649 * for the card to fetch (via DMA). 1650 */ 1651 static void iwl_req_fw_callback(const struct firmware *ucode_raw, void *context) 1652 { 1653 int min_core, max_core, loaded_core; 1654 struct iwl_drv *drv = context; 1655 struct iwl_fw *fw = &drv->fw; 1656 const struct iwl_ucode_header *ucode; 1657 struct iwlwifi_opmode_table *op; 1658 int err; 1659 struct iwl_firmware_pieces *pieces; 1660 unsigned int api_min, api_max; 1661 size_t trigger_tlv_sz[FW_DBG_TRIGGER_MAX]; 1662 u32 api_ver; 1663 int i; 1664 bool usniffer_images = false; 1665 bool failure = true; 1666 1667 iwl_get_ucode_api_versions(drv->trans, &api_min, &api_max); 1668 1669 fw->ucode_capa.max_probe_length = IWL_DEFAULT_MAX_PROBE_LENGTH; 1670 fw->ucode_capa.standard_phy_calibration_size = 1671 IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE; 1672 fw->ucode_capa.n_scan_channels = IWL_DEFAULT_SCAN_CHANNELS; 1673 fw->ucode_capa.num_stations = IWL_STATION_COUNT_MAX; 1674 fw->ucode_capa.num_beacons = 1; 1675 fw->ucode_capa.num_mcast_key_entries = 2; 1676 /* dump all fw memory areas by default */ 1677 fw->dbg.dump_mask = 0xffffffff; 1678 1679 pieces = kzalloc_obj(*pieces); 1680 if (!pieces) 1681 goto out_free_fw; 1682 1683 if (!ucode_raw) 1684 goto try_again; 1685 1686 IWL_DEBUG_FW_INFO(drv, "Loaded firmware file '%s' (%zd bytes).\n", 1687 drv->firmware_name, ucode_raw->size); 1688 1689 /* Make sure that we got at least the API version number */ 1690 if (ucode_raw->size < 4) { 1691 IWL_ERR(drv, "File size way too small!\n"); 1692 goto try_again; 1693 } 1694 1695 /* Data from ucode file: header followed by uCode images */ 1696 ucode = (const struct iwl_ucode_header *)ucode_raw->data; 1697 1698 if (ucode->ver) 1699 err = iwl_parse_v1_v2_firmware(drv, ucode_raw, pieces); 1700 else 1701 err = iwl_parse_tlv_firmware(drv, ucode_raw, pieces, 1702 &fw->ucode_capa, &usniffer_images); 1703 1704 if (err) 1705 goto try_again; 1706 1707 if (fw_has_api(&drv->fw.ucode_capa, IWL_UCODE_TLV_API_NEW_VERSION)) 1708 api_ver = drv->fw.ucode_ver; 1709 else 1710 api_ver = IWL_UCODE_API(drv->fw.ucode_ver); 1711 1712 /* 1713 * api_ver should match the api version forming part of the 1714 * firmware filename ... but we don't check for that and only rely 1715 * on the API version read from firmware header from here on forward 1716 */ 1717 1718 /* 1719 * if -cN.ucode file was loaded, core version == file version, 1720 * otherwise core version == file version (API version) - 3 1721 */ 1722 if (iwl_api_is_core_number(drv->fw_index)) 1723 loaded_core = api_ver; 1724 else 1725 loaded_core = api_ver - API_TO_CORE_OFFS; 1726 1727 min_core = iwl_api_to_core(api_min); 1728 max_core = iwl_api_to_core(api_max); 1729 1730 if (loaded_core < min_core || loaded_core > max_core) { 1731 IWL_ERR(drv, 1732 "Driver unable to support your firmware API. " 1733 "Driver supports FW core %d..%d, firmware is %d.\n", 1734 min_core, max_core, loaded_core); 1735 goto try_again; 1736 } 1737 1738 /* 1739 * In mvm uCode there is no difference between data and instructions 1740 * sections. 1741 */ 1742 if (fw->type == IWL_FW_DVM && validate_sec_sizes(drv, pieces, 1743 drv->trans->cfg)) 1744 goto try_again; 1745 1746 /* Allocate ucode buffers for card's bus-master loading ... */ 1747 1748 /* Runtime instructions and 2 copies of data: 1749 * 1) unmodified from disk 1750 * 2) backup cache for save/restore during power-downs 1751 */ 1752 for (i = 0; i < IWL_UCODE_TYPE_MAX; i++) 1753 if (iwl_alloc_ucode(drv, pieces, i)) 1754 goto out_free_fw; 1755 1756 if (pieces->dbg_dest_tlv_init) { 1757 size_t dbg_dest_size = sizeof(*drv->fw.dbg.dest_tlv) + 1758 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) * 1759 drv->fw.dbg.n_dest_reg; 1760 1761 drv->fw.dbg.dest_tlv = kmalloc(dbg_dest_size, GFP_KERNEL); 1762 1763 if (!drv->fw.dbg.dest_tlv) 1764 goto out_free_fw; 1765 1766 if (*pieces->dbg_dest_ver == 0) { 1767 memcpy(drv->fw.dbg.dest_tlv, pieces->dbg_dest_tlv_v1, 1768 dbg_dest_size); 1769 } else { 1770 struct iwl_fw_dbg_dest_tlv_v1 *dest_tlv = 1771 drv->fw.dbg.dest_tlv; 1772 1773 dest_tlv->version = pieces->dbg_dest_tlv->version; 1774 dest_tlv->monitor_mode = 1775 pieces->dbg_dest_tlv->monitor_mode; 1776 dest_tlv->size_power = 1777 pieces->dbg_dest_tlv->size_power; 1778 dest_tlv->wrap_count = 1779 pieces->dbg_dest_tlv->wrap_count; 1780 dest_tlv->write_ptr_reg = 1781 pieces->dbg_dest_tlv->write_ptr_reg; 1782 dest_tlv->base_shift = 1783 pieces->dbg_dest_tlv->base_shift; 1784 memcpy(dest_tlv->reg_ops, 1785 pieces->dbg_dest_tlv->reg_ops, 1786 sizeof(drv->fw.dbg.dest_tlv->reg_ops[0]) * 1787 drv->fw.dbg.n_dest_reg); 1788 1789 /* In version 1 of the destination tlv, which is 1790 * relevant for internal buffer exclusively, 1791 * the base address is part of given with the length 1792 * of the buffer, and the size shift is give instead of 1793 * end shift. We now store these values in base_reg, 1794 * and end shift, and when dumping the data we'll 1795 * manipulate it for extracting both the length and 1796 * base address */ 1797 dest_tlv->base_reg = pieces->dbg_dest_tlv->cfg_reg; 1798 dest_tlv->end_shift = 1799 pieces->dbg_dest_tlv->size_shift; 1800 } 1801 } 1802 1803 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.conf_tlv); i++) { 1804 if (pieces->dbg_conf_tlv[i]) { 1805 drv->fw.dbg.conf_tlv[i] = 1806 kmemdup(pieces->dbg_conf_tlv[i], 1807 pieces->dbg_conf_tlv_len[i], 1808 GFP_KERNEL); 1809 if (!drv->fw.dbg.conf_tlv[i]) 1810 goto out_free_fw; 1811 } 1812 } 1813 1814 memset(&trigger_tlv_sz, 0xff, sizeof(trigger_tlv_sz)); 1815 1816 trigger_tlv_sz[FW_DBG_TRIGGER_MISSED_BEACONS] = 1817 sizeof(struct iwl_fw_dbg_trigger_missed_bcon); 1818 trigger_tlv_sz[FW_DBG_TRIGGER_CHANNEL_SWITCH] = 0; 1819 trigger_tlv_sz[FW_DBG_TRIGGER_FW_NOTIF] = 1820 sizeof(struct iwl_fw_dbg_trigger_cmd); 1821 trigger_tlv_sz[FW_DBG_TRIGGER_MLME] = 1822 sizeof(struct iwl_fw_dbg_trigger_mlme); 1823 trigger_tlv_sz[FW_DBG_TRIGGER_STATS] = 1824 sizeof(struct iwl_fw_dbg_trigger_stats); 1825 trigger_tlv_sz[FW_DBG_TRIGGER_RSSI] = 1826 sizeof(struct iwl_fw_dbg_trigger_low_rssi); 1827 trigger_tlv_sz[FW_DBG_TRIGGER_TXQ_TIMERS] = 1828 sizeof(struct iwl_fw_dbg_trigger_txq_timer); 1829 trigger_tlv_sz[FW_DBG_TRIGGER_TIME_EVENT] = 1830 sizeof(struct iwl_fw_dbg_trigger_time_event); 1831 trigger_tlv_sz[FW_DBG_TRIGGER_BA] = 1832 sizeof(struct iwl_fw_dbg_trigger_ba); 1833 trigger_tlv_sz[FW_DBG_TRIGGER_TDLS] = 1834 sizeof(struct iwl_fw_dbg_trigger_tdls); 1835 1836 for (i = 0; i < ARRAY_SIZE(drv->fw.dbg.trigger_tlv); i++) { 1837 if (pieces->dbg_trigger_tlv[i]) { 1838 /* 1839 * If the trigger isn't long enough, WARN and exit. 1840 * Someone is trying to debug something and he won't 1841 * be able to catch the bug he is trying to chase. 1842 * We'd better be noisy to be sure he knows what's 1843 * going on. 1844 */ 1845 if (WARN_ON(pieces->dbg_trigger_tlv_len[i] < 1846 (trigger_tlv_sz[i] + 1847 sizeof(struct iwl_fw_dbg_trigger_tlv)))) 1848 goto out_free_fw; 1849 drv->fw.dbg.trigger_tlv_len[i] = 1850 pieces->dbg_trigger_tlv_len[i]; 1851 drv->fw.dbg.trigger_tlv[i] = 1852 kmemdup(pieces->dbg_trigger_tlv[i], 1853 drv->fw.dbg.trigger_tlv_len[i], 1854 GFP_KERNEL); 1855 if (!drv->fw.dbg.trigger_tlv[i]) 1856 goto out_free_fw; 1857 } 1858 } 1859 1860 /* Now that we can no longer fail, copy information */ 1861 1862 drv->fw.dbg.mem_tlv = pieces->dbg_mem_tlv; 1863 pieces->dbg_mem_tlv = NULL; 1864 drv->fw.dbg.n_mem_tlv = pieces->n_mem_tlv; 1865 1866 /* 1867 * The (size - 16) / 12 formula is based on the information recorded 1868 * for each event, which is of mode 1 (including timestamp) for all 1869 * new microcodes that include this information. 1870 */ 1871 fw->init_evtlog_ptr = pieces->init_evtlog_ptr; 1872 if (pieces->init_evtlog_size) 1873 fw->init_evtlog_size = (pieces->init_evtlog_size - 16)/12; 1874 else 1875 fw->init_evtlog_size = 1876 drv->trans->mac_cfg->base->max_event_log_size; 1877 fw->init_errlog_ptr = pieces->init_errlog_ptr; 1878 fw->inst_evtlog_ptr = pieces->inst_evtlog_ptr; 1879 if (pieces->inst_evtlog_size) 1880 fw->inst_evtlog_size = (pieces->inst_evtlog_size - 16)/12; 1881 else 1882 fw->inst_evtlog_size = 1883 drv->trans->mac_cfg->base->max_event_log_size; 1884 fw->inst_errlog_ptr = pieces->inst_errlog_ptr; 1885 1886 /* 1887 * figure out the offset of chain noise reset and gain commands 1888 * base on the size of standard phy calibration commands table size 1889 */ 1890 if (fw->ucode_capa.standard_phy_calibration_size > 1891 IWL_MAX_PHY_CALIBRATE_TBL_SIZE) 1892 fw->ucode_capa.standard_phy_calibration_size = 1893 IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE; 1894 1895 /* We have our copies now, allow OS release its copies */ 1896 release_firmware(ucode_raw); 1897 1898 iwl_dbg_tlv_load_bin(drv->trans->dev, drv->trans); 1899 1900 mutex_lock(&iwlwifi_opmode_table_mtx); 1901 switch (fw->type) { 1902 case IWL_FW_DVM: 1903 op = &iwlwifi_opmode_table[DVM_OP_MODE]; 1904 break; 1905 default: 1906 WARN(1, "Invalid fw type %d\n", fw->type); 1907 fallthrough; 1908 case IWL_FW_MVM: 1909 op = &iwlwifi_opmode_table[MVM_OP_MODE]; 1910 break; 1911 } 1912 1913 #if IS_ENABLED(CONFIG_IWLMLD) 1914 if (iwl_drv_is_wifi7_supported(drv->trans)) 1915 op = &iwlwifi_opmode_table[MLD_OP_MODE]; 1916 #endif 1917 1918 IWL_INFO(drv, "loaded firmware version %s op_mode %s\n", 1919 drv->fw.fw_version, op->name); 1920 1921 /* add this device to the list of devices using this op_mode */ 1922 list_add_tail(&drv->list, &op->drv); 1923 1924 if (op->ops) { 1925 drv->op_mode = _iwl_op_mode_start(drv, op); 1926 1927 if (!drv->op_mode) { 1928 mutex_unlock(&iwlwifi_opmode_table_mtx); 1929 goto out_unbind; 1930 } 1931 } else { 1932 request_module_nowait("%s", op->name); 1933 } 1934 mutex_unlock(&iwlwifi_opmode_table_mtx); 1935 1936 complete(&drv->request_firmware_complete); 1937 1938 failure = false; 1939 goto free; 1940 1941 try_again: 1942 /* try next, if any */ 1943 release_firmware(ucode_raw); 1944 if (iwl_request_firmware(drv, false)) 1945 goto out_unbind; 1946 goto free; 1947 1948 out_free_fw: 1949 release_firmware(ucode_raw); 1950 out_unbind: 1951 complete(&drv->request_firmware_complete); 1952 device_release_driver(drv->trans->dev); 1953 /* drv has just been freed by the release */ 1954 failure = false; 1955 free: 1956 if (failure) 1957 iwl_dealloc_ucode(drv); 1958 1959 if (pieces) { 1960 for (i = 0; i < ARRAY_SIZE(pieces->img); i++) 1961 kfree(pieces->img[i].sec); 1962 kfree(pieces->dbg_mem_tlv); 1963 kfree(pieces); 1964 } 1965 } 1966 1967 struct iwl_drv *iwl_drv_start(struct iwl_trans *trans) 1968 { 1969 struct iwl_drv *drv; 1970 int ret; 1971 1972 drv = kzalloc_obj(*drv); 1973 if (!drv) { 1974 ret = -ENOMEM; 1975 goto err; 1976 } 1977 1978 drv->trans = trans; 1979 drv->dev = trans->dev; 1980 1981 init_completion(&drv->request_firmware_complete); 1982 INIT_LIST_HEAD(&drv->list); 1983 1984 #ifdef CONFIG_IWLWIFI_DEBUGFS 1985 /* Create the device debugfs entries. */ 1986 drv->dbgfs_drv = debugfs_create_dir(dev_name(trans->dev), 1987 iwl_dbgfs_root); 1988 1989 /* Create transport layer debugfs dir */ 1990 drv->trans->dbgfs_dir = debugfs_create_dir("trans", drv->dbgfs_drv); 1991 #endif 1992 1993 drv->trans->dbg.domains_bitmap = IWL_TRANS_FW_DBG_DOMAIN(drv->trans); 1994 if (iwlwifi_mod_params.enable_ini != ENABLE_INI) { 1995 /* We have a non-default value in the module parameter, 1996 * take its value 1997 */ 1998 drv->trans->dbg.domains_bitmap &= 0xffff; 1999 if (iwlwifi_mod_params.enable_ini != IWL_FW_INI_PRESET_DISABLE) { 2000 if (iwlwifi_mod_params.enable_ini > ENABLE_INI) { 2001 IWL_ERR(trans, 2002 "invalid enable_ini module parameter value: max = %d, using 0 instead\n", 2003 ENABLE_INI); 2004 iwlwifi_mod_params.enable_ini = 0; 2005 } 2006 drv->trans->dbg.domains_bitmap = 2007 BIT(IWL_FW_DBG_DOMAIN_POS + iwlwifi_mod_params.enable_ini); 2008 } 2009 } 2010 2011 ret = iwl_request_firmware(drv, true); 2012 if (ret) { 2013 IWL_ERR(trans, "Couldn't request the fw\n"); 2014 goto err_fw; 2015 } 2016 2017 return drv; 2018 2019 err_fw: 2020 #ifdef CONFIG_IWLWIFI_DEBUGFS 2021 debugfs_remove_recursive(drv->dbgfs_drv); 2022 #endif 2023 iwl_dbg_tlv_free(drv->trans); 2024 kfree(drv); 2025 err: 2026 return ERR_PTR(ret); 2027 } 2028 2029 void iwl_drv_stop(struct iwl_drv *drv) 2030 { 2031 wait_for_completion(&drv->request_firmware_complete); 2032 2033 mutex_lock(&iwlwifi_opmode_table_mtx); 2034 2035 _iwl_op_mode_stop(drv); 2036 2037 iwl_dealloc_ucode(drv); 2038 2039 /* 2040 * List is empty (this item wasn't added) 2041 * when firmware loading failed -- in that 2042 * case we can't remove it from any list. 2043 */ 2044 if (!list_empty(&drv->list)) 2045 list_del(&drv->list); 2046 mutex_unlock(&iwlwifi_opmode_table_mtx); 2047 2048 #ifdef CONFIG_IWLWIFI_DEBUGFS 2049 debugfs_remove_recursive(drv->dbgfs_drv); 2050 #endif 2051 2052 iwl_dbg_tlv_free(drv->trans); 2053 2054 kfree(drv); 2055 } 2056 2057 /* shared module parameters */ 2058 struct iwl_mod_params iwlwifi_mod_params = { 2059 .fw_restart = true, 2060 .bt_coex_active = true, 2061 .power_level = IWL_POWER_INDEX_1, 2062 .uapsd_disable = IWL_DISABLE_UAPSD_BSS | IWL_DISABLE_UAPSD_P2P_CLIENT, 2063 .enable_ini = ENABLE_INI, 2064 /* the rest are 0 by default */ 2065 }; 2066 IWL_EXPORT_SYMBOL(iwlwifi_mod_params); 2067 2068 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops) 2069 { 2070 int i; 2071 struct iwl_drv *drv; 2072 struct iwlwifi_opmode_table *op; 2073 2074 mutex_lock(&iwlwifi_opmode_table_mtx); 2075 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) { 2076 op = &iwlwifi_opmode_table[i]; 2077 if (strcmp(op->name, name)) 2078 continue; 2079 op->ops = ops; 2080 /* TODO: need to handle exceptional case */ 2081 list_for_each_entry(drv, &op->drv, list) 2082 drv->op_mode = _iwl_op_mode_start(drv, op); 2083 2084 mutex_unlock(&iwlwifi_opmode_table_mtx); 2085 return 0; 2086 } 2087 mutex_unlock(&iwlwifi_opmode_table_mtx); 2088 return -EIO; 2089 } 2090 IWL_EXPORT_SYMBOL(iwl_opmode_register); 2091 2092 void iwl_opmode_deregister(const char *name) 2093 { 2094 int i; 2095 struct iwl_drv *drv; 2096 2097 mutex_lock(&iwlwifi_opmode_table_mtx); 2098 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) { 2099 if (strcmp(iwlwifi_opmode_table[i].name, name)) 2100 continue; 2101 iwlwifi_opmode_table[i].ops = NULL; 2102 2103 /* call the stop routine for all devices */ 2104 list_for_each_entry(drv, &iwlwifi_opmode_table[i].drv, list) 2105 _iwl_op_mode_stop(drv); 2106 2107 mutex_unlock(&iwlwifi_opmode_table_mtx); 2108 return; 2109 } 2110 mutex_unlock(&iwlwifi_opmode_table_mtx); 2111 } 2112 IWL_EXPORT_SYMBOL(iwl_opmode_deregister); 2113 2114 static int __init iwl_drv_init(void) 2115 { 2116 int i, err; 2117 2118 for (i = 0; i < ARRAY_SIZE(iwlwifi_opmode_table); i++) 2119 INIT_LIST_HEAD(&iwlwifi_opmode_table[i].drv); 2120 2121 #ifdef CONFIG_IWLWIFI_DEBUGFS 2122 /* Create the root of iwlwifi debugfs subsystem. */ 2123 iwl_dbgfs_root = debugfs_create_dir(DRV_NAME, NULL); 2124 #endif 2125 2126 err = iwl_pci_register_driver(); 2127 if (err) 2128 goto cleanup_debugfs; 2129 2130 return 0; 2131 2132 cleanup_debugfs: 2133 #ifdef CONFIG_IWLWIFI_DEBUGFS 2134 debugfs_remove_recursive(iwl_dbgfs_root); 2135 #endif 2136 return err; 2137 } 2138 module_init(iwl_drv_init); 2139 2140 static void __exit iwl_drv_exit(void) 2141 { 2142 iwl_pci_unregister_driver(); 2143 iwl_trans_free_restart_list(); 2144 2145 #ifdef CONFIG_IWLWIFI_DEBUGFS 2146 debugfs_remove_recursive(iwl_dbgfs_root); 2147 #endif 2148 } 2149 module_exit(iwl_drv_exit); 2150 2151 #ifdef CONFIG_IWLWIFI_DEBUG 2152 module_param_named(debug, iwlwifi_mod_params.debug_level, uint, 0644); 2153 MODULE_PARM_DESC(debug, "debug output mask"); 2154 #endif 2155 2156 module_param_named(swcrypto, iwlwifi_mod_params.swcrypto, int, 0444); 2157 MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])"); 2158 module_param_named(11n_disable, iwlwifi_mod_params.disable_11n, uint, 0444); 2159 MODULE_PARM_DESC(11n_disable, 2160 "disable 11n functionality, bitmap: 1: full, 2: disable agg TX, 4: disable agg RX, 8 enable agg TX"); 2161 module_param_named(amsdu_size, iwlwifi_mod_params.amsdu_size, int, 0444); 2162 MODULE_PARM_DESC(amsdu_size, 2163 "amsdu size 0: 12K for multi Rx queue devices, 2K for AX210 devices, " 2164 "4K for other devices 1:4K 2:8K 3:12K (16K buffers) 4: 2K (default 0)"); 2165 module_param_named(fw_restart, iwlwifi_mod_params.fw_restart, bool, 0444); 2166 MODULE_PARM_DESC(fw_restart, "restart firmware in case of error (default true)"); 2167 2168 module_param_named(nvm_file, iwlwifi_mod_params.nvm_file, charp, 0444); 2169 MODULE_PARM_DESC(nvm_file, "NVM file name"); 2170 2171 module_param_named(uapsd_disable, iwlwifi_mod_params.uapsd_disable, uint, 0644); 2172 MODULE_PARM_DESC(uapsd_disable, 2173 "disable U-APSD functionality bitmap 1: BSS 2: P2P Client (default: 3)"); 2174 2175 module_param_named(enable_ini, iwlwifi_mod_params.enable_ini, uint, 0444); 2176 MODULE_PARM_DESC(enable_ini, 2177 "0:disable, 1-15:FW_DBG_PRESET Values, 16:enabled without preset value defined," 2178 "Debug INI TLV FW debug infrastructure (default: 16)"); 2179 2180 /* 2181 * set bt_coex_active to true, uCode will do kill/defer 2182 * every time the priority line is asserted (BT is sending signals on the 2183 * priority line in the PCIx). 2184 * set bt_coex_active to false, uCode will ignore the BT activity and 2185 * perform the normal operation 2186 * 2187 * User might experience transmit issue on some platform due to WiFi/BT 2188 * co-exist problem. The possible behaviors are: 2189 * Able to scan and finding all the available AP 2190 * Not able to associate with any AP 2191 * On those platforms, WiFi communication can be restored by set 2192 * "bt_coex_active" module parameter to "false" 2193 * 2194 * default: bt_coex_active = true (BT_COEX_ENABLE) 2195 */ 2196 module_param_named(bt_coex_active, iwlwifi_mod_params.bt_coex_active, 2197 bool, 0444); 2198 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)"); 2199 2200 module_param_named(led_mode, iwlwifi_mod_params.led_mode, int, 0444); 2201 MODULE_PARM_DESC(led_mode, "0=system default, " 2202 "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)"); 2203 2204 module_param_named(power_save, iwlwifi_mod_params.power_save, bool, 0444); 2205 MODULE_PARM_DESC(power_save, 2206 "enable WiFi power management (default: disable)"); 2207 2208 module_param_named(power_level, iwlwifi_mod_params.power_level, int, 0444); 2209 MODULE_PARM_DESC(power_level, 2210 "default power save level (range from 1 - 5, default: 1)"); 2211 2212 module_param_named(disable_11ac, iwlwifi_mod_params.disable_11ac, bool, 0444); 2213 MODULE_PARM_DESC(disable_11ac, "Disable VHT capabilities (default: false)"); 2214 2215 module_param_named(remove_when_gone, 2216 iwlwifi_mod_params.remove_when_gone, bool, 2217 0444); 2218 MODULE_PARM_DESC(remove_when_gone, 2219 "Remove dev from PCIe bus if it is deemed inaccessible (default: false)"); 2220 2221 module_param_named(disable_11ax, iwlwifi_mod_params.disable_11ax, bool, 2222 S_IRUGO); 2223 MODULE_PARM_DESC(disable_11ax, "Disable HE capabilities (default: false)"); 2224 2225 module_param_named(disable_11be, iwlwifi_mod_params.disable_11be, bool, 0444); 2226 MODULE_PARM_DESC(disable_11be, "Disable EHT capabilities (default: false)"); 2227