xref: /openbmc/linux/drivers/net/wireless/intel/iwlegacy/4965.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
116da78b7SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27ac9a364SKalle Valo /******************************************************************************
37ac9a364SKalle Valo  *
47ac9a364SKalle Valo  * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
57ac9a364SKalle Valo  *
67ac9a364SKalle Valo  * Contact Information:
77ac9a364SKalle Valo  *  Intel Linux Wireless <ilw@linux.intel.com>
87ac9a364SKalle Valo  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
97ac9a364SKalle Valo  *
107ac9a364SKalle Valo  *****************************************************************************/
117ac9a364SKalle Valo 
127ac9a364SKalle Valo #include <linux/kernel.h>
137ac9a364SKalle Valo #include <linux/module.h>
147ac9a364SKalle Valo #include <linux/pci.h>
157ac9a364SKalle Valo #include <linux/dma-mapping.h>
167ac9a364SKalle Valo #include <linux/delay.h>
177ac9a364SKalle Valo #include <linux/sched.h>
187ac9a364SKalle Valo #include <linux/skbuff.h>
197ac9a364SKalle Valo #include <linux/netdevice.h>
201410b2fcSAkinobu Mita #include <linux/units.h>
217ac9a364SKalle Valo #include <net/mac80211.h>
227ac9a364SKalle Valo #include <linux/etherdevice.h>
237ac9a364SKalle Valo #include <asm/unaligned.h>
247ac9a364SKalle Valo 
257ac9a364SKalle Valo #include "common.h"
267ac9a364SKalle Valo #include "4965.h"
277ac9a364SKalle Valo 
287951a3bfSLee Jones /*
297ac9a364SKalle Valo  * il_verify_inst_sparse - verify runtime uCode image in card vs. host,
307ac9a364SKalle Valo  *   using sample data 100 bytes apart.  If these sample points are good,
317ac9a364SKalle Valo  *   it's a pretty good bet that everything between them is good, too.
327ac9a364SKalle Valo  */
337ac9a364SKalle Valo static int
il4965_verify_inst_sparse(struct il_priv * il,__le32 * image,u32 len)347ac9a364SKalle Valo il4965_verify_inst_sparse(struct il_priv *il, __le32 * image, u32 len)
357ac9a364SKalle Valo {
367ac9a364SKalle Valo 	u32 val;
377ac9a364SKalle Valo 	int ret = 0;
387ac9a364SKalle Valo 	u32 errcnt = 0;
397ac9a364SKalle Valo 	u32 i;
407ac9a364SKalle Valo 
417ac9a364SKalle Valo 	D_INFO("ucode inst image size is %u\n", len);
427ac9a364SKalle Valo 
437ac9a364SKalle Valo 	for (i = 0; i < len; i += 100, image += 100 / sizeof(u32)) {
447ac9a364SKalle Valo 		/* read data comes through single port, auto-incr addr */
457ac9a364SKalle Valo 		/* NOTE: Use the debugless read so we don't flood kernel log
467ac9a364SKalle Valo 		 * if IL_DL_IO is set */
477ac9a364SKalle Valo 		il_wr(il, HBUS_TARG_MEM_RADDR, i + IL4965_RTC_INST_LOWER_BOUND);
487ac9a364SKalle Valo 		val = _il_rd(il, HBUS_TARG_MEM_RDAT);
497ac9a364SKalle Valo 		if (val != le32_to_cpu(*image)) {
507ac9a364SKalle Valo 			ret = -EIO;
517ac9a364SKalle Valo 			errcnt++;
527ac9a364SKalle Valo 			if (errcnt >= 3)
537ac9a364SKalle Valo 				break;
547ac9a364SKalle Valo 		}
557ac9a364SKalle Valo 	}
567ac9a364SKalle Valo 
577ac9a364SKalle Valo 	return ret;
587ac9a364SKalle Valo }
597ac9a364SKalle Valo 
607951a3bfSLee Jones /*
617ac9a364SKalle Valo  * il4965_verify_inst_full - verify runtime uCode image in card vs. host,
627ac9a364SKalle Valo  *     looking at all data.
637ac9a364SKalle Valo  */
647ac9a364SKalle Valo static int
il4965_verify_inst_full(struct il_priv * il,__le32 * image,u32 len)657ac9a364SKalle Valo il4965_verify_inst_full(struct il_priv *il, __le32 * image, u32 len)
667ac9a364SKalle Valo {
677ac9a364SKalle Valo 	u32 val;
687ac9a364SKalle Valo 	u32 save_len = len;
697ac9a364SKalle Valo 	int ret = 0;
707ac9a364SKalle Valo 	u32 errcnt;
717ac9a364SKalle Valo 
727ac9a364SKalle Valo 	D_INFO("ucode inst image size is %u\n", len);
737ac9a364SKalle Valo 
747ac9a364SKalle Valo 	il_wr(il, HBUS_TARG_MEM_RADDR, IL4965_RTC_INST_LOWER_BOUND);
757ac9a364SKalle Valo 
767ac9a364SKalle Valo 	errcnt = 0;
777ac9a364SKalle Valo 	for (; len > 0; len -= sizeof(u32), image++) {
787ac9a364SKalle Valo 		/* read data comes through single port, auto-incr addr */
797ac9a364SKalle Valo 		/* NOTE: Use the debugless read so we don't flood kernel log
807ac9a364SKalle Valo 		 * if IL_DL_IO is set */
817ac9a364SKalle Valo 		val = _il_rd(il, HBUS_TARG_MEM_RDAT);
827ac9a364SKalle Valo 		if (val != le32_to_cpu(*image)) {
837ac9a364SKalle Valo 			IL_ERR("uCode INST section is invalid at "
847ac9a364SKalle Valo 			       "offset 0x%x, is 0x%x, s/b 0x%x\n",
857ac9a364SKalle Valo 			       save_len - len, val, le32_to_cpu(*image));
867ac9a364SKalle Valo 			ret = -EIO;
877ac9a364SKalle Valo 			errcnt++;
887ac9a364SKalle Valo 			if (errcnt >= 20)
897ac9a364SKalle Valo 				break;
907ac9a364SKalle Valo 		}
917ac9a364SKalle Valo 	}
927ac9a364SKalle Valo 
937ac9a364SKalle Valo 	if (!errcnt)
947ac9a364SKalle Valo 		D_INFO("ucode image in INSTRUCTION memory is good\n");
957ac9a364SKalle Valo 
967ac9a364SKalle Valo 	return ret;
977ac9a364SKalle Valo }
987ac9a364SKalle Valo 
997951a3bfSLee Jones /*
1007ac9a364SKalle Valo  * il4965_verify_ucode - determine which instruction image is in SRAM,
1017ac9a364SKalle Valo  *    and verify its contents
1027ac9a364SKalle Valo  */
1037ac9a364SKalle Valo int
il4965_verify_ucode(struct il_priv * il)1047ac9a364SKalle Valo il4965_verify_ucode(struct il_priv *il)
1057ac9a364SKalle Valo {
1067ac9a364SKalle Valo 	__le32 *image;
1077ac9a364SKalle Valo 	u32 len;
1087ac9a364SKalle Valo 	int ret;
1097ac9a364SKalle Valo 
1107ac9a364SKalle Valo 	/* Try bootstrap */
1117ac9a364SKalle Valo 	image = (__le32 *) il->ucode_boot.v_addr;
1127ac9a364SKalle Valo 	len = il->ucode_boot.len;
1137ac9a364SKalle Valo 	ret = il4965_verify_inst_sparse(il, image, len);
1147ac9a364SKalle Valo 	if (!ret) {
1157ac9a364SKalle Valo 		D_INFO("Bootstrap uCode is good in inst SRAM\n");
1167ac9a364SKalle Valo 		return 0;
1177ac9a364SKalle Valo 	}
1187ac9a364SKalle Valo 
1197ac9a364SKalle Valo 	/* Try initialize */
1207ac9a364SKalle Valo 	image = (__le32 *) il->ucode_init.v_addr;
1217ac9a364SKalle Valo 	len = il->ucode_init.len;
1227ac9a364SKalle Valo 	ret = il4965_verify_inst_sparse(il, image, len);
1237ac9a364SKalle Valo 	if (!ret) {
1247ac9a364SKalle Valo 		D_INFO("Initialize uCode is good in inst SRAM\n");
1257ac9a364SKalle Valo 		return 0;
1267ac9a364SKalle Valo 	}
1277ac9a364SKalle Valo 
1287ac9a364SKalle Valo 	/* Try runtime/protocol */
1297ac9a364SKalle Valo 	image = (__le32 *) il->ucode_code.v_addr;
1307ac9a364SKalle Valo 	len = il->ucode_code.len;
1317ac9a364SKalle Valo 	ret = il4965_verify_inst_sparse(il, image, len);
1327ac9a364SKalle Valo 	if (!ret) {
1337ac9a364SKalle Valo 		D_INFO("Runtime uCode is good in inst SRAM\n");
1347ac9a364SKalle Valo 		return 0;
1357ac9a364SKalle Valo 	}
1367ac9a364SKalle Valo 
1377ac9a364SKalle Valo 	IL_ERR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
1387ac9a364SKalle Valo 
1397ac9a364SKalle Valo 	/* Since nothing seems to match, show first several data entries in
1407ac9a364SKalle Valo 	 * instruction SRAM, so maybe visual inspection will give a clue.
1417ac9a364SKalle Valo 	 * Selection of bootstrap image (vs. other images) is arbitrary. */
1427ac9a364SKalle Valo 	image = (__le32 *) il->ucode_boot.v_addr;
1437ac9a364SKalle Valo 	len = il->ucode_boot.len;
1447ac9a364SKalle Valo 	ret = il4965_verify_inst_full(il, image, len);
1457ac9a364SKalle Valo 
1467ac9a364SKalle Valo 	return ret;
1477ac9a364SKalle Valo }
1487ac9a364SKalle Valo 
1497ac9a364SKalle Valo /******************************************************************************
1507ac9a364SKalle Valo  *
1517ac9a364SKalle Valo  * EEPROM related functions
1527ac9a364SKalle Valo  *
1537ac9a364SKalle Valo ******************************************************************************/
1547ac9a364SKalle Valo 
1557ac9a364SKalle Valo /*
1567ac9a364SKalle Valo  * The device's EEPROM semaphore prevents conflicts between driver and uCode
1577ac9a364SKalle Valo  * when accessing the EEPROM; each access is a series of pulses to/from the
1587ac9a364SKalle Valo  * EEPROM chip, not a single event, so even reads could conflict if they
1597ac9a364SKalle Valo  * weren't arbitrated by the semaphore.
1607ac9a364SKalle Valo  */
1617ac9a364SKalle Valo int
il4965_eeprom_acquire_semaphore(struct il_priv * il)1627ac9a364SKalle Valo il4965_eeprom_acquire_semaphore(struct il_priv *il)
1637ac9a364SKalle Valo {
1647ac9a364SKalle Valo 	u16 count;
1657ac9a364SKalle Valo 	int ret;
1667ac9a364SKalle Valo 
1677ac9a364SKalle Valo 	for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
1687ac9a364SKalle Valo 		/* Request semaphore */
1697ac9a364SKalle Valo 		il_set_bit(il, CSR_HW_IF_CONFIG_REG,
1707ac9a364SKalle Valo 			   CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1717ac9a364SKalle Valo 
1727ac9a364SKalle Valo 		/* See if we got it */
1737ac9a364SKalle Valo 		ret =
1747ac9a364SKalle Valo 		    _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
1757ac9a364SKalle Valo 				 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
1767ac9a364SKalle Valo 				 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
1777ac9a364SKalle Valo 				 EEPROM_SEM_TIMEOUT);
1787ac9a364SKalle Valo 		if (ret >= 0)
1797ac9a364SKalle Valo 			return ret;
1807ac9a364SKalle Valo 	}
1817ac9a364SKalle Valo 
1827ac9a364SKalle Valo 	return ret;
1837ac9a364SKalle Valo }
1847ac9a364SKalle Valo 
1857ac9a364SKalle Valo void
il4965_eeprom_release_semaphore(struct il_priv * il)1867ac9a364SKalle Valo il4965_eeprom_release_semaphore(struct il_priv *il)
1877ac9a364SKalle Valo {
1887ac9a364SKalle Valo 	il_clear_bit(il, CSR_HW_IF_CONFIG_REG,
1897ac9a364SKalle Valo 		     CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1907ac9a364SKalle Valo 
1917ac9a364SKalle Valo }
1927ac9a364SKalle Valo 
1937ac9a364SKalle Valo int
il4965_eeprom_check_version(struct il_priv * il)1947ac9a364SKalle Valo il4965_eeprom_check_version(struct il_priv *il)
1957ac9a364SKalle Valo {
1967ac9a364SKalle Valo 	u16 eeprom_ver;
1977ac9a364SKalle Valo 	u16 calib_ver;
1987ac9a364SKalle Valo 
1997ac9a364SKalle Valo 	eeprom_ver = il_eeprom_query16(il, EEPROM_VERSION);
2007ac9a364SKalle Valo 	calib_ver = il_eeprom_query16(il, EEPROM_4965_CALIB_VERSION_OFFSET);
2017ac9a364SKalle Valo 
2027ac9a364SKalle Valo 	if (eeprom_ver < il->cfg->eeprom_ver ||
2037ac9a364SKalle Valo 	    calib_ver < il->cfg->eeprom_calib_ver)
2047ac9a364SKalle Valo 		goto err;
2057ac9a364SKalle Valo 
2067ac9a364SKalle Valo 	IL_INFO("device EEPROM VER=0x%x, CALIB=0x%x\n", eeprom_ver, calib_ver);
2077ac9a364SKalle Valo 
2087ac9a364SKalle Valo 	return 0;
2097ac9a364SKalle Valo err:
2107ac9a364SKalle Valo 	IL_ERR("Unsupported (too old) EEPROM VER=0x%x < 0x%x "
2117ac9a364SKalle Valo 	       "CALIB=0x%x < 0x%x\n", eeprom_ver, il->cfg->eeprom_ver,
2127ac9a364SKalle Valo 	       calib_ver, il->cfg->eeprom_calib_ver);
2137ac9a364SKalle Valo 	return -EINVAL;
2147ac9a364SKalle Valo 
2157ac9a364SKalle Valo }
2167ac9a364SKalle Valo 
2177ac9a364SKalle Valo void
il4965_eeprom_get_mac(const struct il_priv * il,u8 * mac)2187ac9a364SKalle Valo il4965_eeprom_get_mac(const struct il_priv *il, u8 * mac)
2197ac9a364SKalle Valo {
2207ac9a364SKalle Valo 	const u8 *addr = il_eeprom_query_addr(il,
2217ac9a364SKalle Valo 					      EEPROM_MAC_ADDRESS);
2227ac9a364SKalle Valo 	memcpy(mac, addr, ETH_ALEN);
2237ac9a364SKalle Valo }
2247ac9a364SKalle Valo 
2257ac9a364SKalle Valo /* Send led command */
2267ac9a364SKalle Valo static int
il4965_send_led_cmd(struct il_priv * il,struct il_led_cmd * led_cmd)2277ac9a364SKalle Valo il4965_send_led_cmd(struct il_priv *il, struct il_led_cmd *led_cmd)
2287ac9a364SKalle Valo {
2297ac9a364SKalle Valo 	struct il_host_cmd cmd = {
2307ac9a364SKalle Valo 		.id = C_LEDS,
2317ac9a364SKalle Valo 		.len = sizeof(struct il_led_cmd),
2327ac9a364SKalle Valo 		.data = led_cmd,
2337ac9a364SKalle Valo 		.flags = CMD_ASYNC,
2347ac9a364SKalle Valo 		.callback = NULL,
2357ac9a364SKalle Valo 	};
2367ac9a364SKalle Valo 	u32 reg;
2377ac9a364SKalle Valo 
2387ac9a364SKalle Valo 	reg = _il_rd(il, CSR_LED_REG);
2397ac9a364SKalle Valo 	if (reg != (reg & CSR_LED_BSM_CTRL_MSK))
2407ac9a364SKalle Valo 		_il_wr(il, CSR_LED_REG, reg & CSR_LED_BSM_CTRL_MSK);
2417ac9a364SKalle Valo 
2427ac9a364SKalle Valo 	return il_send_cmd(il, &cmd);
2437ac9a364SKalle Valo }
2447ac9a364SKalle Valo 
2457ac9a364SKalle Valo /* Set led register off */
2467ac9a364SKalle Valo void
il4965_led_enable(struct il_priv * il)2477ac9a364SKalle Valo il4965_led_enable(struct il_priv *il)
2487ac9a364SKalle Valo {
2497ac9a364SKalle Valo 	_il_wr(il, CSR_LED_REG, CSR_LED_REG_TRUN_ON);
2507ac9a364SKalle Valo }
2517ac9a364SKalle Valo 
2527ac9a364SKalle Valo static int il4965_send_tx_power(struct il_priv *il);
2537ac9a364SKalle Valo static int il4965_hw_get_temperature(struct il_priv *il);
2547ac9a364SKalle Valo 
2557ac9a364SKalle Valo /* Highest firmware API version supported */
2567ac9a364SKalle Valo #define IL4965_UCODE_API_MAX 2
2577ac9a364SKalle Valo 
2587ac9a364SKalle Valo /* Lowest firmware API version supported */
2597ac9a364SKalle Valo #define IL4965_UCODE_API_MIN 2
2607ac9a364SKalle Valo 
2617ac9a364SKalle Valo #define IL4965_FW_PRE "iwlwifi-4965-"
2627ac9a364SKalle Valo #define _IL4965_MODULE_FIRMWARE(api) IL4965_FW_PRE #api ".ucode"
2637ac9a364SKalle Valo #define IL4965_MODULE_FIRMWARE(api) _IL4965_MODULE_FIRMWARE(api)
2647ac9a364SKalle Valo 
2657ac9a364SKalle Valo /* check contents of special bootstrap uCode SRAM */
2667ac9a364SKalle Valo static int
il4965_verify_bsm(struct il_priv * il)2677ac9a364SKalle Valo il4965_verify_bsm(struct il_priv *il)
2687ac9a364SKalle Valo {
2697ac9a364SKalle Valo 	__le32 *image = il->ucode_boot.v_addr;
2707ac9a364SKalle Valo 	u32 len = il->ucode_boot.len;
2717ac9a364SKalle Valo 	u32 reg;
2727ac9a364SKalle Valo 	u32 val;
2737ac9a364SKalle Valo 
2747ac9a364SKalle Valo 	D_INFO("Begin verify bsm\n");
2757ac9a364SKalle Valo 
2767ac9a364SKalle Valo 	/* verify BSM SRAM contents */
2777ac9a364SKalle Valo 	val = il_rd_prph(il, BSM_WR_DWCOUNT_REG);
2787ac9a364SKalle Valo 	for (reg = BSM_SRAM_LOWER_BOUND; reg < BSM_SRAM_LOWER_BOUND + len;
2797ac9a364SKalle Valo 	     reg += sizeof(u32), image++) {
2807ac9a364SKalle Valo 		val = il_rd_prph(il, reg);
2817ac9a364SKalle Valo 		if (val != le32_to_cpu(*image)) {
2827ac9a364SKalle Valo 			IL_ERR("BSM uCode verification failed at "
2837ac9a364SKalle Valo 			       "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
2847ac9a364SKalle Valo 			       BSM_SRAM_LOWER_BOUND, reg - BSM_SRAM_LOWER_BOUND,
2857ac9a364SKalle Valo 			       len, val, le32_to_cpu(*image));
2867ac9a364SKalle Valo 			return -EIO;
2877ac9a364SKalle Valo 		}
2887ac9a364SKalle Valo 	}
2897ac9a364SKalle Valo 
2907ac9a364SKalle Valo 	D_INFO("BSM bootstrap uCode image OK\n");
2917ac9a364SKalle Valo 
2927ac9a364SKalle Valo 	return 0;
2937ac9a364SKalle Valo }
2947ac9a364SKalle Valo 
2957951a3bfSLee Jones /*
2967ac9a364SKalle Valo  * il4965_load_bsm - Load bootstrap instructions
2977ac9a364SKalle Valo  *
2987ac9a364SKalle Valo  * BSM operation:
2997ac9a364SKalle Valo  *
3007ac9a364SKalle Valo  * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
3017ac9a364SKalle Valo  * in special SRAM that does not power down during RFKILL.  When powering back
3027ac9a364SKalle Valo  * up after power-saving sleeps (or during initial uCode load), the BSM loads
3037ac9a364SKalle Valo  * the bootstrap program into the on-board processor, and starts it.
3047ac9a364SKalle Valo  *
3057ac9a364SKalle Valo  * The bootstrap program loads (via DMA) instructions and data for a new
3067ac9a364SKalle Valo  * program from host DRAM locations indicated by the host driver in the
3077ac9a364SKalle Valo  * BSM_DRAM_* registers.  Once the new program is loaded, it starts
3087ac9a364SKalle Valo  * automatically.
3097ac9a364SKalle Valo  *
3107ac9a364SKalle Valo  * When initializing the NIC, the host driver points the BSM to the
3117ac9a364SKalle Valo  * "initialize" uCode image.  This uCode sets up some internal data, then
3127ac9a364SKalle Valo  * notifies host via "initialize alive" that it is complete.
3137ac9a364SKalle Valo  *
3147ac9a364SKalle Valo  * The host then replaces the BSM_DRAM_* pointer values to point to the
3157ac9a364SKalle Valo  * normal runtime uCode instructions and a backup uCode data cache buffer
3167ac9a364SKalle Valo  * (filled initially with starting data values for the on-board processor),
3177ac9a364SKalle Valo  * then triggers the "initialize" uCode to load and launch the runtime uCode,
3187ac9a364SKalle Valo  * which begins normal operation.
3197ac9a364SKalle Valo  *
3207ac9a364SKalle Valo  * When doing a power-save shutdown, runtime uCode saves data SRAM into
3217ac9a364SKalle Valo  * the backup data cache in DRAM before SRAM is powered down.
3227ac9a364SKalle Valo  *
3237ac9a364SKalle Valo  * When powering back up, the BSM loads the bootstrap program.  This reloads
3247ac9a364SKalle Valo  * the runtime uCode instructions and the backup data cache into SRAM,
3257ac9a364SKalle Valo  * and re-launches the runtime uCode from where it left off.
3267ac9a364SKalle Valo  */
3277ac9a364SKalle Valo static int
il4965_load_bsm(struct il_priv * il)3287ac9a364SKalle Valo il4965_load_bsm(struct il_priv *il)
3297ac9a364SKalle Valo {
3307ac9a364SKalle Valo 	__le32 *image = il->ucode_boot.v_addr;
3317ac9a364SKalle Valo 	u32 len = il->ucode_boot.len;
3327ac9a364SKalle Valo 	dma_addr_t pinst;
3337ac9a364SKalle Valo 	dma_addr_t pdata;
3347ac9a364SKalle Valo 	u32 inst_len;
3357ac9a364SKalle Valo 	u32 data_len;
3367ac9a364SKalle Valo 	int i;
3377ac9a364SKalle Valo 	u32 done;
3387ac9a364SKalle Valo 	u32 reg_offset;
3397ac9a364SKalle Valo 	int ret;
3407ac9a364SKalle Valo 
3417ac9a364SKalle Valo 	D_INFO("Begin load bsm\n");
3427ac9a364SKalle Valo 
3437ac9a364SKalle Valo 	il->ucode_type = UCODE_RT;
3447ac9a364SKalle Valo 
3457ac9a364SKalle Valo 	/* make sure bootstrap program is no larger than BSM's SRAM size */
3467ac9a364SKalle Valo 	if (len > IL49_MAX_BSM_SIZE)
3477ac9a364SKalle Valo 		return -EINVAL;
3487ac9a364SKalle Valo 
3497ac9a364SKalle Valo 	/* Tell bootstrap uCode where to find the "Initialize" uCode
3507ac9a364SKalle Valo 	 *   in host DRAM ... host DRAM physical address bits 35:4 for 4965.
3517ac9a364SKalle Valo 	 * NOTE:  il_init_alive_start() will replace these values,
3527ac9a364SKalle Valo 	 *        after the "initialize" uCode has run, to point to
3537ac9a364SKalle Valo 	 *        runtime/protocol instructions and backup data cache.
3547ac9a364SKalle Valo 	 */
3557ac9a364SKalle Valo 	pinst = il->ucode_init.p_addr >> 4;
3567ac9a364SKalle Valo 	pdata = il->ucode_init_data.p_addr >> 4;
3577ac9a364SKalle Valo 	inst_len = il->ucode_init.len;
3587ac9a364SKalle Valo 	data_len = il->ucode_init_data.len;
3597ac9a364SKalle Valo 
3607ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_INST_PTR_REG, pinst);
3617ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_DATA_PTR_REG, pdata);
3627ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
3637ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
3647ac9a364SKalle Valo 
3657ac9a364SKalle Valo 	/* Fill BSM memory with bootstrap instructions */
3667ac9a364SKalle Valo 	for (reg_offset = BSM_SRAM_LOWER_BOUND;
3677ac9a364SKalle Valo 	     reg_offset < BSM_SRAM_LOWER_BOUND + len;
3687ac9a364SKalle Valo 	     reg_offset += sizeof(u32), image++)
3697ac9a364SKalle Valo 		_il_wr_prph(il, reg_offset, le32_to_cpu(*image));
3707ac9a364SKalle Valo 
3717ac9a364SKalle Valo 	ret = il4965_verify_bsm(il);
3727ac9a364SKalle Valo 	if (ret)
3737ac9a364SKalle Valo 		return ret;
3747ac9a364SKalle Valo 
3757ac9a364SKalle Valo 	/* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
3767ac9a364SKalle Valo 	il_wr_prph(il, BSM_WR_MEM_SRC_REG, 0x0);
3777ac9a364SKalle Valo 	il_wr_prph(il, BSM_WR_MEM_DST_REG, IL49_RTC_INST_LOWER_BOUND);
3787ac9a364SKalle Valo 	il_wr_prph(il, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
3797ac9a364SKalle Valo 
3807ac9a364SKalle Valo 	/* Load bootstrap code into instruction SRAM now,
3817ac9a364SKalle Valo 	 *   to prepare to load "initialize" uCode */
3827ac9a364SKalle Valo 	il_wr_prph(il, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
3837ac9a364SKalle Valo 
3847ac9a364SKalle Valo 	/* Wait for load of bootstrap uCode to finish */
3857ac9a364SKalle Valo 	for (i = 0; i < 100; i++) {
3867ac9a364SKalle Valo 		done = il_rd_prph(il, BSM_WR_CTRL_REG);
3877ac9a364SKalle Valo 		if (!(done & BSM_WR_CTRL_REG_BIT_START))
3887ac9a364SKalle Valo 			break;
3897ac9a364SKalle Valo 		udelay(10);
3907ac9a364SKalle Valo 	}
3917ac9a364SKalle Valo 	if (i < 100)
3927ac9a364SKalle Valo 		D_INFO("BSM write complete, poll %d iterations\n", i);
3937ac9a364SKalle Valo 	else {
3947ac9a364SKalle Valo 		IL_ERR("BSM write did not complete!\n");
3957ac9a364SKalle Valo 		return -EIO;
3967ac9a364SKalle Valo 	}
3977ac9a364SKalle Valo 
3987ac9a364SKalle Valo 	/* Enable future boot loads whenever power management unit triggers it
3997ac9a364SKalle Valo 	 *   (e.g. when powering back up after power-save shutdown) */
4007ac9a364SKalle Valo 	il_wr_prph(il, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
4017ac9a364SKalle Valo 
4027ac9a364SKalle Valo 	return 0;
4037ac9a364SKalle Valo }
4047ac9a364SKalle Valo 
4057951a3bfSLee Jones /*
4067ac9a364SKalle Valo  * il4965_set_ucode_ptrs - Set uCode address location
4077ac9a364SKalle Valo  *
4087ac9a364SKalle Valo  * Tell initialization uCode where to find runtime uCode.
4097ac9a364SKalle Valo  *
4107ac9a364SKalle Valo  * BSM registers initially contain pointers to initialization uCode.
4117ac9a364SKalle Valo  * We need to replace them to load runtime uCode inst and data,
4127ac9a364SKalle Valo  * and to save runtime data when powering down.
4137ac9a364SKalle Valo  */
4147ac9a364SKalle Valo static int
il4965_set_ucode_ptrs(struct il_priv * il)4157ac9a364SKalle Valo il4965_set_ucode_ptrs(struct il_priv *il)
4167ac9a364SKalle Valo {
4177ac9a364SKalle Valo 	dma_addr_t pinst;
4187ac9a364SKalle Valo 	dma_addr_t pdata;
4197ac9a364SKalle Valo 
4207ac9a364SKalle Valo 	/* bits 35:4 for 4965 */
4217ac9a364SKalle Valo 	pinst = il->ucode_code.p_addr >> 4;
4227ac9a364SKalle Valo 	pdata = il->ucode_data_backup.p_addr >> 4;
4237ac9a364SKalle Valo 
4247ac9a364SKalle Valo 	/* Tell bootstrap uCode where to find image to load */
4257ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_INST_PTR_REG, pinst);
4267ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_DATA_PTR_REG, pdata);
4277ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_DATA_BYTECOUNT_REG, il->ucode_data.len);
4287ac9a364SKalle Valo 
4297ac9a364SKalle Valo 	/* Inst byte count must be last to set up, bit 31 signals uCode
4307ac9a364SKalle Valo 	 *   that all new ptr/size info is in place */
4317ac9a364SKalle Valo 	il_wr_prph(il, BSM_DRAM_INST_BYTECOUNT_REG,
4327ac9a364SKalle Valo 		   il->ucode_code.len | BSM_DRAM_INST_LOAD);
4337ac9a364SKalle Valo 	D_INFO("Runtime uCode pointers are set.\n");
4347ac9a364SKalle Valo 
43531044326SXu Wang 	return 0;
4367ac9a364SKalle Valo }
4377ac9a364SKalle Valo 
4387951a3bfSLee Jones /*
4397ac9a364SKalle Valo  * il4965_init_alive_start - Called after N_ALIVE notification received
4407ac9a364SKalle Valo  *
4417ac9a364SKalle Valo  * Called after N_ALIVE notification received from "initialize" uCode.
4427ac9a364SKalle Valo  *
4437ac9a364SKalle Valo  * The 4965 "initialize" ALIVE reply contains calibration data for:
4447ac9a364SKalle Valo  *   Voltage, temperature, and MIMO tx gain correction, now stored in il
4457ac9a364SKalle Valo  *   (3945 does not contain this data).
4467ac9a364SKalle Valo  *
4477ac9a364SKalle Valo  * Tell "initialize" uCode to go ahead and load the runtime uCode.
4487ac9a364SKalle Valo */
4497ac9a364SKalle Valo static void
il4965_init_alive_start(struct il_priv * il)4507ac9a364SKalle Valo il4965_init_alive_start(struct il_priv *il)
4517ac9a364SKalle Valo {
4527ac9a364SKalle Valo 	/* Bootstrap uCode has loaded initialize uCode ... verify inst image.
4537ac9a364SKalle Valo 	 * This is a paranoid check, because we would not have gotten the
4547ac9a364SKalle Valo 	 * "initialize" alive if code weren't properly loaded.  */
4557ac9a364SKalle Valo 	if (il4965_verify_ucode(il)) {
4567ac9a364SKalle Valo 		/* Runtime instruction load was bad;
4577ac9a364SKalle Valo 		 * take it all the way back down so we can try again */
4587ac9a364SKalle Valo 		D_INFO("Bad \"initialize\" uCode load.\n");
4597ac9a364SKalle Valo 		goto restart;
4607ac9a364SKalle Valo 	}
4617ac9a364SKalle Valo 
4627ac9a364SKalle Valo 	/* Calculate temperature */
4637ac9a364SKalle Valo 	il->temperature = il4965_hw_get_temperature(il);
4647ac9a364SKalle Valo 
4657ac9a364SKalle Valo 	/* Send pointers to protocol/runtime uCode image ... init code will
4667ac9a364SKalle Valo 	 * load and launch runtime uCode, which will send us another "Alive"
4677ac9a364SKalle Valo 	 * notification. */
4687ac9a364SKalle Valo 	D_INFO("Initialization Alive received.\n");
4697ac9a364SKalle Valo 	if (il4965_set_ucode_ptrs(il)) {
4707ac9a364SKalle Valo 		/* Runtime instruction load won't happen;
4717ac9a364SKalle Valo 		 * take it all the way back down so we can try again */
4727ac9a364SKalle Valo 		D_INFO("Couldn't set up uCode pointers.\n");
4737ac9a364SKalle Valo 		goto restart;
4747ac9a364SKalle Valo 	}
4757ac9a364SKalle Valo 	return;
4767ac9a364SKalle Valo 
4777ac9a364SKalle Valo restart:
4787ac9a364SKalle Valo 	queue_work(il->workqueue, &il->restart);
4797ac9a364SKalle Valo }
4807ac9a364SKalle Valo 
4817ac9a364SKalle Valo static bool
iw4965_is_ht40_channel(__le32 rxon_flags)4827ac9a364SKalle Valo iw4965_is_ht40_channel(__le32 rxon_flags)
4837ac9a364SKalle Valo {
4847ac9a364SKalle Valo 	int chan_mod =
4857ac9a364SKalle Valo 	    le32_to_cpu(rxon_flags & RXON_FLG_CHANNEL_MODE_MSK) >>
4867ac9a364SKalle Valo 	    RXON_FLG_CHANNEL_MODE_POS;
4877ac9a364SKalle Valo 	return (chan_mod == CHANNEL_MODE_PURE_40 ||
4887ac9a364SKalle Valo 		chan_mod == CHANNEL_MODE_MIXED);
4897ac9a364SKalle Valo }
4907ac9a364SKalle Valo 
4917ac9a364SKalle Valo void
il4965_nic_config(struct il_priv * il)4927ac9a364SKalle Valo il4965_nic_config(struct il_priv *il)
4937ac9a364SKalle Valo {
4947ac9a364SKalle Valo 	unsigned long flags;
4957ac9a364SKalle Valo 	u16 radio_cfg;
4967ac9a364SKalle Valo 
4977ac9a364SKalle Valo 	spin_lock_irqsave(&il->lock, flags);
4987ac9a364SKalle Valo 
4997ac9a364SKalle Valo 	radio_cfg = il_eeprom_query16(il, EEPROM_RADIO_CONFIG);
5007ac9a364SKalle Valo 
5017ac9a364SKalle Valo 	/* write radio config values to register */
5027ac9a364SKalle Valo 	if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
5037ac9a364SKalle Valo 		il_set_bit(il, CSR_HW_IF_CONFIG_REG,
5047ac9a364SKalle Valo 			   EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
5057ac9a364SKalle Valo 			   EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
5067ac9a364SKalle Valo 			   EEPROM_RF_CFG_DASH_MSK(radio_cfg));
5077ac9a364SKalle Valo 
5087ac9a364SKalle Valo 	/* set CSR_HW_CONFIG_REG for uCode use */
5097ac9a364SKalle Valo 	il_set_bit(il, CSR_HW_IF_CONFIG_REG,
5107ac9a364SKalle Valo 		   CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
5117ac9a364SKalle Valo 		   CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
5127ac9a364SKalle Valo 
5137ac9a364SKalle Valo 	il->calib_info =
5147ac9a364SKalle Valo 	    (struct il_eeprom_calib_info *)
5157ac9a364SKalle Valo 	    il_eeprom_query_addr(il, EEPROM_4965_CALIB_TXPOWER_OFFSET);
5167ac9a364SKalle Valo 
5177ac9a364SKalle Valo 	spin_unlock_irqrestore(&il->lock, flags);
5187ac9a364SKalle Valo }
5197ac9a364SKalle Valo 
5207ac9a364SKalle Valo /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
5217ac9a364SKalle Valo  * Called after every association, but this runs only once!
5227ac9a364SKalle Valo  *  ... once chain noise is calibrated the first time, it's good forever.  */
5237ac9a364SKalle Valo static void
il4965_chain_noise_reset(struct il_priv * il)5247ac9a364SKalle Valo il4965_chain_noise_reset(struct il_priv *il)
5257ac9a364SKalle Valo {
5267ac9a364SKalle Valo 	struct il_chain_noise_data *data = &(il->chain_noise_data);
5277ac9a364SKalle Valo 
5287ac9a364SKalle Valo 	if (data->state == IL_CHAIN_NOISE_ALIVE && il_is_any_associated(il)) {
5297ac9a364SKalle Valo 		struct il_calib_diff_gain_cmd cmd;
5307ac9a364SKalle Valo 
5317ac9a364SKalle Valo 		/* clear data for chain noise calibration algorithm */
5327ac9a364SKalle Valo 		data->chain_noise_a = 0;
5337ac9a364SKalle Valo 		data->chain_noise_b = 0;
5347ac9a364SKalle Valo 		data->chain_noise_c = 0;
5357ac9a364SKalle Valo 		data->chain_signal_a = 0;
5367ac9a364SKalle Valo 		data->chain_signal_b = 0;
5377ac9a364SKalle Valo 		data->chain_signal_c = 0;
5387ac9a364SKalle Valo 		data->beacon_count = 0;
5397ac9a364SKalle Valo 
5407ac9a364SKalle Valo 		memset(&cmd, 0, sizeof(cmd));
5417ac9a364SKalle Valo 		cmd.hdr.op_code = IL_PHY_CALIBRATE_DIFF_GAIN_CMD;
5427ac9a364SKalle Valo 		cmd.diff_gain_a = 0;
5437ac9a364SKalle Valo 		cmd.diff_gain_b = 0;
5447ac9a364SKalle Valo 		cmd.diff_gain_c = 0;
5457ac9a364SKalle Valo 		if (il_send_cmd_pdu(il, C_PHY_CALIBRATION, sizeof(cmd), &cmd))
5467ac9a364SKalle Valo 			IL_ERR("Could not send C_PHY_CALIBRATION\n");
5477ac9a364SKalle Valo 		data->state = IL_CHAIN_NOISE_ACCUMULATE;
5487ac9a364SKalle Valo 		D_CALIB("Run chain_noise_calibrate\n");
5497ac9a364SKalle Valo 	}
5507ac9a364SKalle Valo }
5517ac9a364SKalle Valo 
5527ac9a364SKalle Valo static s32
il4965_math_div_round(s32 num,s32 denom,s32 * res)5537ac9a364SKalle Valo il4965_math_div_round(s32 num, s32 denom, s32 * res)
5547ac9a364SKalle Valo {
5557ac9a364SKalle Valo 	s32 sign = 1;
5567ac9a364SKalle Valo 
5577ac9a364SKalle Valo 	if (num < 0) {
5587ac9a364SKalle Valo 		sign = -sign;
5597ac9a364SKalle Valo 		num = -num;
5607ac9a364SKalle Valo 	}
5617ac9a364SKalle Valo 	if (denom < 0) {
5627ac9a364SKalle Valo 		sign = -sign;
5637ac9a364SKalle Valo 		denom = -denom;
5647ac9a364SKalle Valo 	}
5657ac9a364SKalle Valo 	*res = ((num * 2 + denom) / (denom * 2)) * sign;
5667ac9a364SKalle Valo 
5677ac9a364SKalle Valo 	return 1;
5687ac9a364SKalle Valo }
5697ac9a364SKalle Valo 
5707951a3bfSLee Jones /*
5717ac9a364SKalle Valo  * il4965_get_voltage_compensation - Power supply voltage comp for txpower
5727ac9a364SKalle Valo  *
5737ac9a364SKalle Valo  * Determines power supply voltage compensation for txpower calculations.
5747ac9a364SKalle Valo  * Returns number of 1/2-dB steps to subtract from gain table idx,
5757ac9a364SKalle Valo  * to compensate for difference between power supply voltage during
5767ac9a364SKalle Valo  * factory measurements, vs. current power supply voltage.
5777ac9a364SKalle Valo  *
5787ac9a364SKalle Valo  * Voltage indication is higher for lower voltage.
5797ac9a364SKalle Valo  * Lower voltage requires more gain (lower gain table idx).
5807ac9a364SKalle Valo  */
5817ac9a364SKalle Valo static s32
il4965_get_voltage_compensation(s32 eeprom_voltage,s32 current_voltage)5827ac9a364SKalle Valo il4965_get_voltage_compensation(s32 eeprom_voltage, s32 current_voltage)
5837ac9a364SKalle Valo {
5847ac9a364SKalle Valo 	s32 comp = 0;
5857ac9a364SKalle Valo 
5867ac9a364SKalle Valo 	if (TX_POWER_IL_ILLEGAL_VOLTAGE == eeprom_voltage ||
5877ac9a364SKalle Valo 	    TX_POWER_IL_ILLEGAL_VOLTAGE == current_voltage)
5887ac9a364SKalle Valo 		return 0;
5897ac9a364SKalle Valo 
5907ac9a364SKalle Valo 	il4965_math_div_round(current_voltage - eeprom_voltage,
5917ac9a364SKalle Valo 			      TX_POWER_IL_VOLTAGE_CODES_PER_03V, &comp);
5927ac9a364SKalle Valo 
5937ac9a364SKalle Valo 	if (current_voltage > eeprom_voltage)
5947ac9a364SKalle Valo 		comp *= 2;
5957ac9a364SKalle Valo 	if ((comp < -2) || (comp > 2))
5967ac9a364SKalle Valo 		comp = 0;
5977ac9a364SKalle Valo 
5987ac9a364SKalle Valo 	return comp;
5997ac9a364SKalle Valo }
6007ac9a364SKalle Valo 
6017ac9a364SKalle Valo static s32
il4965_get_tx_atten_grp(u16 channel)6027ac9a364SKalle Valo il4965_get_tx_atten_grp(u16 channel)
6037ac9a364SKalle Valo {
6047ac9a364SKalle Valo 	if (channel >= CALIB_IL_TX_ATTEN_GR5_FCH &&
6057ac9a364SKalle Valo 	    channel <= CALIB_IL_TX_ATTEN_GR5_LCH)
6067ac9a364SKalle Valo 		return CALIB_CH_GROUP_5;
6077ac9a364SKalle Valo 
6087ac9a364SKalle Valo 	if (channel >= CALIB_IL_TX_ATTEN_GR1_FCH &&
6097ac9a364SKalle Valo 	    channel <= CALIB_IL_TX_ATTEN_GR1_LCH)
6107ac9a364SKalle Valo 		return CALIB_CH_GROUP_1;
6117ac9a364SKalle Valo 
6127ac9a364SKalle Valo 	if (channel >= CALIB_IL_TX_ATTEN_GR2_FCH &&
6137ac9a364SKalle Valo 	    channel <= CALIB_IL_TX_ATTEN_GR2_LCH)
6147ac9a364SKalle Valo 		return CALIB_CH_GROUP_2;
6157ac9a364SKalle Valo 
6167ac9a364SKalle Valo 	if (channel >= CALIB_IL_TX_ATTEN_GR3_FCH &&
6177ac9a364SKalle Valo 	    channel <= CALIB_IL_TX_ATTEN_GR3_LCH)
6187ac9a364SKalle Valo 		return CALIB_CH_GROUP_3;
6197ac9a364SKalle Valo 
6207ac9a364SKalle Valo 	if (channel >= CALIB_IL_TX_ATTEN_GR4_FCH &&
6217ac9a364SKalle Valo 	    channel <= CALIB_IL_TX_ATTEN_GR4_LCH)
6227ac9a364SKalle Valo 		return CALIB_CH_GROUP_4;
6237ac9a364SKalle Valo 
6247ac9a364SKalle Valo 	return -EINVAL;
6257ac9a364SKalle Valo }
6267ac9a364SKalle Valo 
6277ac9a364SKalle Valo static u32
il4965_get_sub_band(const struct il_priv * il,u32 channel)6287ac9a364SKalle Valo il4965_get_sub_band(const struct il_priv *il, u32 channel)
6297ac9a364SKalle Valo {
6307ac9a364SKalle Valo 	s32 b = -1;
6317ac9a364SKalle Valo 
6327ac9a364SKalle Valo 	for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
6337ac9a364SKalle Valo 		if (il->calib_info->band_info[b].ch_from == 0)
6347ac9a364SKalle Valo 			continue;
6357ac9a364SKalle Valo 
6367ac9a364SKalle Valo 		if (channel >= il->calib_info->band_info[b].ch_from &&
6377ac9a364SKalle Valo 		    channel <= il->calib_info->band_info[b].ch_to)
6387ac9a364SKalle Valo 			break;
6397ac9a364SKalle Valo 	}
6407ac9a364SKalle Valo 
6417ac9a364SKalle Valo 	return b;
6427ac9a364SKalle Valo }
6437ac9a364SKalle Valo 
6447ac9a364SKalle Valo static s32
il4965_interpolate_value(s32 x,s32 x1,s32 y1,s32 x2,s32 y2)6457ac9a364SKalle Valo il4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
6467ac9a364SKalle Valo {
6477ac9a364SKalle Valo 	s32 val;
6487ac9a364SKalle Valo 
6497ac9a364SKalle Valo 	if (x2 == x1)
6507ac9a364SKalle Valo 		return y1;
6517ac9a364SKalle Valo 	else {
6527ac9a364SKalle Valo 		il4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
6537ac9a364SKalle Valo 		return val + y2;
6547ac9a364SKalle Valo 	}
6557ac9a364SKalle Valo }
6567ac9a364SKalle Valo 
6577951a3bfSLee Jones /*
6587ac9a364SKalle Valo  * il4965_interpolate_chan - Interpolate factory measurements for one channel
6597ac9a364SKalle Valo  *
6607ac9a364SKalle Valo  * Interpolates factory measurements from the two sample channels within a
6617ac9a364SKalle Valo  * sub-band, to apply to channel of interest.  Interpolation is proportional to
6627ac9a364SKalle Valo  * differences in channel frequencies, which is proportional to differences
6637ac9a364SKalle Valo  * in channel number.
6647ac9a364SKalle Valo  */
6657ac9a364SKalle Valo static int
il4965_interpolate_chan(struct il_priv * il,u32 channel,struct il_eeprom_calib_ch_info * chan_info)6667ac9a364SKalle Valo il4965_interpolate_chan(struct il_priv *il, u32 channel,
6677ac9a364SKalle Valo 			struct il_eeprom_calib_ch_info *chan_info)
6687ac9a364SKalle Valo {
6697ac9a364SKalle Valo 	s32 s = -1;
6707ac9a364SKalle Valo 	u32 c;
6717ac9a364SKalle Valo 	u32 m;
6727ac9a364SKalle Valo 	const struct il_eeprom_calib_measure *m1;
6737ac9a364SKalle Valo 	const struct il_eeprom_calib_measure *m2;
6747ac9a364SKalle Valo 	struct il_eeprom_calib_measure *omeas;
6757ac9a364SKalle Valo 	u32 ch_i1;
6767ac9a364SKalle Valo 	u32 ch_i2;
6777ac9a364SKalle Valo 
6787ac9a364SKalle Valo 	s = il4965_get_sub_band(il, channel);
6797ac9a364SKalle Valo 	if (s >= EEPROM_TX_POWER_BANDS) {
6807ac9a364SKalle Valo 		IL_ERR("Tx Power can not find channel %d\n", channel);
6817ac9a364SKalle Valo 		return -1;
6827ac9a364SKalle Valo 	}
6837ac9a364SKalle Valo 
6847ac9a364SKalle Valo 	ch_i1 = il->calib_info->band_info[s].ch1.ch_num;
6857ac9a364SKalle Valo 	ch_i2 = il->calib_info->band_info[s].ch2.ch_num;
6867ac9a364SKalle Valo 	chan_info->ch_num = (u8) channel;
6877ac9a364SKalle Valo 
6887ac9a364SKalle Valo 	D_TXPOWER("channel %d subband %d factory cal ch %d & %d\n", channel, s,
6897ac9a364SKalle Valo 		  ch_i1, ch_i2);
6907ac9a364SKalle Valo 
6917ac9a364SKalle Valo 	for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
6927ac9a364SKalle Valo 		for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
6937ac9a364SKalle Valo 			m1 = &(il->calib_info->band_info[s].ch1.
6947ac9a364SKalle Valo 			       measurements[c][m]);
6957ac9a364SKalle Valo 			m2 = &(il->calib_info->band_info[s].ch2.
6967ac9a364SKalle Valo 			       measurements[c][m]);
6977ac9a364SKalle Valo 			omeas = &(chan_info->measurements[c][m]);
6987ac9a364SKalle Valo 
6997ac9a364SKalle Valo 			omeas->actual_pow =
7007ac9a364SKalle Valo 			    (u8) il4965_interpolate_value(channel, ch_i1,
7017ac9a364SKalle Valo 							  m1->actual_pow, ch_i2,
7027ac9a364SKalle Valo 							  m2->actual_pow);
7037ac9a364SKalle Valo 			omeas->gain_idx =
7047ac9a364SKalle Valo 			    (u8) il4965_interpolate_value(channel, ch_i1,
7057ac9a364SKalle Valo 							  m1->gain_idx, ch_i2,
7067ac9a364SKalle Valo 							  m2->gain_idx);
7077ac9a364SKalle Valo 			omeas->temperature =
7087ac9a364SKalle Valo 			    (u8) il4965_interpolate_value(channel, ch_i1,
7097ac9a364SKalle Valo 							  m1->temperature,
7107ac9a364SKalle Valo 							  ch_i2,
7117ac9a364SKalle Valo 							  m2->temperature);
7127ac9a364SKalle Valo 			omeas->pa_det =
7137ac9a364SKalle Valo 			    (s8) il4965_interpolate_value(channel, ch_i1,
7147ac9a364SKalle Valo 							  m1->pa_det, ch_i2,
7157ac9a364SKalle Valo 							  m2->pa_det);
7167ac9a364SKalle Valo 
7177ac9a364SKalle Valo 			D_TXPOWER("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c,
7187ac9a364SKalle Valo 				  m, m1->actual_pow, m2->actual_pow,
7197ac9a364SKalle Valo 				  omeas->actual_pow);
7207ac9a364SKalle Valo 			D_TXPOWER("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c,
7217ac9a364SKalle Valo 				  m, m1->gain_idx, m2->gain_idx,
7227ac9a364SKalle Valo 				  omeas->gain_idx);
7237ac9a364SKalle Valo 			D_TXPOWER("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c,
7247ac9a364SKalle Valo 				  m, m1->pa_det, m2->pa_det, omeas->pa_det);
7257ac9a364SKalle Valo 			D_TXPOWER("chain %d meas %d  T1=%d  T2=%d  T=%d\n", c,
7267ac9a364SKalle Valo 				  m, m1->temperature, m2->temperature,
7277ac9a364SKalle Valo 				  omeas->temperature);
7287ac9a364SKalle Valo 		}
7297ac9a364SKalle Valo 	}
7307ac9a364SKalle Valo 
7317ac9a364SKalle Valo 	return 0;
7327ac9a364SKalle Valo }
7337ac9a364SKalle Valo 
7347ac9a364SKalle Valo /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
7357ac9a364SKalle Valo  * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
7367ac9a364SKalle Valo static s32 back_off_table[] = {
7377ac9a364SKalle Valo 	10, 10, 10, 10, 10, 15, 17, 20,	/* OFDM SISO 20 MHz */
7387ac9a364SKalle Valo 	10, 10, 10, 10, 10, 15, 17, 20,	/* OFDM MIMO 20 MHz */
7397ac9a364SKalle Valo 	10, 10, 10, 10, 10, 15, 17, 20,	/* OFDM SISO 40 MHz */
7407ac9a364SKalle Valo 	10, 10, 10, 10, 10, 15, 17, 20,	/* OFDM MIMO 40 MHz */
7417ac9a364SKalle Valo 	10			/* CCK */
7427ac9a364SKalle Valo };
7437ac9a364SKalle Valo 
7447ac9a364SKalle Valo /* Thermal compensation values for txpower for various frequency ranges ...
7457ac9a364SKalle Valo  *   ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
7467ac9a364SKalle Valo static struct il4965_txpower_comp_entry {
7477ac9a364SKalle Valo 	s32 degrees_per_05db_a;
7487ac9a364SKalle Valo 	s32 degrees_per_05db_a_denom;
7497ac9a364SKalle Valo } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
7507ac9a364SKalle Valo 	{
7517ac9a364SKalle Valo 	9, 2},			/* group 0 5.2, ch  34-43 */
7527ac9a364SKalle Valo 	{
7537ac9a364SKalle Valo 	4, 1},			/* group 1 5.2, ch  44-70 */
7547ac9a364SKalle Valo 	{
7557ac9a364SKalle Valo 	4, 1},			/* group 2 5.2, ch  71-124 */
7567ac9a364SKalle Valo 	{
7577ac9a364SKalle Valo 	4, 1},			/* group 3 5.2, ch 125-200 */
7587ac9a364SKalle Valo 	{
7597ac9a364SKalle Valo 	3, 1}			/* group 4 2.4, ch   all */
7607ac9a364SKalle Valo };
7617ac9a364SKalle Valo 
7627ac9a364SKalle Valo static s32
get_min_power_idx(s32 rate_power_idx,u32 band)7637ac9a364SKalle Valo get_min_power_idx(s32 rate_power_idx, u32 band)
7647ac9a364SKalle Valo {
7657ac9a364SKalle Valo 	if (!band) {
7667ac9a364SKalle Valo 		if ((rate_power_idx & 7) <= 4)
7677ac9a364SKalle Valo 			return MIN_TX_GAIN_IDX_52GHZ_EXT;
7687ac9a364SKalle Valo 	}
7697ac9a364SKalle Valo 	return MIN_TX_GAIN_IDX;
7707ac9a364SKalle Valo }
7717ac9a364SKalle Valo 
7727ac9a364SKalle Valo struct gain_entry {
7737ac9a364SKalle Valo 	u8 dsp;
7747ac9a364SKalle Valo 	u8 radio;
7757ac9a364SKalle Valo };
7767ac9a364SKalle Valo 
7777ac9a364SKalle Valo static const struct gain_entry gain_table[2][108] = {
7787ac9a364SKalle Valo 	/* 5.2GHz power gain idx table */
7797ac9a364SKalle Valo 	{
7807ac9a364SKalle Valo 	 {123, 0x3F},		/* highest txpower */
7817ac9a364SKalle Valo 	 {117, 0x3F},
7827ac9a364SKalle Valo 	 {110, 0x3F},
7837ac9a364SKalle Valo 	 {104, 0x3F},
7847ac9a364SKalle Valo 	 {98, 0x3F},
7857ac9a364SKalle Valo 	 {110, 0x3E},
7867ac9a364SKalle Valo 	 {104, 0x3E},
7877ac9a364SKalle Valo 	 {98, 0x3E},
7887ac9a364SKalle Valo 	 {110, 0x3D},
7897ac9a364SKalle Valo 	 {104, 0x3D},
7907ac9a364SKalle Valo 	 {98, 0x3D},
7917ac9a364SKalle Valo 	 {110, 0x3C},
7927ac9a364SKalle Valo 	 {104, 0x3C},
7937ac9a364SKalle Valo 	 {98, 0x3C},
7947ac9a364SKalle Valo 	 {110, 0x3B},
7957ac9a364SKalle Valo 	 {104, 0x3B},
7967ac9a364SKalle Valo 	 {98, 0x3B},
7977ac9a364SKalle Valo 	 {110, 0x3A},
7987ac9a364SKalle Valo 	 {104, 0x3A},
7997ac9a364SKalle Valo 	 {98, 0x3A},
8007ac9a364SKalle Valo 	 {110, 0x39},
8017ac9a364SKalle Valo 	 {104, 0x39},
8027ac9a364SKalle Valo 	 {98, 0x39},
8037ac9a364SKalle Valo 	 {110, 0x38},
8047ac9a364SKalle Valo 	 {104, 0x38},
8057ac9a364SKalle Valo 	 {98, 0x38},
8067ac9a364SKalle Valo 	 {110, 0x37},
8077ac9a364SKalle Valo 	 {104, 0x37},
8087ac9a364SKalle Valo 	 {98, 0x37},
8097ac9a364SKalle Valo 	 {110, 0x36},
8107ac9a364SKalle Valo 	 {104, 0x36},
8117ac9a364SKalle Valo 	 {98, 0x36},
8127ac9a364SKalle Valo 	 {110, 0x35},
8137ac9a364SKalle Valo 	 {104, 0x35},
8147ac9a364SKalle Valo 	 {98, 0x35},
8157ac9a364SKalle Valo 	 {110, 0x34},
8167ac9a364SKalle Valo 	 {104, 0x34},
8177ac9a364SKalle Valo 	 {98, 0x34},
8187ac9a364SKalle Valo 	 {110, 0x33},
8197ac9a364SKalle Valo 	 {104, 0x33},
8207ac9a364SKalle Valo 	 {98, 0x33},
8217ac9a364SKalle Valo 	 {110, 0x32},
8227ac9a364SKalle Valo 	 {104, 0x32},
8237ac9a364SKalle Valo 	 {98, 0x32},
8247ac9a364SKalle Valo 	 {110, 0x31},
8257ac9a364SKalle Valo 	 {104, 0x31},
8267ac9a364SKalle Valo 	 {98, 0x31},
8277ac9a364SKalle Valo 	 {110, 0x30},
8287ac9a364SKalle Valo 	 {104, 0x30},
8297ac9a364SKalle Valo 	 {98, 0x30},
8307ac9a364SKalle Valo 	 {110, 0x25},
8317ac9a364SKalle Valo 	 {104, 0x25},
8327ac9a364SKalle Valo 	 {98, 0x25},
8337ac9a364SKalle Valo 	 {110, 0x24},
8347ac9a364SKalle Valo 	 {104, 0x24},
8357ac9a364SKalle Valo 	 {98, 0x24},
8367ac9a364SKalle Valo 	 {110, 0x23},
8377ac9a364SKalle Valo 	 {104, 0x23},
8387ac9a364SKalle Valo 	 {98, 0x23},
8397ac9a364SKalle Valo 	 {110, 0x22},
8407ac9a364SKalle Valo 	 {104, 0x18},
8417ac9a364SKalle Valo 	 {98, 0x18},
8427ac9a364SKalle Valo 	 {110, 0x17},
8437ac9a364SKalle Valo 	 {104, 0x17},
8447ac9a364SKalle Valo 	 {98, 0x17},
8457ac9a364SKalle Valo 	 {110, 0x16},
8467ac9a364SKalle Valo 	 {104, 0x16},
8477ac9a364SKalle Valo 	 {98, 0x16},
8487ac9a364SKalle Valo 	 {110, 0x15},
8497ac9a364SKalle Valo 	 {104, 0x15},
8507ac9a364SKalle Valo 	 {98, 0x15},
8517ac9a364SKalle Valo 	 {110, 0x14},
8527ac9a364SKalle Valo 	 {104, 0x14},
8537ac9a364SKalle Valo 	 {98, 0x14},
8547ac9a364SKalle Valo 	 {110, 0x13},
8557ac9a364SKalle Valo 	 {104, 0x13},
8567ac9a364SKalle Valo 	 {98, 0x13},
8577ac9a364SKalle Valo 	 {110, 0x12},
8587ac9a364SKalle Valo 	 {104, 0x08},
8597ac9a364SKalle Valo 	 {98, 0x08},
8607ac9a364SKalle Valo 	 {110, 0x07},
8617ac9a364SKalle Valo 	 {104, 0x07},
8627ac9a364SKalle Valo 	 {98, 0x07},
8637ac9a364SKalle Valo 	 {110, 0x06},
8647ac9a364SKalle Valo 	 {104, 0x06},
8657ac9a364SKalle Valo 	 {98, 0x06},
8667ac9a364SKalle Valo 	 {110, 0x05},
8677ac9a364SKalle Valo 	 {104, 0x05},
8687ac9a364SKalle Valo 	 {98, 0x05},
8697ac9a364SKalle Valo 	 {110, 0x04},
8707ac9a364SKalle Valo 	 {104, 0x04},
8717ac9a364SKalle Valo 	 {98, 0x04},
8727ac9a364SKalle Valo 	 {110, 0x03},
8737ac9a364SKalle Valo 	 {104, 0x03},
8747ac9a364SKalle Valo 	 {98, 0x03},
8757ac9a364SKalle Valo 	 {110, 0x02},
8767ac9a364SKalle Valo 	 {104, 0x02},
8777ac9a364SKalle Valo 	 {98, 0x02},
8787ac9a364SKalle Valo 	 {110, 0x01},
8797ac9a364SKalle Valo 	 {104, 0x01},
8807ac9a364SKalle Valo 	 {98, 0x01},
8817ac9a364SKalle Valo 	 {110, 0x00},
8827ac9a364SKalle Valo 	 {104, 0x00},
8837ac9a364SKalle Valo 	 {98, 0x00},
8847ac9a364SKalle Valo 	 {93, 0x00},
8857ac9a364SKalle Valo 	 {88, 0x00},
8867ac9a364SKalle Valo 	 {83, 0x00},
8877ac9a364SKalle Valo 	 {78, 0x00},
8887ac9a364SKalle Valo 	 },
8897ac9a364SKalle Valo 	/* 2.4GHz power gain idx table */
8907ac9a364SKalle Valo 	{
8917ac9a364SKalle Valo 	 {110, 0x3f},		/* highest txpower */
8927ac9a364SKalle Valo 	 {104, 0x3f},
8937ac9a364SKalle Valo 	 {98, 0x3f},
8947ac9a364SKalle Valo 	 {110, 0x3e},
8957ac9a364SKalle Valo 	 {104, 0x3e},
8967ac9a364SKalle Valo 	 {98, 0x3e},
8977ac9a364SKalle Valo 	 {110, 0x3d},
8987ac9a364SKalle Valo 	 {104, 0x3d},
8997ac9a364SKalle Valo 	 {98, 0x3d},
9007ac9a364SKalle Valo 	 {110, 0x3c},
9017ac9a364SKalle Valo 	 {104, 0x3c},
9027ac9a364SKalle Valo 	 {98, 0x3c},
9037ac9a364SKalle Valo 	 {110, 0x3b},
9047ac9a364SKalle Valo 	 {104, 0x3b},
9057ac9a364SKalle Valo 	 {98, 0x3b},
9067ac9a364SKalle Valo 	 {110, 0x3a},
9077ac9a364SKalle Valo 	 {104, 0x3a},
9087ac9a364SKalle Valo 	 {98, 0x3a},
9097ac9a364SKalle Valo 	 {110, 0x39},
9107ac9a364SKalle Valo 	 {104, 0x39},
9117ac9a364SKalle Valo 	 {98, 0x39},
9127ac9a364SKalle Valo 	 {110, 0x38},
9137ac9a364SKalle Valo 	 {104, 0x38},
9147ac9a364SKalle Valo 	 {98, 0x38},
9157ac9a364SKalle Valo 	 {110, 0x37},
9167ac9a364SKalle Valo 	 {104, 0x37},
9177ac9a364SKalle Valo 	 {98, 0x37},
9187ac9a364SKalle Valo 	 {110, 0x36},
9197ac9a364SKalle Valo 	 {104, 0x36},
9207ac9a364SKalle Valo 	 {98, 0x36},
9217ac9a364SKalle Valo 	 {110, 0x35},
9227ac9a364SKalle Valo 	 {104, 0x35},
9237ac9a364SKalle Valo 	 {98, 0x35},
9247ac9a364SKalle Valo 	 {110, 0x34},
9257ac9a364SKalle Valo 	 {104, 0x34},
9267ac9a364SKalle Valo 	 {98, 0x34},
9277ac9a364SKalle Valo 	 {110, 0x33},
9287ac9a364SKalle Valo 	 {104, 0x33},
9297ac9a364SKalle Valo 	 {98, 0x33},
9307ac9a364SKalle Valo 	 {110, 0x32},
9317ac9a364SKalle Valo 	 {104, 0x32},
9327ac9a364SKalle Valo 	 {98, 0x32},
9337ac9a364SKalle Valo 	 {110, 0x31},
9347ac9a364SKalle Valo 	 {104, 0x31},
9357ac9a364SKalle Valo 	 {98, 0x31},
9367ac9a364SKalle Valo 	 {110, 0x30},
9377ac9a364SKalle Valo 	 {104, 0x30},
9387ac9a364SKalle Valo 	 {98, 0x30},
9397ac9a364SKalle Valo 	 {110, 0x6},
9407ac9a364SKalle Valo 	 {104, 0x6},
9417ac9a364SKalle Valo 	 {98, 0x6},
9427ac9a364SKalle Valo 	 {110, 0x5},
9437ac9a364SKalle Valo 	 {104, 0x5},
9447ac9a364SKalle Valo 	 {98, 0x5},
9457ac9a364SKalle Valo 	 {110, 0x4},
9467ac9a364SKalle Valo 	 {104, 0x4},
9477ac9a364SKalle Valo 	 {98, 0x4},
9487ac9a364SKalle Valo 	 {110, 0x3},
9497ac9a364SKalle Valo 	 {104, 0x3},
9507ac9a364SKalle Valo 	 {98, 0x3},
9517ac9a364SKalle Valo 	 {110, 0x2},
9527ac9a364SKalle Valo 	 {104, 0x2},
9537ac9a364SKalle Valo 	 {98, 0x2},
9547ac9a364SKalle Valo 	 {110, 0x1},
9557ac9a364SKalle Valo 	 {104, 0x1},
9567ac9a364SKalle Valo 	 {98, 0x1},
9577ac9a364SKalle Valo 	 {110, 0x0},
9587ac9a364SKalle Valo 	 {104, 0x0},
9597ac9a364SKalle Valo 	 {98, 0x0},
9607ac9a364SKalle Valo 	 {97, 0},
9617ac9a364SKalle Valo 	 {96, 0},
9627ac9a364SKalle Valo 	 {95, 0},
9637ac9a364SKalle Valo 	 {94, 0},
9647ac9a364SKalle Valo 	 {93, 0},
9657ac9a364SKalle Valo 	 {92, 0},
9667ac9a364SKalle Valo 	 {91, 0},
9677ac9a364SKalle Valo 	 {90, 0},
9687ac9a364SKalle Valo 	 {89, 0},
9697ac9a364SKalle Valo 	 {88, 0},
9707ac9a364SKalle Valo 	 {87, 0},
9717ac9a364SKalle Valo 	 {86, 0},
9727ac9a364SKalle Valo 	 {85, 0},
9737ac9a364SKalle Valo 	 {84, 0},
9747ac9a364SKalle Valo 	 {83, 0},
9757ac9a364SKalle Valo 	 {82, 0},
9767ac9a364SKalle Valo 	 {81, 0},
9777ac9a364SKalle Valo 	 {80, 0},
9787ac9a364SKalle Valo 	 {79, 0},
9797ac9a364SKalle Valo 	 {78, 0},
9807ac9a364SKalle Valo 	 {77, 0},
9817ac9a364SKalle Valo 	 {76, 0},
9827ac9a364SKalle Valo 	 {75, 0},
9837ac9a364SKalle Valo 	 {74, 0},
9847ac9a364SKalle Valo 	 {73, 0},
9857ac9a364SKalle Valo 	 {72, 0},
9867ac9a364SKalle Valo 	 {71, 0},
9877ac9a364SKalle Valo 	 {70, 0},
9887ac9a364SKalle Valo 	 {69, 0},
9897ac9a364SKalle Valo 	 {68, 0},
9907ac9a364SKalle Valo 	 {67, 0},
9917ac9a364SKalle Valo 	 {66, 0},
9927ac9a364SKalle Valo 	 {65, 0},
9937ac9a364SKalle Valo 	 {64, 0},
9947ac9a364SKalle Valo 	 {63, 0},
9957ac9a364SKalle Valo 	 {62, 0},
9967ac9a364SKalle Valo 	 {61, 0},
9977ac9a364SKalle Valo 	 {60, 0},
9987ac9a364SKalle Valo 	 {59, 0},
9997ac9a364SKalle Valo 	 }
10007ac9a364SKalle Valo };
10017ac9a364SKalle Valo 
10027ac9a364SKalle Valo static int
il4965_fill_txpower_tbl(struct il_priv * il,u8 band,u16 channel,u8 is_ht40,u8 ctrl_chan_high,struct il4965_tx_power_db * tx_power_tbl)10037ac9a364SKalle Valo il4965_fill_txpower_tbl(struct il_priv *il, u8 band, u16 channel, u8 is_ht40,
10047ac9a364SKalle Valo 			u8 ctrl_chan_high,
10057ac9a364SKalle Valo 			struct il4965_tx_power_db *tx_power_tbl)
10067ac9a364SKalle Valo {
10077ac9a364SKalle Valo 	u8 saturation_power;
10087ac9a364SKalle Valo 	s32 target_power;
10097ac9a364SKalle Valo 	s32 user_target_power;
10107ac9a364SKalle Valo 	s32 power_limit;
10117ac9a364SKalle Valo 	s32 current_temp;
10127ac9a364SKalle Valo 	s32 reg_limit;
10137ac9a364SKalle Valo 	s32 current_regulatory;
10147ac9a364SKalle Valo 	s32 txatten_grp = CALIB_CH_GROUP_MAX;
10157ac9a364SKalle Valo 	int i;
10167ac9a364SKalle Valo 	int c;
10177ac9a364SKalle Valo 	const struct il_channel_info *ch_info = NULL;
10187ac9a364SKalle Valo 	struct il_eeprom_calib_ch_info ch_eeprom_info;
10197ac9a364SKalle Valo 	const struct il_eeprom_calib_measure *measurement;
10207ac9a364SKalle Valo 	s16 voltage;
10217ac9a364SKalle Valo 	s32 init_voltage;
10227ac9a364SKalle Valo 	s32 voltage_compensation;
10237ac9a364SKalle Valo 	s32 degrees_per_05db_num;
10247ac9a364SKalle Valo 	s32 degrees_per_05db_denom;
10257ac9a364SKalle Valo 	s32 factory_temp;
10267ac9a364SKalle Valo 	s32 temperature_comp[2];
10277ac9a364SKalle Valo 	s32 factory_gain_idx[2];
10287ac9a364SKalle Valo 	s32 factory_actual_pwr[2];
10297ac9a364SKalle Valo 	s32 power_idx;
10307ac9a364SKalle Valo 
10317ac9a364SKalle Valo 	/* tx_power_user_lmt is in dBm, convert to half-dBm (half-dB units
10327ac9a364SKalle Valo 	 *   are used for idxing into txpower table) */
10337ac9a364SKalle Valo 	user_target_power = 2 * il->tx_power_user_lmt;
10347ac9a364SKalle Valo 
10357ac9a364SKalle Valo 	/* Get current (RXON) channel, band, width */
10367ac9a364SKalle Valo 	D_TXPOWER("chan %d band %d is_ht40 %d\n", channel, band, is_ht40);
10377ac9a364SKalle Valo 
10387ac9a364SKalle Valo 	ch_info = il_get_channel_info(il, il->band, channel);
10397ac9a364SKalle Valo 
10407ac9a364SKalle Valo 	if (!il_is_channel_valid(ch_info))
10417ac9a364SKalle Valo 		return -EINVAL;
10427ac9a364SKalle Valo 
10437ac9a364SKalle Valo 	/* get txatten group, used to select 1) thermal txpower adjustment
10447ac9a364SKalle Valo 	 *   and 2) mimo txpower balance between Tx chains. */
10457ac9a364SKalle Valo 	txatten_grp = il4965_get_tx_atten_grp(channel);
10467ac9a364SKalle Valo 	if (txatten_grp < 0) {
10477ac9a364SKalle Valo 		IL_ERR("Can't find txatten group for channel %d.\n", channel);
10487ac9a364SKalle Valo 		return txatten_grp;
10497ac9a364SKalle Valo 	}
10507ac9a364SKalle Valo 
10517ac9a364SKalle Valo 	D_TXPOWER("channel %d belongs to txatten group %d\n", channel,
10527ac9a364SKalle Valo 		  txatten_grp);
10537ac9a364SKalle Valo 
10547ac9a364SKalle Valo 	if (is_ht40) {
10557ac9a364SKalle Valo 		if (ctrl_chan_high)
10567ac9a364SKalle Valo 			channel -= 2;
10577ac9a364SKalle Valo 		else
10587ac9a364SKalle Valo 			channel += 2;
10597ac9a364SKalle Valo 	}
10607ac9a364SKalle Valo 
10617ac9a364SKalle Valo 	/* hardware txpower limits ...
10627ac9a364SKalle Valo 	 * saturation (clipping distortion) txpowers are in half-dBm */
10637ac9a364SKalle Valo 	if (band)
10647ac9a364SKalle Valo 		saturation_power = il->calib_info->saturation_power24;
10657ac9a364SKalle Valo 	else
10667ac9a364SKalle Valo 		saturation_power = il->calib_info->saturation_power52;
10677ac9a364SKalle Valo 
10687ac9a364SKalle Valo 	if (saturation_power < IL_TX_POWER_SATURATION_MIN ||
10697ac9a364SKalle Valo 	    saturation_power > IL_TX_POWER_SATURATION_MAX) {
10707ac9a364SKalle Valo 		if (band)
10717ac9a364SKalle Valo 			saturation_power = IL_TX_POWER_DEFAULT_SATURATION_24;
10727ac9a364SKalle Valo 		else
10737ac9a364SKalle Valo 			saturation_power = IL_TX_POWER_DEFAULT_SATURATION_52;
10747ac9a364SKalle Valo 	}
10757ac9a364SKalle Valo 
10767ac9a364SKalle Valo 	/* regulatory txpower limits ... reg_limit values are in half-dBm,
10777ac9a364SKalle Valo 	 *   max_power_avg values are in dBm, convert * 2 */
10787ac9a364SKalle Valo 	if (is_ht40)
10797ac9a364SKalle Valo 		reg_limit = ch_info->ht40_max_power_avg * 2;
10807ac9a364SKalle Valo 	else
10817ac9a364SKalle Valo 		reg_limit = ch_info->max_power_avg * 2;
10827ac9a364SKalle Valo 
10837ac9a364SKalle Valo 	if ((reg_limit < IL_TX_POWER_REGULATORY_MIN) ||
10847ac9a364SKalle Valo 	    (reg_limit > IL_TX_POWER_REGULATORY_MAX)) {
10857ac9a364SKalle Valo 		if (band)
10867ac9a364SKalle Valo 			reg_limit = IL_TX_POWER_DEFAULT_REGULATORY_24;
10877ac9a364SKalle Valo 		else
10887ac9a364SKalle Valo 			reg_limit = IL_TX_POWER_DEFAULT_REGULATORY_52;
10897ac9a364SKalle Valo 	}
10907ac9a364SKalle Valo 
10917ac9a364SKalle Valo 	/* Interpolate txpower calibration values for this channel,
10927ac9a364SKalle Valo 	 *   based on factory calibration tests on spaced channels. */
10937ac9a364SKalle Valo 	il4965_interpolate_chan(il, channel, &ch_eeprom_info);
10947ac9a364SKalle Valo 
10957ac9a364SKalle Valo 	/* calculate tx gain adjustment based on power supply voltage */
10967ac9a364SKalle Valo 	voltage = le16_to_cpu(il->calib_info->voltage);
10977ac9a364SKalle Valo 	init_voltage = (s32) le32_to_cpu(il->card_alive_init.voltage);
10987ac9a364SKalle Valo 	voltage_compensation =
10997ac9a364SKalle Valo 	    il4965_get_voltage_compensation(voltage, init_voltage);
11007ac9a364SKalle Valo 
11017ac9a364SKalle Valo 	D_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n", init_voltage,
11027ac9a364SKalle Valo 		  voltage, voltage_compensation);
11037ac9a364SKalle Valo 
11047ac9a364SKalle Valo 	/* get current temperature (Celsius) */
11057ac9a364SKalle Valo 	current_temp = max(il->temperature, IL_TX_POWER_TEMPERATURE_MIN);
11067ac9a364SKalle Valo 	current_temp = min(il->temperature, IL_TX_POWER_TEMPERATURE_MAX);
11071410b2fcSAkinobu Mita 	current_temp = kelvin_to_celsius(current_temp);
11087ac9a364SKalle Valo 
11097ac9a364SKalle Valo 	/* select thermal txpower adjustment params, based on channel group
11107ac9a364SKalle Valo 	 *   (same frequency group used for mimo txatten adjustment) */
11117ac9a364SKalle Valo 	degrees_per_05db_num =
11127ac9a364SKalle Valo 	    tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
11137ac9a364SKalle Valo 	degrees_per_05db_denom =
11147ac9a364SKalle Valo 	    tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
11157ac9a364SKalle Valo 
11167ac9a364SKalle Valo 	/* get per-chain txpower values from factory measurements */
11177ac9a364SKalle Valo 	for (c = 0; c < 2; c++) {
11187ac9a364SKalle Valo 		measurement = &ch_eeprom_info.measurements[c][1];
11197ac9a364SKalle Valo 
11207ac9a364SKalle Valo 		/* txgain adjustment (in half-dB steps) based on difference
11217ac9a364SKalle Valo 		 *   between factory and current temperature */
11227ac9a364SKalle Valo 		factory_temp = measurement->temperature;
11237ac9a364SKalle Valo 		il4965_math_div_round((current_temp -
11247ac9a364SKalle Valo 				       factory_temp) * degrees_per_05db_denom,
11257ac9a364SKalle Valo 				      degrees_per_05db_num,
11267ac9a364SKalle Valo 				      &temperature_comp[c]);
11277ac9a364SKalle Valo 
11287ac9a364SKalle Valo 		factory_gain_idx[c] = measurement->gain_idx;
11297ac9a364SKalle Valo 		factory_actual_pwr[c] = measurement->actual_pow;
11307ac9a364SKalle Valo 
11317ac9a364SKalle Valo 		D_TXPOWER("chain = %d\n", c);
11327ac9a364SKalle Valo 		D_TXPOWER("fctry tmp %d, " "curr tmp %d, comp %d steps\n",
11337ac9a364SKalle Valo 			  factory_temp, current_temp, temperature_comp[c]);
11347ac9a364SKalle Valo 
11357ac9a364SKalle Valo 		D_TXPOWER("fctry idx %d, fctry pwr %d\n", factory_gain_idx[c],
11367ac9a364SKalle Valo 			  factory_actual_pwr[c]);
11377ac9a364SKalle Valo 	}
11387ac9a364SKalle Valo 
11397ac9a364SKalle Valo 	/* for each of 33 bit-rates (including 1 for CCK) */
11407ac9a364SKalle Valo 	for (i = 0; i < POWER_TBL_NUM_ENTRIES; i++) {
11417ac9a364SKalle Valo 		u8 is_mimo_rate;
11427ac9a364SKalle Valo 		union il4965_tx_power_dual_stream tx_power;
11437ac9a364SKalle Valo 
11447ac9a364SKalle Valo 		/* for mimo, reduce each chain's txpower by half
11457ac9a364SKalle Valo 		 * (3dB, 6 steps), so total output power is regulatory
11467ac9a364SKalle Valo 		 * compliant. */
11477ac9a364SKalle Valo 		if (i & 0x8) {
11487ac9a364SKalle Valo 			current_regulatory =
11497ac9a364SKalle Valo 			    reg_limit -
11507ac9a364SKalle Valo 			    IL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
11517ac9a364SKalle Valo 			is_mimo_rate = 1;
11527ac9a364SKalle Valo 		} else {
11537ac9a364SKalle Valo 			current_regulatory = reg_limit;
11547ac9a364SKalle Valo 			is_mimo_rate = 0;
11557ac9a364SKalle Valo 		}
11567ac9a364SKalle Valo 
11577ac9a364SKalle Valo 		/* find txpower limit, either hardware or regulatory */
11587ac9a364SKalle Valo 		power_limit = saturation_power - back_off_table[i];
11597ac9a364SKalle Valo 		if (power_limit > current_regulatory)
11607ac9a364SKalle Valo 			power_limit = current_regulatory;
11617ac9a364SKalle Valo 
11627ac9a364SKalle Valo 		/* reduce user's txpower request if necessary
11637ac9a364SKalle Valo 		 * for this rate on this channel */
11647ac9a364SKalle Valo 		target_power = user_target_power;
11657ac9a364SKalle Valo 		if (target_power > power_limit)
11667ac9a364SKalle Valo 			target_power = power_limit;
11677ac9a364SKalle Valo 
11687ac9a364SKalle Valo 		D_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n", i,
11697ac9a364SKalle Valo 			  saturation_power - back_off_table[i],
11707ac9a364SKalle Valo 			  current_regulatory, user_target_power, target_power);
11717ac9a364SKalle Valo 
11727ac9a364SKalle Valo 		/* for each of 2 Tx chains (radio transmitters) */
11737ac9a364SKalle Valo 		for (c = 0; c < 2; c++) {
11747ac9a364SKalle Valo 			s32 atten_value;
11757ac9a364SKalle Valo 
11767ac9a364SKalle Valo 			if (is_mimo_rate)
11777ac9a364SKalle Valo 				atten_value =
11787ac9a364SKalle Valo 				    (s32) le32_to_cpu(il->card_alive_init.
11797ac9a364SKalle Valo 						      tx_atten[txatten_grp][c]);
11807ac9a364SKalle Valo 			else
11817ac9a364SKalle Valo 				atten_value = 0;
11827ac9a364SKalle Valo 
11837ac9a364SKalle Valo 			/* calculate idx; higher idx means lower txpower */
11847ac9a364SKalle Valo 			power_idx =
11857ac9a364SKalle Valo 			    (u8) (factory_gain_idx[c] -
11867ac9a364SKalle Valo 				  (target_power - factory_actual_pwr[c]) -
11877ac9a364SKalle Valo 				  temperature_comp[c] - voltage_compensation +
11887ac9a364SKalle Valo 				  atten_value);
11897ac9a364SKalle Valo 
11907ac9a364SKalle Valo /*			D_TXPOWER("calculated txpower idx %d\n",
11917ac9a364SKalle Valo 						power_idx); */
11927ac9a364SKalle Valo 
11937ac9a364SKalle Valo 			if (power_idx < get_min_power_idx(i, band))
11947ac9a364SKalle Valo 				power_idx = get_min_power_idx(i, band);
11957ac9a364SKalle Valo 
11967ac9a364SKalle Valo 			/* adjust 5 GHz idx to support negative idxes */
11977ac9a364SKalle Valo 			if (!band)
11987ac9a364SKalle Valo 				power_idx += 9;
11997ac9a364SKalle Valo 
12007ac9a364SKalle Valo 			/* CCK, rate 32, reduce txpower for CCK */
12017ac9a364SKalle Valo 			if (i == POWER_TBL_CCK_ENTRY)
12027ac9a364SKalle Valo 				power_idx +=
12037ac9a364SKalle Valo 				    IL_TX_POWER_CCK_COMPENSATION_C_STEP;
12047ac9a364SKalle Valo 
12057ac9a364SKalle Valo 			/* stay within the table! */
12067ac9a364SKalle Valo 			if (power_idx > 107) {
12077ac9a364SKalle Valo 				IL_WARN("txpower idx %d > 107\n", power_idx);
12087ac9a364SKalle Valo 				power_idx = 107;
12097ac9a364SKalle Valo 			}
12107ac9a364SKalle Valo 			if (power_idx < 0) {
12117ac9a364SKalle Valo 				IL_WARN("txpower idx %d < 0\n", power_idx);
12127ac9a364SKalle Valo 				power_idx = 0;
12137ac9a364SKalle Valo 			}
12147ac9a364SKalle Valo 
12157ac9a364SKalle Valo 			/* fill txpower command for this rate/chain */
12167ac9a364SKalle Valo 			tx_power.s.radio_tx_gain[c] =
12177ac9a364SKalle Valo 			    gain_table[band][power_idx].radio;
12187ac9a364SKalle Valo 			tx_power.s.dsp_predis_atten[c] =
12197ac9a364SKalle Valo 			    gain_table[band][power_idx].dsp;
12207ac9a364SKalle Valo 
12217ac9a364SKalle Valo 			D_TXPOWER("chain %d mimo %d idx %d "
12227ac9a364SKalle Valo 				  "gain 0x%02x dsp %d\n", c, atten_value,
12237ac9a364SKalle Valo 				  power_idx, tx_power.s.radio_tx_gain[c],
12247ac9a364SKalle Valo 				  tx_power.s.dsp_predis_atten[c]);
12257ac9a364SKalle Valo 		}		/* for each chain */
12267ac9a364SKalle Valo 
12277ac9a364SKalle Valo 		tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
12287ac9a364SKalle Valo 
12297ac9a364SKalle Valo 	}			/* for each rate */
12307ac9a364SKalle Valo 
12317ac9a364SKalle Valo 	return 0;
12327ac9a364SKalle Valo }
12337ac9a364SKalle Valo 
12347951a3bfSLee Jones /*
12357ac9a364SKalle Valo  * il4965_send_tx_power - Configure the TXPOWER level user limit
12367ac9a364SKalle Valo  *
12377ac9a364SKalle Valo  * Uses the active RXON for channel, band, and characteristics (ht40, high)
12387ac9a364SKalle Valo  * The power limit is taken from il->tx_power_user_lmt.
12397ac9a364SKalle Valo  */
12407ac9a364SKalle Valo static int
il4965_send_tx_power(struct il_priv * il)12417ac9a364SKalle Valo il4965_send_tx_power(struct il_priv *il)
12427ac9a364SKalle Valo {
12437ac9a364SKalle Valo 	struct il4965_txpowertable_cmd cmd = { 0 };
12447ac9a364SKalle Valo 	int ret;
12457ac9a364SKalle Valo 	u8 band = 0;
12467ac9a364SKalle Valo 	bool is_ht40 = false;
12477ac9a364SKalle Valo 	u8 ctrl_chan_high = 0;
12487ac9a364SKalle Valo 
12497ac9a364SKalle Valo 	if (WARN_ONCE
12507ac9a364SKalle Valo 	    (test_bit(S_SCAN_HW, &il->status),
12517ac9a364SKalle Valo 	     "TX Power requested while scanning!\n"))
12527ac9a364SKalle Valo 		return -EAGAIN;
12537ac9a364SKalle Valo 
125457fbcce3SJohannes Berg 	band = il->band == NL80211_BAND_2GHZ;
12557ac9a364SKalle Valo 
12567ac9a364SKalle Valo 	is_ht40 = iw4965_is_ht40_channel(il->active.flags);
12577ac9a364SKalle Valo 
12587ac9a364SKalle Valo 	if (is_ht40 && (il->active.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
12597ac9a364SKalle Valo 		ctrl_chan_high = 1;
12607ac9a364SKalle Valo 
12617ac9a364SKalle Valo 	cmd.band = band;
12627ac9a364SKalle Valo 	cmd.channel = il->active.channel;
12637ac9a364SKalle Valo 
12647ac9a364SKalle Valo 	ret =
12657ac9a364SKalle Valo 	    il4965_fill_txpower_tbl(il, band, le16_to_cpu(il->active.channel),
12667ac9a364SKalle Valo 				    is_ht40, ctrl_chan_high, &cmd.tx_power);
12677ac9a364SKalle Valo 	if (ret)
12687ac9a364SKalle Valo 		goto out;
12697ac9a364SKalle Valo 
12707ac9a364SKalle Valo 	ret = il_send_cmd_pdu(il, C_TX_PWR_TBL, sizeof(cmd), &cmd);
12717ac9a364SKalle Valo 
12727ac9a364SKalle Valo out:
12737ac9a364SKalle Valo 	return ret;
12747ac9a364SKalle Valo }
12757ac9a364SKalle Valo 
12767ac9a364SKalle Valo static int
il4965_send_rxon_assoc(struct il_priv * il)12777ac9a364SKalle Valo il4965_send_rxon_assoc(struct il_priv *il)
12787ac9a364SKalle Valo {
12797ac9a364SKalle Valo 	int ret = 0;
12807ac9a364SKalle Valo 	struct il4965_rxon_assoc_cmd rxon_assoc;
12817ac9a364SKalle Valo 	const struct il_rxon_cmd *rxon1 = &il->staging;
12827ac9a364SKalle Valo 	const struct il_rxon_cmd *rxon2 = &il->active;
12837ac9a364SKalle Valo 
128452a31267SJia-Ju Bai 	lockdep_assert_held(&il->mutex);
128552a31267SJia-Ju Bai 
12867ac9a364SKalle Valo 	if (rxon1->flags == rxon2->flags &&
12877ac9a364SKalle Valo 	    rxon1->filter_flags == rxon2->filter_flags &&
12887ac9a364SKalle Valo 	    rxon1->cck_basic_rates == rxon2->cck_basic_rates &&
12897ac9a364SKalle Valo 	    rxon1->ofdm_ht_single_stream_basic_rates ==
12907ac9a364SKalle Valo 	    rxon2->ofdm_ht_single_stream_basic_rates &&
12917ac9a364SKalle Valo 	    rxon1->ofdm_ht_dual_stream_basic_rates ==
12927ac9a364SKalle Valo 	    rxon2->ofdm_ht_dual_stream_basic_rates &&
12937ac9a364SKalle Valo 	    rxon1->rx_chain == rxon2->rx_chain &&
12947ac9a364SKalle Valo 	    rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates) {
12957ac9a364SKalle Valo 		D_INFO("Using current RXON_ASSOC.  Not resending.\n");
12967ac9a364SKalle Valo 		return 0;
12977ac9a364SKalle Valo 	}
12987ac9a364SKalle Valo 
12997ac9a364SKalle Valo 	rxon_assoc.flags = il->staging.flags;
13007ac9a364SKalle Valo 	rxon_assoc.filter_flags = il->staging.filter_flags;
13017ac9a364SKalle Valo 	rxon_assoc.ofdm_basic_rates = il->staging.ofdm_basic_rates;
13027ac9a364SKalle Valo 	rxon_assoc.cck_basic_rates = il->staging.cck_basic_rates;
13037ac9a364SKalle Valo 	rxon_assoc.reserved = 0;
13047ac9a364SKalle Valo 	rxon_assoc.ofdm_ht_single_stream_basic_rates =
13057ac9a364SKalle Valo 	    il->staging.ofdm_ht_single_stream_basic_rates;
13067ac9a364SKalle Valo 	rxon_assoc.ofdm_ht_dual_stream_basic_rates =
13077ac9a364SKalle Valo 	    il->staging.ofdm_ht_dual_stream_basic_rates;
13087ac9a364SKalle Valo 	rxon_assoc.rx_chain_select_flags = il->staging.rx_chain;
13097ac9a364SKalle Valo 
13107ac9a364SKalle Valo 	ret =
13117ac9a364SKalle Valo 	    il_send_cmd_pdu_async(il, C_RXON_ASSOC, sizeof(rxon_assoc),
13127ac9a364SKalle Valo 				  &rxon_assoc, NULL);
13137ac9a364SKalle Valo 
13147ac9a364SKalle Valo 	return ret;
13157ac9a364SKalle Valo }
13167ac9a364SKalle Valo 
13177ac9a364SKalle Valo static int
il4965_commit_rxon(struct il_priv * il)13187ac9a364SKalle Valo il4965_commit_rxon(struct il_priv *il)
13197ac9a364SKalle Valo {
13207ac9a364SKalle Valo 	/* cast away the const for active_rxon in this function */
13217ac9a364SKalle Valo 	struct il_rxon_cmd *active_rxon = (void *)&il->active;
13227ac9a364SKalle Valo 	int ret;
13237ac9a364SKalle Valo 	bool new_assoc = !!(il->staging.filter_flags & RXON_FILTER_ASSOC_MSK);
13247ac9a364SKalle Valo 
13257ac9a364SKalle Valo 	if (!il_is_alive(il))
13267ac9a364SKalle Valo 		return -EBUSY;
13277ac9a364SKalle Valo 
13287ac9a364SKalle Valo 	/* always get timestamp with Rx frame */
13297ac9a364SKalle Valo 	il->staging.flags |= RXON_FLG_TSF2HOST_MSK;
13307ac9a364SKalle Valo 
13317ac9a364SKalle Valo 	ret = il_check_rxon_cmd(il);
13327ac9a364SKalle Valo 	if (ret) {
13337ac9a364SKalle Valo 		IL_ERR("Invalid RXON configuration.  Not committing.\n");
13347ac9a364SKalle Valo 		return -EINVAL;
13357ac9a364SKalle Valo 	}
13367ac9a364SKalle Valo 
13377ac9a364SKalle Valo 	/*
13387ac9a364SKalle Valo 	 * receive commit_rxon request
13397ac9a364SKalle Valo 	 * abort any previous channel switch if still in process
13407ac9a364SKalle Valo 	 */
13417ac9a364SKalle Valo 	if (test_bit(S_CHANNEL_SWITCH_PENDING, &il->status) &&
13427ac9a364SKalle Valo 	    il->switch_channel != il->staging.channel) {
13437ac9a364SKalle Valo 		D_11H("abort channel switch on %d\n",
13447ac9a364SKalle Valo 		      le16_to_cpu(il->switch_channel));
13457ac9a364SKalle Valo 		il_chswitch_done(il, false);
13467ac9a364SKalle Valo 	}
13477ac9a364SKalle Valo 
13487ac9a364SKalle Valo 	/* If we don't need to send a full RXON, we can use
13497ac9a364SKalle Valo 	 * il_rxon_assoc_cmd which is used to reconfigure filter
13507ac9a364SKalle Valo 	 * and other flags for the current radio configuration. */
13517ac9a364SKalle Valo 	if (!il_full_rxon_required(il)) {
13527ac9a364SKalle Valo 		ret = il_send_rxon_assoc(il);
13537ac9a364SKalle Valo 		if (ret) {
13547ac9a364SKalle Valo 			IL_ERR("Error setting RXON_ASSOC (%d)\n", ret);
13557ac9a364SKalle Valo 			return ret;
13567ac9a364SKalle Valo 		}
13577ac9a364SKalle Valo 
13587ac9a364SKalle Valo 		memcpy(active_rxon, &il->staging, sizeof(*active_rxon));
13597ac9a364SKalle Valo 		il_print_rx_config_cmd(il);
13607ac9a364SKalle Valo 		/*
13617ac9a364SKalle Valo 		 * We do not commit tx power settings while channel changing,
13627ac9a364SKalle Valo 		 * do it now if tx power changed.
13637ac9a364SKalle Valo 		 */
13647ac9a364SKalle Valo 		il_set_tx_power(il, il->tx_power_next, false);
13657ac9a364SKalle Valo 		return 0;
13667ac9a364SKalle Valo 	}
13677ac9a364SKalle Valo 
13687ac9a364SKalle Valo 	/* If we are currently associated and the new config requires
13697ac9a364SKalle Valo 	 * an RXON_ASSOC and the new config wants the associated mask enabled,
13707ac9a364SKalle Valo 	 * we must clear the associated from the active configuration
13717ac9a364SKalle Valo 	 * before we apply the new config */
13727ac9a364SKalle Valo 	if (il_is_associated(il) && new_assoc) {
13737ac9a364SKalle Valo 		D_INFO("Toggling associated bit on current RXON\n");
13747ac9a364SKalle Valo 		active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
13757ac9a364SKalle Valo 
13767ac9a364SKalle Valo 		ret =
13777ac9a364SKalle Valo 		    il_send_cmd_pdu(il, C_RXON,
13787ac9a364SKalle Valo 				    sizeof(struct il_rxon_cmd), active_rxon);
13797ac9a364SKalle Valo 
13807ac9a364SKalle Valo 		/* If the mask clearing failed then we set
13817ac9a364SKalle Valo 		 * active_rxon back to what it was previously */
13827ac9a364SKalle Valo 		if (ret) {
13837ac9a364SKalle Valo 			active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
13847ac9a364SKalle Valo 			IL_ERR("Error clearing ASSOC_MSK (%d)\n", ret);
13857ac9a364SKalle Valo 			return ret;
13867ac9a364SKalle Valo 		}
13877ac9a364SKalle Valo 		il_clear_ucode_stations(il);
13887ac9a364SKalle Valo 		il_restore_stations(il);
13897ac9a364SKalle Valo 		ret = il4965_restore_default_wep_keys(il);
13907ac9a364SKalle Valo 		if (ret) {
13917ac9a364SKalle Valo 			IL_ERR("Failed to restore WEP keys (%d)\n", ret);
13927ac9a364SKalle Valo 			return ret;
13937ac9a364SKalle Valo 		}
13947ac9a364SKalle Valo 	}
13957ac9a364SKalle Valo 
13967ac9a364SKalle Valo 	D_INFO("Sending RXON\n" "* with%s RXON_FILTER_ASSOC_MSK\n"
13977ac9a364SKalle Valo 	       "* channel = %d\n" "* bssid = %pM\n", (new_assoc ? "" : "out"),
13987ac9a364SKalle Valo 	       le16_to_cpu(il->staging.channel), il->staging.bssid_addr);
13997ac9a364SKalle Valo 
14007ac9a364SKalle Valo 	il_set_rxon_hwcrypto(il, !il->cfg->mod_params->sw_crypto);
14017ac9a364SKalle Valo 
14027ac9a364SKalle Valo 	/* Apply the new configuration
14037ac9a364SKalle Valo 	 * RXON unassoc clears the station table in uCode so restoration of
14047ac9a364SKalle Valo 	 * stations is needed after it (the RXON command) completes
14057ac9a364SKalle Valo 	 */
14067ac9a364SKalle Valo 	if (!new_assoc) {
14077ac9a364SKalle Valo 		ret =
14087ac9a364SKalle Valo 		    il_send_cmd_pdu(il, C_RXON,
14097ac9a364SKalle Valo 				    sizeof(struct il_rxon_cmd), &il->staging);
14107ac9a364SKalle Valo 		if (ret) {
14117ac9a364SKalle Valo 			IL_ERR("Error setting new RXON (%d)\n", ret);
14127ac9a364SKalle Valo 			return ret;
14137ac9a364SKalle Valo 		}
14147ac9a364SKalle Valo 		D_INFO("Return from !new_assoc RXON.\n");
14157ac9a364SKalle Valo 		memcpy(active_rxon, &il->staging, sizeof(*active_rxon));
14167ac9a364SKalle Valo 		il_clear_ucode_stations(il);
14177ac9a364SKalle Valo 		il_restore_stations(il);
14187ac9a364SKalle Valo 		ret = il4965_restore_default_wep_keys(il);
14197ac9a364SKalle Valo 		if (ret) {
14207ac9a364SKalle Valo 			IL_ERR("Failed to restore WEP keys (%d)\n", ret);
14217ac9a364SKalle Valo 			return ret;
14227ac9a364SKalle Valo 		}
14237ac9a364SKalle Valo 	}
14247ac9a364SKalle Valo 	if (new_assoc) {
14257ac9a364SKalle Valo 		il->start_calib = 0;
14267ac9a364SKalle Valo 		/* Apply the new configuration
14277ac9a364SKalle Valo 		 * RXON assoc doesn't clear the station table in uCode,
14287ac9a364SKalle Valo 		 */
14297ac9a364SKalle Valo 		ret =
14307ac9a364SKalle Valo 		    il_send_cmd_pdu(il, C_RXON,
14317ac9a364SKalle Valo 				    sizeof(struct il_rxon_cmd), &il->staging);
14327ac9a364SKalle Valo 		if (ret) {
14337ac9a364SKalle Valo 			IL_ERR("Error setting new RXON (%d)\n", ret);
14347ac9a364SKalle Valo 			return ret;
14357ac9a364SKalle Valo 		}
14367ac9a364SKalle Valo 		memcpy(active_rxon, &il->staging, sizeof(*active_rxon));
14377ac9a364SKalle Valo 	}
14387ac9a364SKalle Valo 	il_print_rx_config_cmd(il);
14397ac9a364SKalle Valo 
14407ac9a364SKalle Valo 	il4965_init_sensitivity(il);
14417ac9a364SKalle Valo 
14427ac9a364SKalle Valo 	/* If we issue a new RXON command which required a tune then we must
14437ac9a364SKalle Valo 	 * send a new TXPOWER command or we won't be able to Tx any frames */
14447ac9a364SKalle Valo 	ret = il_set_tx_power(il, il->tx_power_next, true);
14457ac9a364SKalle Valo 	if (ret) {
14467ac9a364SKalle Valo 		IL_ERR("Error sending TX power (%d)\n", ret);
14477ac9a364SKalle Valo 		return ret;
14487ac9a364SKalle Valo 	}
14497ac9a364SKalle Valo 
14507ac9a364SKalle Valo 	return 0;
14517ac9a364SKalle Valo }
14527ac9a364SKalle Valo 
14537ac9a364SKalle Valo static int
il4965_hw_channel_switch(struct il_priv * il,struct ieee80211_channel_switch * ch_switch)14547ac9a364SKalle Valo il4965_hw_channel_switch(struct il_priv *il,
14557ac9a364SKalle Valo 			 struct ieee80211_channel_switch *ch_switch)
14567ac9a364SKalle Valo {
14577ac9a364SKalle Valo 	int rc;
14587ac9a364SKalle Valo 	u8 band = 0;
14597ac9a364SKalle Valo 	bool is_ht40 = false;
14607ac9a364SKalle Valo 	u8 ctrl_chan_high = 0;
14617ac9a364SKalle Valo 	struct il4965_channel_switch_cmd cmd;
14627ac9a364SKalle Valo 	const struct il_channel_info *ch_info;
14637ac9a364SKalle Valo 	u32 switch_time_in_usec, ucode_switch_time;
14647ac9a364SKalle Valo 	u16 ch;
14657ac9a364SKalle Valo 	u32 tsf_low;
14667ac9a364SKalle Valo 	u8 switch_count;
14677ac9a364SKalle Valo 	u16 beacon_interval = le16_to_cpu(il->timing.beacon_interval);
14687ac9a364SKalle Valo 	struct ieee80211_vif *vif = il->vif;
146957fbcce3SJohannes Berg 	band = (il->band == NL80211_BAND_2GHZ);
14707ac9a364SKalle Valo 
14717ac9a364SKalle Valo 	if (WARN_ON_ONCE(vif == NULL))
14727ac9a364SKalle Valo 		return -EIO;
14737ac9a364SKalle Valo 
14747ac9a364SKalle Valo 	is_ht40 = iw4965_is_ht40_channel(il->staging.flags);
14757ac9a364SKalle Valo 
14767ac9a364SKalle Valo 	if (is_ht40 && (il->staging.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
14777ac9a364SKalle Valo 		ctrl_chan_high = 1;
14787ac9a364SKalle Valo 
14797ac9a364SKalle Valo 	cmd.band = band;
14807ac9a364SKalle Valo 	cmd.expect_beacon = 0;
14817ac9a364SKalle Valo 	ch = ch_switch->chandef.chan->hw_value;
14827ac9a364SKalle Valo 	cmd.channel = cpu_to_le16(ch);
14837ac9a364SKalle Valo 	cmd.rxon_flags = il->staging.flags;
14847ac9a364SKalle Valo 	cmd.rxon_filter_flags = il->staging.filter_flags;
14857ac9a364SKalle Valo 	switch_count = ch_switch->count;
14867ac9a364SKalle Valo 	tsf_low = ch_switch->timestamp & 0x0ffffffff;
14877ac9a364SKalle Valo 	/*
14887ac9a364SKalle Valo 	 * calculate the ucode channel switch time
14897ac9a364SKalle Valo 	 * adding TSF as one of the factor for when to switch
14907ac9a364SKalle Valo 	 */
14917ac9a364SKalle Valo 	if (il->ucode_beacon_time > tsf_low && beacon_interval) {
14927ac9a364SKalle Valo 		if (switch_count >
14937ac9a364SKalle Valo 		    ((il->ucode_beacon_time - tsf_low) / beacon_interval)) {
14947ac9a364SKalle Valo 			switch_count -=
14957ac9a364SKalle Valo 			    (il->ucode_beacon_time - tsf_low) / beacon_interval;
14967ac9a364SKalle Valo 		} else
14977ac9a364SKalle Valo 			switch_count = 0;
14987ac9a364SKalle Valo 	}
14997ac9a364SKalle Valo 	if (switch_count <= 1)
15007ac9a364SKalle Valo 		cmd.switch_time = cpu_to_le32(il->ucode_beacon_time);
15017ac9a364SKalle Valo 	else {
15027ac9a364SKalle Valo 		switch_time_in_usec =
15037ac9a364SKalle Valo 		    vif->bss_conf.beacon_int * switch_count * TIME_UNIT;
15047ac9a364SKalle Valo 		ucode_switch_time =
15057ac9a364SKalle Valo 		    il_usecs_to_beacons(il, switch_time_in_usec,
15067ac9a364SKalle Valo 					beacon_interval);
15077ac9a364SKalle Valo 		cmd.switch_time =
15087ac9a364SKalle Valo 		    il_add_beacon_time(il, il->ucode_beacon_time,
15097ac9a364SKalle Valo 				       ucode_switch_time, beacon_interval);
15107ac9a364SKalle Valo 	}
15117ac9a364SKalle Valo 	D_11H("uCode time for the switch is 0x%x\n", cmd.switch_time);
15127ac9a364SKalle Valo 	ch_info = il_get_channel_info(il, il->band, ch);
15137ac9a364SKalle Valo 	if (ch_info)
15147ac9a364SKalle Valo 		cmd.expect_beacon = il_is_channel_radar(ch_info);
15157ac9a364SKalle Valo 	else {
15167ac9a364SKalle Valo 		IL_ERR("invalid channel switch from %u to %u\n",
15177ac9a364SKalle Valo 		       il->active.channel, ch);
15187ac9a364SKalle Valo 		return -EFAULT;
15197ac9a364SKalle Valo 	}
15207ac9a364SKalle Valo 
15217ac9a364SKalle Valo 	rc = il4965_fill_txpower_tbl(il, band, ch, is_ht40, ctrl_chan_high,
15227ac9a364SKalle Valo 				     &cmd.tx_power);
15237ac9a364SKalle Valo 	if (rc) {
15247ac9a364SKalle Valo 		D_11H("error:%d  fill txpower_tbl\n", rc);
15257ac9a364SKalle Valo 		return rc;
15267ac9a364SKalle Valo 	}
15277ac9a364SKalle Valo 
15287ac9a364SKalle Valo 	return il_send_cmd_pdu(il, C_CHANNEL_SWITCH, sizeof(cmd), &cmd);
15297ac9a364SKalle Valo }
15307ac9a364SKalle Valo 
15317951a3bfSLee Jones /*
15327ac9a364SKalle Valo  * il4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
15337ac9a364SKalle Valo  */
15347ac9a364SKalle Valo static void
il4965_txq_update_byte_cnt_tbl(struct il_priv * il,struct il_tx_queue * txq,u16 byte_cnt)15357ac9a364SKalle Valo il4965_txq_update_byte_cnt_tbl(struct il_priv *il, struct il_tx_queue *txq,
15367ac9a364SKalle Valo 			       u16 byte_cnt)
15377ac9a364SKalle Valo {
15387ac9a364SKalle Valo 	struct il4965_scd_bc_tbl *scd_bc_tbl = il->scd_bc_tbls.addr;
15397ac9a364SKalle Valo 	int txq_id = txq->q.id;
15407ac9a364SKalle Valo 	int write_ptr = txq->q.write_ptr;
15417ac9a364SKalle Valo 	int len = byte_cnt + IL_TX_CRC_SIZE + IL_TX_DELIMITER_SIZE;
15427ac9a364SKalle Valo 	__le16 bc_ent;
15437ac9a364SKalle Valo 
15447ac9a364SKalle Valo 	WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
15457ac9a364SKalle Valo 
15467ac9a364SKalle Valo 	bc_ent = cpu_to_le16(len & 0xFFF);
15477ac9a364SKalle Valo 	/* Set up byte count within first 256 entries */
15487ac9a364SKalle Valo 	scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
15497ac9a364SKalle Valo 
15507ac9a364SKalle Valo 	/* If within first 64 entries, duplicate at end */
15517ac9a364SKalle Valo 	if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
15527ac9a364SKalle Valo 		scd_bc_tbl[txq_id].tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] =
15537ac9a364SKalle Valo 		    bc_ent;
15547ac9a364SKalle Valo }
15557ac9a364SKalle Valo 
15567951a3bfSLee Jones /*
15577ac9a364SKalle Valo  * il4965_hw_get_temperature - return the calibrated temperature (in Kelvin)
15587ac9a364SKalle Valo  *
15597ac9a364SKalle Valo  * A return of <0 indicates bogus data in the stats
15607ac9a364SKalle Valo  */
15617ac9a364SKalle Valo static int
il4965_hw_get_temperature(struct il_priv * il)15627ac9a364SKalle Valo il4965_hw_get_temperature(struct il_priv *il)
15637ac9a364SKalle Valo {
15647ac9a364SKalle Valo 	s32 temperature;
15657ac9a364SKalle Valo 	s32 vt;
15667ac9a364SKalle Valo 	s32 R1, R2, R3;
15677ac9a364SKalle Valo 	u32 R4;
15687ac9a364SKalle Valo 
15697ac9a364SKalle Valo 	if (test_bit(S_TEMPERATURE, &il->status) &&
15707ac9a364SKalle Valo 	    (il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)) {
15717ac9a364SKalle Valo 		D_TEMP("Running HT40 temperature calibration\n");
15727ac9a364SKalle Valo 		R1 = (s32) le32_to_cpu(il->card_alive_init.therm_r1[1]);
15737ac9a364SKalle Valo 		R2 = (s32) le32_to_cpu(il->card_alive_init.therm_r2[1]);
15747ac9a364SKalle Valo 		R3 = (s32) le32_to_cpu(il->card_alive_init.therm_r3[1]);
15757ac9a364SKalle Valo 		R4 = le32_to_cpu(il->card_alive_init.therm_r4[1]);
15767ac9a364SKalle Valo 	} else {
15777ac9a364SKalle Valo 		D_TEMP("Running temperature calibration\n");
15787ac9a364SKalle Valo 		R1 = (s32) le32_to_cpu(il->card_alive_init.therm_r1[0]);
15797ac9a364SKalle Valo 		R2 = (s32) le32_to_cpu(il->card_alive_init.therm_r2[0]);
15807ac9a364SKalle Valo 		R3 = (s32) le32_to_cpu(il->card_alive_init.therm_r3[0]);
15817ac9a364SKalle Valo 		R4 = le32_to_cpu(il->card_alive_init.therm_r4[0]);
15827ac9a364SKalle Valo 	}
15837ac9a364SKalle Valo 
15847ac9a364SKalle Valo 	/*
15857ac9a364SKalle Valo 	 * Temperature is only 23 bits, so sign extend out to 32.
15867ac9a364SKalle Valo 	 *
15877ac9a364SKalle Valo 	 * NOTE If we haven't received a stats notification yet
15887ac9a364SKalle Valo 	 * with an updated temperature, use R4 provided to us in the
15897ac9a364SKalle Valo 	 * "initialize" ALIVE response.
15907ac9a364SKalle Valo 	 */
15917ac9a364SKalle Valo 	if (!test_bit(S_TEMPERATURE, &il->status))
15927ac9a364SKalle Valo 		vt = sign_extend32(R4, 23);
15937ac9a364SKalle Valo 	else
15947ac9a364SKalle Valo 		vt = sign_extend32(le32_to_cpu
15957ac9a364SKalle Valo 				   (il->_4965.stats.general.common.temperature),
15967ac9a364SKalle Valo 				   23);
15977ac9a364SKalle Valo 
15987ac9a364SKalle Valo 	D_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n", R1, R2, R3, vt);
15997ac9a364SKalle Valo 
16007ac9a364SKalle Valo 	if (R3 == R1) {
16017ac9a364SKalle Valo 		IL_ERR("Calibration conflict R1 == R3\n");
16027ac9a364SKalle Valo 		return -1;
16037ac9a364SKalle Valo 	}
16047ac9a364SKalle Valo 
16057ac9a364SKalle Valo 	/* Calculate temperature in degrees Kelvin, adjust by 97%.
16067ac9a364SKalle Valo 	 * Add offset to center the adjustment around 0 degrees Centigrade. */
16077ac9a364SKalle Valo 	temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
16087ac9a364SKalle Valo 	temperature /= (R3 - R1);
16097ac9a364SKalle Valo 	temperature =
16107ac9a364SKalle Valo 	    (temperature * 97) / 100 + TEMPERATURE_CALIB_KELVIN_OFFSET;
16117ac9a364SKalle Valo 
16121410b2fcSAkinobu Mita 	D_TEMP("Calibrated temperature: %dK, %ldC\n", temperature,
16131410b2fcSAkinobu Mita 	       kelvin_to_celsius(temperature));
16147ac9a364SKalle Valo 
16157ac9a364SKalle Valo 	return temperature;
16167ac9a364SKalle Valo }
16177ac9a364SKalle Valo 
16187ac9a364SKalle Valo /* Adjust Txpower only if temperature variance is greater than threshold. */
16197ac9a364SKalle Valo #define IL_TEMPERATURE_THRESHOLD   3
16207ac9a364SKalle Valo 
16217951a3bfSLee Jones /*
16227ac9a364SKalle Valo  * il4965_is_temp_calib_needed - determines if new calibration is needed
16237ac9a364SKalle Valo  *
16247ac9a364SKalle Valo  * If the temperature changed has changed sufficiently, then a recalibration
16257ac9a364SKalle Valo  * is needed.
16267ac9a364SKalle Valo  *
16277ac9a364SKalle Valo  * Assumes caller will replace il->last_temperature once calibration
16287ac9a364SKalle Valo  * executed.
16297ac9a364SKalle Valo  */
16307ac9a364SKalle Valo static int
il4965_is_temp_calib_needed(struct il_priv * il)16317ac9a364SKalle Valo il4965_is_temp_calib_needed(struct il_priv *il)
16327ac9a364SKalle Valo {
16337ac9a364SKalle Valo 	int temp_diff;
16347ac9a364SKalle Valo 
16357ac9a364SKalle Valo 	if (!test_bit(S_STATS, &il->status)) {
16367ac9a364SKalle Valo 		D_TEMP("Temperature not updated -- no stats.\n");
16377ac9a364SKalle Valo 		return 0;
16387ac9a364SKalle Valo 	}
16397ac9a364SKalle Valo 
16407ac9a364SKalle Valo 	temp_diff = il->temperature - il->last_temperature;
16417ac9a364SKalle Valo 
16427ac9a364SKalle Valo 	/* get absolute value */
16437ac9a364SKalle Valo 	if (temp_diff < 0) {
16447ac9a364SKalle Valo 		D_POWER("Getting cooler, delta %d\n", temp_diff);
16457ac9a364SKalle Valo 		temp_diff = -temp_diff;
16467ac9a364SKalle Valo 	} else if (temp_diff == 0)
16477ac9a364SKalle Valo 		D_POWER("Temperature unchanged\n");
16487ac9a364SKalle Valo 	else
16497ac9a364SKalle Valo 		D_POWER("Getting warmer, delta %d\n", temp_diff);
16507ac9a364SKalle Valo 
16517ac9a364SKalle Valo 	if (temp_diff < IL_TEMPERATURE_THRESHOLD) {
16527ac9a364SKalle Valo 		D_POWER(" => thermal txpower calib not needed\n");
16537ac9a364SKalle Valo 		return 0;
16547ac9a364SKalle Valo 	}
16557ac9a364SKalle Valo 
16567ac9a364SKalle Valo 	D_POWER(" => thermal txpower calib needed\n");
16577ac9a364SKalle Valo 
16587ac9a364SKalle Valo 	return 1;
16597ac9a364SKalle Valo }
16607ac9a364SKalle Valo 
16617ac9a364SKalle Valo void
il4965_temperature_calib(struct il_priv * il)16627ac9a364SKalle Valo il4965_temperature_calib(struct il_priv *il)
16637ac9a364SKalle Valo {
16647ac9a364SKalle Valo 	s32 temp;
16657ac9a364SKalle Valo 
16667ac9a364SKalle Valo 	temp = il4965_hw_get_temperature(il);
16677ac9a364SKalle Valo 	if (IL_TX_POWER_TEMPERATURE_OUT_OF_RANGE(temp))
16687ac9a364SKalle Valo 		return;
16697ac9a364SKalle Valo 
16707ac9a364SKalle Valo 	if (il->temperature != temp) {
16717ac9a364SKalle Valo 		if (il->temperature)
16721410b2fcSAkinobu Mita 			D_TEMP("Temperature changed " "from %ldC to %ldC\n",
16731410b2fcSAkinobu Mita 			       kelvin_to_celsius(il->temperature),
16741410b2fcSAkinobu Mita 			       kelvin_to_celsius(temp));
16757ac9a364SKalle Valo 		else
16761410b2fcSAkinobu Mita 			D_TEMP("Temperature " "initialized to %ldC\n",
16771410b2fcSAkinobu Mita 			       kelvin_to_celsius(temp));
16787ac9a364SKalle Valo 	}
16797ac9a364SKalle Valo 
16807ac9a364SKalle Valo 	il->temperature = temp;
16817ac9a364SKalle Valo 	set_bit(S_TEMPERATURE, &il->status);
16827ac9a364SKalle Valo 
16837ac9a364SKalle Valo 	if (!il->disable_tx_power_cal &&
16847ac9a364SKalle Valo 	    unlikely(!test_bit(S_SCANNING, &il->status)) &&
16857ac9a364SKalle Valo 	    il4965_is_temp_calib_needed(il))
16867ac9a364SKalle Valo 		queue_work(il->workqueue, &il->txpower_work);
16877ac9a364SKalle Valo }
16887ac9a364SKalle Valo 
16897ac9a364SKalle Valo static u16
il4965_get_hcmd_size(u8 cmd_id,u16 len)16907ac9a364SKalle Valo il4965_get_hcmd_size(u8 cmd_id, u16 len)
16917ac9a364SKalle Valo {
16927ac9a364SKalle Valo 	switch (cmd_id) {
16937ac9a364SKalle Valo 	case C_RXON:
16947ac9a364SKalle Valo 		return (u16) sizeof(struct il4965_rxon_cmd);
16957ac9a364SKalle Valo 	default:
16967ac9a364SKalle Valo 		return len;
16977ac9a364SKalle Valo 	}
16987ac9a364SKalle Valo }
16997ac9a364SKalle Valo 
17007ac9a364SKalle Valo static u16
il4965_build_addsta_hcmd(const struct il_addsta_cmd * cmd,u8 * data)17017ac9a364SKalle Valo il4965_build_addsta_hcmd(const struct il_addsta_cmd *cmd, u8 * data)
17027ac9a364SKalle Valo {
17037ac9a364SKalle Valo 	struct il4965_addsta_cmd *addsta = (struct il4965_addsta_cmd *)data;
17047ac9a364SKalle Valo 	addsta->mode = cmd->mode;
17057ac9a364SKalle Valo 	memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
17067ac9a364SKalle Valo 	memcpy(&addsta->key, &cmd->key, sizeof(struct il4965_keyinfo));
17077ac9a364SKalle Valo 	addsta->station_flags = cmd->station_flags;
17087ac9a364SKalle Valo 	addsta->station_flags_msk = cmd->station_flags_msk;
17097ac9a364SKalle Valo 	addsta->tid_disable_tx = cmd->tid_disable_tx;
17107ac9a364SKalle Valo 	addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
17117ac9a364SKalle Valo 	addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
17127ac9a364SKalle Valo 	addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
17137ac9a364SKalle Valo 	addsta->sleep_tx_count = cmd->sleep_tx_count;
17147ac9a364SKalle Valo 	addsta->reserved1 = cpu_to_le16(0);
17157ac9a364SKalle Valo 	addsta->reserved2 = cpu_to_le16(0);
17167ac9a364SKalle Valo 
17177ac9a364SKalle Valo 	return (u16) sizeof(struct il4965_addsta_cmd);
17187ac9a364SKalle Valo }
17197ac9a364SKalle Valo 
17207ac9a364SKalle Valo static void
il4965_post_scan(struct il_priv * il)17217ac9a364SKalle Valo il4965_post_scan(struct il_priv *il)
17227ac9a364SKalle Valo {
17237ac9a364SKalle Valo 	/*
17247ac9a364SKalle Valo 	 * Since setting the RXON may have been deferred while
17257ac9a364SKalle Valo 	 * performing the scan, fire one off if needed
17267ac9a364SKalle Valo 	 */
17277ac9a364SKalle Valo 	if (memcmp(&il->staging, &il->active, sizeof(il->staging)))
17287ac9a364SKalle Valo 		il_commit_rxon(il);
17297ac9a364SKalle Valo }
17307ac9a364SKalle Valo 
17317ac9a364SKalle Valo static void
il4965_post_associate(struct il_priv * il)17327ac9a364SKalle Valo il4965_post_associate(struct il_priv *il)
17337ac9a364SKalle Valo {
17347ac9a364SKalle Valo 	struct ieee80211_vif *vif = il->vif;
17357ac9a364SKalle Valo 	int ret = 0;
17367ac9a364SKalle Valo 
17377ac9a364SKalle Valo 	if (!vif || !il->is_open)
17387ac9a364SKalle Valo 		return;
17397ac9a364SKalle Valo 
17407ac9a364SKalle Valo 	if (test_bit(S_EXIT_PENDING, &il->status))
17417ac9a364SKalle Valo 		return;
17427ac9a364SKalle Valo 
17437ac9a364SKalle Valo 	il_scan_cancel_timeout(il, 200);
17447ac9a364SKalle Valo 
17457ac9a364SKalle Valo 	il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
17467ac9a364SKalle Valo 	il_commit_rxon(il);
17477ac9a364SKalle Valo 
17487ac9a364SKalle Valo 	ret = il_send_rxon_timing(il);
17497ac9a364SKalle Valo 	if (ret)
17507ac9a364SKalle Valo 		IL_WARN("RXON timing - " "Attempting to continue.\n");
17517ac9a364SKalle Valo 
17527ac9a364SKalle Valo 	il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
17537ac9a364SKalle Valo 
17547ac9a364SKalle Valo 	il_set_rxon_ht(il, &il->current_ht_config);
17557ac9a364SKalle Valo 
17567ac9a364SKalle Valo 	if (il->ops->set_rxon_chain)
17577ac9a364SKalle Valo 		il->ops->set_rxon_chain(il);
17587ac9a364SKalle Valo 
1759*f276e20bSJohannes Berg 	il->staging.assoc_id = cpu_to_le16(vif->cfg.aid);
17607ac9a364SKalle Valo 
1761*f276e20bSJohannes Berg 	D_ASSOC("assoc id %d beacon interval %d\n", vif->cfg.aid,
17627ac9a364SKalle Valo 		vif->bss_conf.beacon_int);
17637ac9a364SKalle Valo 
17647ac9a364SKalle Valo 	if (vif->bss_conf.use_short_preamble)
17657ac9a364SKalle Valo 		il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
17667ac9a364SKalle Valo 	else
17677ac9a364SKalle Valo 		il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
17687ac9a364SKalle Valo 
17697ac9a364SKalle Valo 	if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
17707ac9a364SKalle Valo 		if (vif->bss_conf.use_short_slot)
17717ac9a364SKalle Valo 			il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
17727ac9a364SKalle Valo 		else
17737ac9a364SKalle Valo 			il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
17747ac9a364SKalle Valo 	}
17757ac9a364SKalle Valo 
17767ac9a364SKalle Valo 	il_commit_rxon(il);
17777ac9a364SKalle Valo 
1778*f276e20bSJohannes Berg 	D_ASSOC("Associated as %d to: %pM\n", vif->cfg.aid,
17797ac9a364SKalle Valo 		il->active.bssid_addr);
17807ac9a364SKalle Valo 
17817ac9a364SKalle Valo 	switch (vif->type) {
17827ac9a364SKalle Valo 	case NL80211_IFTYPE_STATION:
17837ac9a364SKalle Valo 		break;
17847ac9a364SKalle Valo 	case NL80211_IFTYPE_ADHOC:
17857ac9a364SKalle Valo 		il4965_send_beacon_cmd(il);
17867ac9a364SKalle Valo 		break;
17877ac9a364SKalle Valo 	default:
17887ac9a364SKalle Valo 		IL_ERR("%s Should not be called in %d mode\n", __func__,
17897ac9a364SKalle Valo 		       vif->type);
17907ac9a364SKalle Valo 		break;
17917ac9a364SKalle Valo 	}
17927ac9a364SKalle Valo 
17937ac9a364SKalle Valo 	/* the chain noise calibration will enabled PM upon completion
17947ac9a364SKalle Valo 	 * If chain noise has already been run, then we need to enable
17957ac9a364SKalle Valo 	 * power management here */
17967ac9a364SKalle Valo 	if (il->chain_noise_data.state == IL_CHAIN_NOISE_DONE)
17977ac9a364SKalle Valo 		il_power_update_mode(il, false);
17987ac9a364SKalle Valo 
17997ac9a364SKalle Valo 	/* Enable Rx differential gain and sensitivity calibrations */
18007ac9a364SKalle Valo 	il4965_chain_noise_reset(il);
18017ac9a364SKalle Valo 	il->start_calib = 1;
18027ac9a364SKalle Valo }
18037ac9a364SKalle Valo 
18047ac9a364SKalle Valo static void
il4965_config_ap(struct il_priv * il)18057ac9a364SKalle Valo il4965_config_ap(struct il_priv *il)
18067ac9a364SKalle Valo {
18077ac9a364SKalle Valo 	struct ieee80211_vif *vif = il->vif;
18087ac9a364SKalle Valo 	int ret = 0;
18097ac9a364SKalle Valo 
18107ac9a364SKalle Valo 	lockdep_assert_held(&il->mutex);
18117ac9a364SKalle Valo 
18127ac9a364SKalle Valo 	if (test_bit(S_EXIT_PENDING, &il->status))
18137ac9a364SKalle Valo 		return;
18147ac9a364SKalle Valo 
18157ac9a364SKalle Valo 	/* The following should be done only at AP bring up */
18167ac9a364SKalle Valo 	if (!il_is_associated(il)) {
18177ac9a364SKalle Valo 
18187ac9a364SKalle Valo 		/* RXON - unassoc (to set timing command) */
18197ac9a364SKalle Valo 		il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
18207ac9a364SKalle Valo 		il_commit_rxon(il);
18217ac9a364SKalle Valo 
18227ac9a364SKalle Valo 		/* RXON Timing */
18237ac9a364SKalle Valo 		ret = il_send_rxon_timing(il);
18247ac9a364SKalle Valo 		if (ret)
18257ac9a364SKalle Valo 			IL_WARN("RXON timing failed - "
18267ac9a364SKalle Valo 				"Attempting to continue.\n");
18277ac9a364SKalle Valo 
18287ac9a364SKalle Valo 		/* AP has all antennas */
18297ac9a364SKalle Valo 		il->chain_noise_data.active_chains = il->hw_params.valid_rx_ant;
18307ac9a364SKalle Valo 		il_set_rxon_ht(il, &il->current_ht_config);
18317ac9a364SKalle Valo 		if (il->ops->set_rxon_chain)
18327ac9a364SKalle Valo 			il->ops->set_rxon_chain(il);
18337ac9a364SKalle Valo 
18347ac9a364SKalle Valo 		il->staging.assoc_id = 0;
18357ac9a364SKalle Valo 
18367ac9a364SKalle Valo 		if (vif->bss_conf.use_short_preamble)
18377ac9a364SKalle Valo 			il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
18387ac9a364SKalle Valo 		else
18397ac9a364SKalle Valo 			il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
18407ac9a364SKalle Valo 
18417ac9a364SKalle Valo 		if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
18427ac9a364SKalle Valo 			if (vif->bss_conf.use_short_slot)
18437ac9a364SKalle Valo 				il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
18447ac9a364SKalle Valo 			else
18457ac9a364SKalle Valo 				il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
18467ac9a364SKalle Valo 		}
18477ac9a364SKalle Valo 		/* need to send beacon cmd before committing assoc RXON! */
18487ac9a364SKalle Valo 		il4965_send_beacon_cmd(il);
18497ac9a364SKalle Valo 		/* restore RXON assoc */
18507ac9a364SKalle Valo 		il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
18517ac9a364SKalle Valo 		il_commit_rxon(il);
18527ac9a364SKalle Valo 	}
18537ac9a364SKalle Valo 	il4965_send_beacon_cmd(il);
18547ac9a364SKalle Valo }
18557ac9a364SKalle Valo 
18567ac9a364SKalle Valo const struct il_ops il4965_ops = {
18577ac9a364SKalle Valo 	.txq_update_byte_cnt_tbl = il4965_txq_update_byte_cnt_tbl,
18587ac9a364SKalle Valo 	.txq_attach_buf_to_tfd = il4965_hw_txq_attach_buf_to_tfd,
18597ac9a364SKalle Valo 	.txq_free_tfd = il4965_hw_txq_free_tfd,
18607ac9a364SKalle Valo 	.txq_init = il4965_hw_tx_queue_init,
18617ac9a364SKalle Valo 	.is_valid_rtc_data_addr = il4965_hw_valid_rtc_data_addr,
18627ac9a364SKalle Valo 	.init_alive_start = il4965_init_alive_start,
18637ac9a364SKalle Valo 	.load_ucode = il4965_load_bsm,
18647ac9a364SKalle Valo 	.dump_nic_error_log = il4965_dump_nic_error_log,
18657ac9a364SKalle Valo 	.dump_fh = il4965_dump_fh,
18667ac9a364SKalle Valo 	.set_channel_switch = il4965_hw_channel_switch,
18677ac9a364SKalle Valo 	.apm_init = il_apm_init,
18687ac9a364SKalle Valo 	.send_tx_power = il4965_send_tx_power,
18697ac9a364SKalle Valo 	.update_chain_flags = il4965_update_chain_flags,
18707ac9a364SKalle Valo 	.eeprom_acquire_semaphore = il4965_eeprom_acquire_semaphore,
18717ac9a364SKalle Valo 	.eeprom_release_semaphore = il4965_eeprom_release_semaphore,
18727ac9a364SKalle Valo 
18737ac9a364SKalle Valo 	.rxon_assoc = il4965_send_rxon_assoc,
18747ac9a364SKalle Valo 	.commit_rxon = il4965_commit_rxon,
18757ac9a364SKalle Valo 	.set_rxon_chain = il4965_set_rxon_chain,
18767ac9a364SKalle Valo 
18777ac9a364SKalle Valo 	.get_hcmd_size = il4965_get_hcmd_size,
18787ac9a364SKalle Valo 	.build_addsta_hcmd = il4965_build_addsta_hcmd,
18797ac9a364SKalle Valo 	.request_scan = il4965_request_scan,
18807ac9a364SKalle Valo 	.post_scan = il4965_post_scan,
18817ac9a364SKalle Valo 
18827ac9a364SKalle Valo 	.post_associate = il4965_post_associate,
18837ac9a364SKalle Valo 	.config_ap = il4965_config_ap,
18847ac9a364SKalle Valo 	.manage_ibss_station = il4965_manage_ibss_station,
18857ac9a364SKalle Valo 	.update_bcast_stations = il4965_update_bcast_stations,
18867ac9a364SKalle Valo 
18877ac9a364SKalle Valo 	.send_led_cmd = il4965_send_led_cmd,
18887ac9a364SKalle Valo };
18897ac9a364SKalle Valo 
18907ac9a364SKalle Valo struct il_cfg il4965_cfg = {
18917ac9a364SKalle Valo 	.name = "Intel(R) Wireless WiFi Link 4965AGN",
18927ac9a364SKalle Valo 	.fw_name_pre = IL4965_FW_PRE,
18937ac9a364SKalle Valo 	.ucode_api_max = IL4965_UCODE_API_MAX,
18947ac9a364SKalle Valo 	.ucode_api_min = IL4965_UCODE_API_MIN,
18957ac9a364SKalle Valo 	.sku = IL_SKU_A | IL_SKU_G | IL_SKU_N,
18967ac9a364SKalle Valo 	.valid_tx_ant = ANT_AB,
18977ac9a364SKalle Valo 	.valid_rx_ant = ANT_ABC,
18987ac9a364SKalle Valo 	.eeprom_ver = EEPROM_4965_EEPROM_VERSION,
18997ac9a364SKalle Valo 	.eeprom_calib_ver = EEPROM_4965_TX_POWER_VERSION,
19007ac9a364SKalle Valo 	.mod_params = &il4965_mod_params,
19017ac9a364SKalle Valo 	.led_mode = IL_LED_BLINK,
19027ac9a364SKalle Valo 	/*
19037ac9a364SKalle Valo 	 * Force use of chains B and C for scan RX on 5 GHz band
19047ac9a364SKalle Valo 	 * because the device has off-channel reception on chain A.
19057ac9a364SKalle Valo 	 */
190657fbcce3SJohannes Berg 	.scan_rx_antennas[NL80211_BAND_5GHZ] = ANT_BC,
19077ac9a364SKalle Valo 
19087ac9a364SKalle Valo 	.eeprom_size = IL4965_EEPROM_IMG_SIZE,
19097ac9a364SKalle Valo 	.num_of_queues = IL49_NUM_QUEUES,
19107ac9a364SKalle Valo 	.num_of_ampdu_queues = IL49_NUM_AMPDU_QUEUES,
19117ac9a364SKalle Valo 	.pll_cfg_val = 0,
19127ac9a364SKalle Valo 	.set_l0s = true,
19137ac9a364SKalle Valo 	.use_bsm = true,
19147ac9a364SKalle Valo 	.led_compensation = 61,
19157ac9a364SKalle Valo 	.chain_noise_num_beacons = IL4965_CAL_NUM_BEACONS,
19167ac9a364SKalle Valo 	.wd_timeout = IL_DEF_WD_TIMEOUT,
19177ac9a364SKalle Valo 	.temperature_kelvin = true,
19187ac9a364SKalle Valo 	.ucode_tracing = true,
19197ac9a364SKalle Valo 	.sensitivity_calib_by_driver = true,
19207ac9a364SKalle Valo 	.chain_noise_calib_by_driver = true,
19217ac9a364SKalle Valo 
19227ac9a364SKalle Valo 	.regulatory_bands = {
19237ac9a364SKalle Valo 		EEPROM_REGULATORY_BAND_1_CHANNELS,
19247ac9a364SKalle Valo 		EEPROM_REGULATORY_BAND_2_CHANNELS,
19257ac9a364SKalle Valo 		EEPROM_REGULATORY_BAND_3_CHANNELS,
19267ac9a364SKalle Valo 		EEPROM_REGULATORY_BAND_4_CHANNELS,
19277ac9a364SKalle Valo 		EEPROM_REGULATORY_BAND_5_CHANNELS,
19287ac9a364SKalle Valo 		EEPROM_4965_REGULATORY_BAND_24_HT40_CHANNELS,
19297ac9a364SKalle Valo 		EEPROM_4965_REGULATORY_BAND_52_HT40_CHANNELS
19307ac9a364SKalle Valo 	},
19317ac9a364SKalle Valo 
19327ac9a364SKalle Valo };
19337ac9a364SKalle Valo 
19347ac9a364SKalle Valo /* Module firmware */
19357ac9a364SKalle Valo MODULE_FIRMWARE(IL4965_MODULE_FIRMWARE(IL4965_UCODE_API_MAX));
1936