iwl-4965.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/pci.h>
  30. #include <linux/dma-mapping.h>
  31. #include <linux/delay.h>
  32. #include <linux/sched.h>
  33. #include <linux/skbuff.h>
  34. #include <linux/netdevice.h>
  35. #include <linux/wireless.h>
  36. #include <net/mac80211.h>
  37. #include <linux/etherdevice.h>
  38. #include <asm/unaligned.h>
  39. #include "iwl-eeprom.h"
  40. #include "iwl-dev.h"
  41. #include "iwl-core.h"
  42. #include "iwl-io.h"
  43. #include "iwl-helpers.h"
  44. #include "iwl-calib.h"
  45. #include "iwl-sta.h"
  46. #include "iwl-agn-led.h"
  47. #include "iwl-agn.h"
  48. #include "iwl-agn-debugfs.h"
  49. static int iwl4965_send_tx_power(struct iwl_priv *priv);
  50. static int iwl4965_hw_get_temperature(struct iwl_priv *priv);
  51. /* Highest firmware API version supported */
  52. #define IWL4965_UCODE_API_MAX 2
  53. /* Lowest firmware API version supported */
  54. #define IWL4965_UCODE_API_MIN 2
  55. #define IWL4965_FW_PRE "iwlwifi-4965-"
  56. #define _IWL4965_MODULE_FIRMWARE(api) IWL4965_FW_PRE #api ".ucode"
  57. #define IWL4965_MODULE_FIRMWARE(api) _IWL4965_MODULE_FIRMWARE(api)
  58. /* check contents of special bootstrap uCode SRAM */
  59. static int iwl4965_verify_bsm(struct iwl_priv *priv)
  60. {
  61. __le32 *image = priv->ucode_boot.v_addr;
  62. u32 len = priv->ucode_boot.len;
  63. u32 reg;
  64. u32 val;
  65. IWL_DEBUG_INFO(priv, "Begin verify bsm\n");
  66. /* verify BSM SRAM contents */
  67. val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
  68. for (reg = BSM_SRAM_LOWER_BOUND;
  69. reg < BSM_SRAM_LOWER_BOUND + len;
  70. reg += sizeof(u32), image++) {
  71. val = iwl_read_prph(priv, reg);
  72. if (val != le32_to_cpu(*image)) {
  73. IWL_ERR(priv, "BSM uCode verification failed at "
  74. "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
  75. BSM_SRAM_LOWER_BOUND,
  76. reg - BSM_SRAM_LOWER_BOUND, len,
  77. val, le32_to_cpu(*image));
  78. return -EIO;
  79. }
  80. }
  81. IWL_DEBUG_INFO(priv, "BSM bootstrap uCode image OK\n");
  82. return 0;
  83. }
  84. /**
  85. * iwl4965_load_bsm - Load bootstrap instructions
  86. *
  87. * BSM operation:
  88. *
  89. * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
  90. * in special SRAM that does not power down during RFKILL. When powering back
  91. * up after power-saving sleeps (or during initial uCode load), the BSM loads
  92. * the bootstrap program into the on-board processor, and starts it.
  93. *
  94. * The bootstrap program loads (via DMA) instructions and data for a new
  95. * program from host DRAM locations indicated by the host driver in the
  96. * BSM_DRAM_* registers. Once the new program is loaded, it starts
  97. * automatically.
  98. *
  99. * When initializing the NIC, the host driver points the BSM to the
  100. * "initialize" uCode image. This uCode sets up some internal data, then
  101. * notifies host via "initialize alive" that it is complete.
  102. *
  103. * The host then replaces the BSM_DRAM_* pointer values to point to the
  104. * normal runtime uCode instructions and a backup uCode data cache buffer
  105. * (filled initially with starting data values for the on-board processor),
  106. * then triggers the "initialize" uCode to load and launch the runtime uCode,
  107. * which begins normal operation.
  108. *
  109. * When doing a power-save shutdown, runtime uCode saves data SRAM into
  110. * the backup data cache in DRAM before SRAM is powered down.
  111. *
  112. * When powering back up, the BSM loads the bootstrap program. This reloads
  113. * the runtime uCode instructions and the backup data cache into SRAM,
  114. * and re-launches the runtime uCode from where it left off.
  115. */
  116. static int iwl4965_load_bsm(struct iwl_priv *priv)
  117. {
  118. __le32 *image = priv->ucode_boot.v_addr;
  119. u32 len = priv->ucode_boot.len;
  120. dma_addr_t pinst;
  121. dma_addr_t pdata;
  122. u32 inst_len;
  123. u32 data_len;
  124. int i;
  125. u32 done;
  126. u32 reg_offset;
  127. int ret;
  128. IWL_DEBUG_INFO(priv, "Begin load bsm\n");
  129. priv->ucode_type = UCODE_RT;
  130. /* make sure bootstrap program is no larger than BSM's SRAM size */
  131. if (len > IWL49_MAX_BSM_SIZE)
  132. return -EINVAL;
  133. /* Tell bootstrap uCode where to find the "Initialize" uCode
  134. * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
  135. * NOTE: iwl_init_alive_start() will replace these values,
  136. * after the "initialize" uCode has run, to point to
  137. * runtime/protocol instructions and backup data cache.
  138. */
  139. pinst = priv->ucode_init.p_addr >> 4;
  140. pdata = priv->ucode_init_data.p_addr >> 4;
  141. inst_len = priv->ucode_init.len;
  142. data_len = priv->ucode_init_data.len;
  143. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  144. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  145. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
  146. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
  147. /* Fill BSM memory with bootstrap instructions */
  148. for (reg_offset = BSM_SRAM_LOWER_BOUND;
  149. reg_offset < BSM_SRAM_LOWER_BOUND + len;
  150. reg_offset += sizeof(u32), image++)
  151. _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
  152. ret = iwl4965_verify_bsm(priv);
  153. if (ret)
  154. return ret;
  155. /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
  156. iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
  157. iwl_write_prph(priv, BSM_WR_MEM_DST_REG, IWL49_RTC_INST_LOWER_BOUND);
  158. iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
  159. /* Load bootstrap code into instruction SRAM now,
  160. * to prepare to load "initialize" uCode */
  161. iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
  162. /* Wait for load of bootstrap uCode to finish */
  163. for (i = 0; i < 100; i++) {
  164. done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
  165. if (!(done & BSM_WR_CTRL_REG_BIT_START))
  166. break;
  167. udelay(10);
  168. }
  169. if (i < 100)
  170. IWL_DEBUG_INFO(priv, "BSM write complete, poll %d iterations\n", i);
  171. else {
  172. IWL_ERR(priv, "BSM write did not complete!\n");
  173. return -EIO;
  174. }
  175. /* Enable future boot loads whenever power management unit triggers it
  176. * (e.g. when powering back up after power-save shutdown) */
  177. iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
  178. return 0;
  179. }
  180. /**
  181. * iwl4965_set_ucode_ptrs - Set uCode address location
  182. *
  183. * Tell initialization uCode where to find runtime uCode.
  184. *
  185. * BSM registers initially contain pointers to initialization uCode.
  186. * We need to replace them to load runtime uCode inst and data,
  187. * and to save runtime data when powering down.
  188. */
  189. static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
  190. {
  191. dma_addr_t pinst;
  192. dma_addr_t pdata;
  193. int ret = 0;
  194. /* bits 35:4 for 4965 */
  195. pinst = priv->ucode_code.p_addr >> 4;
  196. pdata = priv->ucode_data_backup.p_addr >> 4;
  197. /* Tell bootstrap uCode where to find image to load */
  198. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  199. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  200. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  201. priv->ucode_data.len);
  202. /* Inst byte count must be last to set up, bit 31 signals uCode
  203. * that all new ptr/size info is in place */
  204. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  205. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  206. IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
  207. return ret;
  208. }
  209. /**
  210. * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
  211. *
  212. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  213. *
  214. * The 4965 "initialize" ALIVE reply contains calibration data for:
  215. * Voltage, temperature, and MIMO tx gain correction, now stored in priv
  216. * (3945 does not contain this data).
  217. *
  218. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  219. */
  220. static void iwl4965_init_alive_start(struct iwl_priv *priv)
  221. {
  222. /* Check alive response for "valid" sign from uCode */
  223. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  224. /* We had an error bringing up the hardware, so take it
  225. * all the way back down so we can try again */
  226. IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
  227. goto restart;
  228. }
  229. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  230. * This is a paranoid check, because we would not have gotten the
  231. * "initialize" alive if code weren't properly loaded. */
  232. if (iwl_verify_ucode(priv)) {
  233. /* Runtime instruction load was bad;
  234. * take it all the way back down so we can try again */
  235. IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
  236. goto restart;
  237. }
  238. /* Calculate temperature */
  239. priv->temperature = iwl4965_hw_get_temperature(priv);
  240. /* Send pointers to protocol/runtime uCode image ... init code will
  241. * load and launch runtime uCode, which will send us another "Alive"
  242. * notification. */
  243. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  244. if (iwl4965_set_ucode_ptrs(priv)) {
  245. /* Runtime instruction load won't happen;
  246. * take it all the way back down so we can try again */
  247. IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
  248. goto restart;
  249. }
  250. return;
  251. restart:
  252. queue_work(priv->workqueue, &priv->restart);
  253. }
  254. static bool is_ht40_channel(__le32 rxon_flags)
  255. {
  256. int chan_mod = le32_to_cpu(rxon_flags & RXON_FLG_CHANNEL_MODE_MSK)
  257. >> RXON_FLG_CHANNEL_MODE_POS;
  258. return ((chan_mod == CHANNEL_MODE_PURE_40) ||
  259. (chan_mod == CHANNEL_MODE_MIXED));
  260. }
  261. /*
  262. * EEPROM handlers
  263. */
  264. static u16 iwl4965_eeprom_calib_version(struct iwl_priv *priv)
  265. {
  266. return iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
  267. }
  268. /*
  269. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  270. * must be called under priv->lock and mac access
  271. */
  272. static void iwl4965_txq_set_sched(struct iwl_priv *priv, u32 mask)
  273. {
  274. iwl_write_prph(priv, IWL49_SCD_TXFACT, mask);
  275. }
  276. static void iwl4965_nic_config(struct iwl_priv *priv)
  277. {
  278. unsigned long flags;
  279. u16 radio_cfg;
  280. spin_lock_irqsave(&priv->lock, flags);
  281. radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
  282. /* write radio config values to register */
  283. if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
  284. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  285. EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
  286. EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
  287. EEPROM_RF_CFG_DASH_MSK(radio_cfg));
  288. /* set CSR_HW_CONFIG_REG for uCode use */
  289. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  290. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  291. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
  292. priv->calib_info = (struct iwl_eeprom_calib_info *)
  293. iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
  294. spin_unlock_irqrestore(&priv->lock, flags);
  295. }
  296. /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
  297. * Called after every association, but this runs only once!
  298. * ... once chain noise is calibrated the first time, it's good forever. */
  299. static void iwl4965_chain_noise_reset(struct iwl_priv *priv)
  300. {
  301. struct iwl_chain_noise_data *data = &(priv->chain_noise_data);
  302. if ((data->state == IWL_CHAIN_NOISE_ALIVE) &&
  303. iwl_is_any_associated(priv)) {
  304. struct iwl_calib_diff_gain_cmd cmd;
  305. /* clear data for chain noise calibration algorithm */
  306. data->chain_noise_a = 0;
  307. data->chain_noise_b = 0;
  308. data->chain_noise_c = 0;
  309. data->chain_signal_a = 0;
  310. data->chain_signal_b = 0;
  311. data->chain_signal_c = 0;
  312. data->beacon_count = 0;
  313. memset(&cmd, 0, sizeof(cmd));
  314. cmd.hdr.op_code = IWL_PHY_CALIBRATE_DIFF_GAIN_CMD;
  315. cmd.diff_gain_a = 0;
  316. cmd.diff_gain_b = 0;
  317. cmd.diff_gain_c = 0;
  318. if (iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  319. sizeof(cmd), &cmd))
  320. IWL_ERR(priv,
  321. "Could not send REPLY_PHY_CALIBRATION_CMD\n");
  322. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  323. IWL_DEBUG_CALIB(priv, "Run chain_noise_calibrate\n");
  324. }
  325. }
  326. static void iwl4965_gain_computation(struct iwl_priv *priv,
  327. u32 *average_noise,
  328. u16 min_average_noise_antenna_i,
  329. u32 min_average_noise,
  330. u8 default_chain)
  331. {
  332. int i, ret;
  333. struct iwl_chain_noise_data *data = &priv->chain_noise_data;
  334. data->delta_gain_code[min_average_noise_antenna_i] = 0;
  335. for (i = default_chain; i < NUM_RX_CHAINS; i++) {
  336. s32 delta_g = 0;
  337. if (!(data->disconn_array[i]) &&
  338. (data->delta_gain_code[i] ==
  339. CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
  340. delta_g = average_noise[i] - min_average_noise;
  341. data->delta_gain_code[i] = (u8)((delta_g * 10) / 15);
  342. data->delta_gain_code[i] =
  343. min(data->delta_gain_code[i],
  344. (u8) CHAIN_NOISE_MAX_DELTA_GAIN_CODE);
  345. data->delta_gain_code[i] =
  346. (data->delta_gain_code[i] | (1 << 2));
  347. } else {
  348. data->delta_gain_code[i] = 0;
  349. }
  350. }
  351. IWL_DEBUG_CALIB(priv, "delta_gain_codes: a %d b %d c %d\n",
  352. data->delta_gain_code[0],
  353. data->delta_gain_code[1],
  354. data->delta_gain_code[2]);
  355. /* Differential gain gets sent to uCode only once */
  356. if (!data->radio_write) {
  357. struct iwl_calib_diff_gain_cmd cmd;
  358. data->radio_write = 1;
  359. memset(&cmd, 0, sizeof(cmd));
  360. cmd.hdr.op_code = IWL_PHY_CALIBRATE_DIFF_GAIN_CMD;
  361. cmd.diff_gain_a = data->delta_gain_code[0];
  362. cmd.diff_gain_b = data->delta_gain_code[1];
  363. cmd.diff_gain_c = data->delta_gain_code[2];
  364. ret = iwl_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  365. sizeof(cmd), &cmd);
  366. if (ret)
  367. IWL_DEBUG_CALIB(priv, "fail sending cmd "
  368. "REPLY_PHY_CALIBRATION_CMD\n");
  369. /* TODO we might want recalculate
  370. * rx_chain in rxon cmd */
  371. /* Mark so we run this algo only once! */
  372. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  373. }
  374. }
  375. static void iwl4965_bg_txpower_work(struct work_struct *work)
  376. {
  377. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  378. txpower_work);
  379. /* If a scan happened to start before we got here
  380. * then just return; the statistics notification will
  381. * kick off another scheduled work to compensate for
  382. * any temperature delta we missed here. */
  383. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  384. test_bit(STATUS_SCANNING, &priv->status))
  385. return;
  386. mutex_lock(&priv->mutex);
  387. /* Regardless of if we are associated, we must reconfigure the
  388. * TX power since frames can be sent on non-radar channels while
  389. * not associated */
  390. iwl4965_send_tx_power(priv);
  391. /* Update last_temperature to keep is_calib_needed from running
  392. * when it isn't needed... */
  393. priv->last_temperature = priv->temperature;
  394. mutex_unlock(&priv->mutex);
  395. }
  396. /*
  397. * Acquire priv->lock before calling this function !
  398. */
  399. static void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
  400. {
  401. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  402. (index & 0xff) | (txq_id << 8));
  403. iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
  404. }
  405. /**
  406. * iwl4965_tx_queue_set_status - (optionally) start Tx/Cmd queue
  407. * @tx_fifo_id: Tx DMA/FIFO channel (range 0-7) that the queue will feed
  408. * @scd_retry: (1) Indicates queue will be used in aggregation mode
  409. *
  410. * NOTE: Acquire priv->lock before calling this function !
  411. */
  412. static void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
  413. struct iwl_tx_queue *txq,
  414. int tx_fifo_id, int scd_retry)
  415. {
  416. int txq_id = txq->q.id;
  417. /* Find out whether to activate Tx queue */
  418. int active = test_bit(txq_id, &priv->txq_ctx_active_msk) ? 1 : 0;
  419. /* Set up and activate */
  420. iwl_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
  421. (active << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  422. (tx_fifo_id << IWL49_SCD_QUEUE_STTS_REG_POS_TXF) |
  423. (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_WSL) |
  424. (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  425. IWL49_SCD_QUEUE_STTS_REG_MSK);
  426. txq->sched_retry = scd_retry;
  427. IWL_DEBUG_INFO(priv, "%s %s Queue %d on AC %d\n",
  428. active ? "Activate" : "Deactivate",
  429. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  430. }
  431. static const s8 default_queue_to_tx_fifo[] = {
  432. IWL_TX_FIFO_VO,
  433. IWL_TX_FIFO_VI,
  434. IWL_TX_FIFO_BE,
  435. IWL_TX_FIFO_BK,
  436. IWL49_CMD_FIFO_NUM,
  437. IWL_TX_FIFO_UNUSED,
  438. IWL_TX_FIFO_UNUSED,
  439. };
  440. static int iwl4965_alive_notify(struct iwl_priv *priv)
  441. {
  442. u32 a;
  443. unsigned long flags;
  444. int i, chan;
  445. u32 reg_val;
  446. spin_lock_irqsave(&priv->lock, flags);
  447. /* Clear 4965's internal Tx Scheduler data base */
  448. priv->scd_base_addr = iwl_read_prph(priv, IWL49_SCD_SRAM_BASE_ADDR);
  449. a = priv->scd_base_addr + IWL49_SCD_CONTEXT_DATA_OFFSET;
  450. for (; a < priv->scd_base_addr + IWL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  451. iwl_write_targ_mem(priv, a, 0);
  452. for (; a < priv->scd_base_addr + IWL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
  453. iwl_write_targ_mem(priv, a, 0);
  454. for (; a < priv->scd_base_addr +
  455. IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(priv->hw_params.max_txq_num); a += 4)
  456. iwl_write_targ_mem(priv, a, 0);
  457. /* Tel 4965 where to find Tx byte count tables */
  458. iwl_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
  459. priv->scd_bc_tbls.dma >> 10);
  460. /* Enable DMA channel */
  461. for (chan = 0; chan < FH49_TCSR_CHNL_NUM ; chan++)
  462. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  463. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  464. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  465. /* Update FH chicken bits */
  466. reg_val = iwl_read_direct32(priv, FH_TX_CHICKEN_BITS_REG);
  467. iwl_write_direct32(priv, FH_TX_CHICKEN_BITS_REG,
  468. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  469. /* Disable chain mode for all queues */
  470. iwl_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
  471. /* Initialize each Tx queue (including the command queue) */
  472. for (i = 0; i < priv->hw_params.max_txq_num; i++) {
  473. /* TFD circular buffer read/write indexes */
  474. iwl_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
  475. iwl_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  476. /* Max Tx Window size for Scheduler-ACK mode */
  477. iwl_write_targ_mem(priv, priv->scd_base_addr +
  478. IWL49_SCD_CONTEXT_QUEUE_OFFSET(i),
  479. (SCD_WIN_SIZE <<
  480. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  481. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  482. /* Frame limit */
  483. iwl_write_targ_mem(priv, priv->scd_base_addr +
  484. IWL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
  485. sizeof(u32),
  486. (SCD_FRAME_LIMIT <<
  487. IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  488. IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  489. }
  490. iwl_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
  491. (1 << priv->hw_params.max_txq_num) - 1);
  492. /* Activate all Tx DMA/FIFO channels */
  493. priv->cfg->ops->lib->txq_set_sched(priv, IWL_MASK(0, 6));
  494. iwl4965_set_wr_ptrs(priv, IWL_DEFAULT_CMD_QUEUE_NUM, 0);
  495. /* make sure all queue are not stopped */
  496. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  497. for (i = 0; i < 4; i++)
  498. atomic_set(&priv->queue_stop_count[i], 0);
  499. /* reset to 0 to enable all the queue first */
  500. priv->txq_ctx_active_msk = 0;
  501. /* Map each Tx/cmd queue to its corresponding fifo */
  502. BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
  503. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  504. int ac = default_queue_to_tx_fifo[i];
  505. iwl_txq_ctx_activate(priv, i);
  506. if (ac == IWL_TX_FIFO_UNUSED)
  507. continue;
  508. iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
  509. }
  510. spin_unlock_irqrestore(&priv->lock, flags);
  511. return 0;
  512. }
  513. static struct iwl_sensitivity_ranges iwl4965_sensitivity = {
  514. .min_nrg_cck = 97,
  515. .max_nrg_cck = 0, /* not used, set to 0 */
  516. .auto_corr_min_ofdm = 85,
  517. .auto_corr_min_ofdm_mrc = 170,
  518. .auto_corr_min_ofdm_x1 = 105,
  519. .auto_corr_min_ofdm_mrc_x1 = 220,
  520. .auto_corr_max_ofdm = 120,
  521. .auto_corr_max_ofdm_mrc = 210,
  522. .auto_corr_max_ofdm_x1 = 140,
  523. .auto_corr_max_ofdm_mrc_x1 = 270,
  524. .auto_corr_min_cck = 125,
  525. .auto_corr_max_cck = 200,
  526. .auto_corr_min_cck_mrc = 200,
  527. .auto_corr_max_cck_mrc = 400,
  528. .nrg_th_cck = 100,
  529. .nrg_th_ofdm = 100,
  530. .barker_corr_th_min = 190,
  531. .barker_corr_th_min_mrc = 390,
  532. .nrg_th_cca = 62,
  533. };
  534. static void iwl4965_set_ct_threshold(struct iwl_priv *priv)
  535. {
  536. /* want Kelvin */
  537. priv->hw_params.ct_kill_threshold =
  538. CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD_LEGACY);
  539. }
  540. /**
  541. * iwl4965_hw_set_hw_params
  542. *
  543. * Called when initializing driver
  544. */
  545. static int iwl4965_hw_set_hw_params(struct iwl_priv *priv)
  546. {
  547. if (priv->cfg->mod_params->num_of_queues >= IWL_MIN_NUM_QUEUES &&
  548. priv->cfg->mod_params->num_of_queues <= IWL49_NUM_QUEUES)
  549. priv->cfg->base_params->num_of_queues =
  550. priv->cfg->mod_params->num_of_queues;
  551. priv->hw_params.max_txq_num = priv->cfg->base_params->num_of_queues;
  552. priv->hw_params.dma_chnl_num = FH49_TCSR_CHNL_NUM;
  553. priv->hw_params.scd_bc_tbls_size =
  554. priv->cfg->base_params->num_of_queues *
  555. sizeof(struct iwl4965_scd_bc_tbl);
  556. priv->hw_params.tfd_size = sizeof(struct iwl_tfd);
  557. priv->hw_params.max_stations = IWL4965_STATION_COUNT;
  558. priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id = IWL4965_BROADCAST_ID;
  559. priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
  560. priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
  561. priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
  562. priv->hw_params.ht40_channel = BIT(IEEE80211_BAND_5GHZ);
  563. priv->hw_params.rx_wrt_ptr_reg = FH_RSCSR_CHNL0_WPTR;
  564. priv->hw_params.tx_chains_num = num_of_ant(priv->cfg->valid_tx_ant);
  565. priv->hw_params.rx_chains_num = num_of_ant(priv->cfg->valid_rx_ant);
  566. priv->hw_params.valid_tx_ant = priv->cfg->valid_tx_ant;
  567. priv->hw_params.valid_rx_ant = priv->cfg->valid_rx_ant;
  568. if (priv->cfg->ops->lib->temp_ops.set_ct_kill)
  569. priv->cfg->ops->lib->temp_ops.set_ct_kill(priv);
  570. priv->hw_params.sens = &iwl4965_sensitivity;
  571. priv->hw_params.beacon_time_tsf_bits = IWLAGN_EXT_BEACON_TIME_POS;
  572. return 0;
  573. }
  574. static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
  575. {
  576. s32 sign = 1;
  577. if (num < 0) {
  578. sign = -sign;
  579. num = -num;
  580. }
  581. if (denom < 0) {
  582. sign = -sign;
  583. denom = -denom;
  584. }
  585. *res = 1;
  586. *res = ((num * 2 + denom) / (denom * 2)) * sign;
  587. return 1;
  588. }
  589. /**
  590. * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
  591. *
  592. * Determines power supply voltage compensation for txpower calculations.
  593. * Returns number of 1/2-dB steps to subtract from gain table index,
  594. * to compensate for difference between power supply voltage during
  595. * factory measurements, vs. current power supply voltage.
  596. *
  597. * Voltage indication is higher for lower voltage.
  598. * Lower voltage requires more gain (lower gain table index).
  599. */
  600. static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
  601. s32 current_voltage)
  602. {
  603. s32 comp = 0;
  604. if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
  605. (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
  606. return 0;
  607. iwl4965_math_div_round(current_voltage - eeprom_voltage,
  608. TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
  609. if (current_voltage > eeprom_voltage)
  610. comp *= 2;
  611. if ((comp < -2) || (comp > 2))
  612. comp = 0;
  613. return comp;
  614. }
  615. static s32 iwl4965_get_tx_atten_grp(u16 channel)
  616. {
  617. if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
  618. channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
  619. return CALIB_CH_GROUP_5;
  620. if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
  621. channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
  622. return CALIB_CH_GROUP_1;
  623. if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
  624. channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
  625. return CALIB_CH_GROUP_2;
  626. if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
  627. channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
  628. return CALIB_CH_GROUP_3;
  629. if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
  630. channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
  631. return CALIB_CH_GROUP_4;
  632. return -1;
  633. }
  634. static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
  635. {
  636. s32 b = -1;
  637. for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
  638. if (priv->calib_info->band_info[b].ch_from == 0)
  639. continue;
  640. if ((channel >= priv->calib_info->band_info[b].ch_from)
  641. && (channel <= priv->calib_info->band_info[b].ch_to))
  642. break;
  643. }
  644. return b;
  645. }
  646. static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
  647. {
  648. s32 val;
  649. if (x2 == x1)
  650. return y1;
  651. else {
  652. iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
  653. return val + y2;
  654. }
  655. }
  656. /**
  657. * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
  658. *
  659. * Interpolates factory measurements from the two sample channels within a
  660. * sub-band, to apply to channel of interest. Interpolation is proportional to
  661. * differences in channel frequencies, which is proportional to differences
  662. * in channel number.
  663. */
  664. static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
  665. struct iwl_eeprom_calib_ch_info *chan_info)
  666. {
  667. s32 s = -1;
  668. u32 c;
  669. u32 m;
  670. const struct iwl_eeprom_calib_measure *m1;
  671. const struct iwl_eeprom_calib_measure *m2;
  672. struct iwl_eeprom_calib_measure *omeas;
  673. u32 ch_i1;
  674. u32 ch_i2;
  675. s = iwl4965_get_sub_band(priv, channel);
  676. if (s >= EEPROM_TX_POWER_BANDS) {
  677. IWL_ERR(priv, "Tx Power can not find channel %d\n", channel);
  678. return -1;
  679. }
  680. ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
  681. ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
  682. chan_info->ch_num = (u8) channel;
  683. IWL_DEBUG_TXPOWER(priv, "channel %d subband %d factory cal ch %d & %d\n",
  684. channel, s, ch_i1, ch_i2);
  685. for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
  686. for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
  687. m1 = &(priv->calib_info->band_info[s].ch1.
  688. measurements[c][m]);
  689. m2 = &(priv->calib_info->band_info[s].ch2.
  690. measurements[c][m]);
  691. omeas = &(chan_info->measurements[c][m]);
  692. omeas->actual_pow =
  693. (u8) iwl4965_interpolate_value(channel, ch_i1,
  694. m1->actual_pow,
  695. ch_i2,
  696. m2->actual_pow);
  697. omeas->gain_idx =
  698. (u8) iwl4965_interpolate_value(channel, ch_i1,
  699. m1->gain_idx, ch_i2,
  700. m2->gain_idx);
  701. omeas->temperature =
  702. (u8) iwl4965_interpolate_value(channel, ch_i1,
  703. m1->temperature,
  704. ch_i2,
  705. m2->temperature);
  706. omeas->pa_det =
  707. (s8) iwl4965_interpolate_value(channel, ch_i1,
  708. m1->pa_det, ch_i2,
  709. m2->pa_det);
  710. IWL_DEBUG_TXPOWER(priv,
  711. "chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
  712. m1->actual_pow, m2->actual_pow, omeas->actual_pow);
  713. IWL_DEBUG_TXPOWER(priv,
  714. "chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
  715. m1->gain_idx, m2->gain_idx, omeas->gain_idx);
  716. IWL_DEBUG_TXPOWER(priv,
  717. "chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
  718. m1->pa_det, m2->pa_det, omeas->pa_det);
  719. IWL_DEBUG_TXPOWER(priv,
  720. "chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
  721. m1->temperature, m2->temperature,
  722. omeas->temperature);
  723. }
  724. }
  725. return 0;
  726. }
  727. /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
  728. * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
  729. static s32 back_off_table[] = {
  730. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
  731. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
  732. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
  733. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
  734. 10 /* CCK */
  735. };
  736. /* Thermal compensation values for txpower for various frequency ranges ...
  737. * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
  738. static struct iwl4965_txpower_comp_entry {
  739. s32 degrees_per_05db_a;
  740. s32 degrees_per_05db_a_denom;
  741. } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
  742. {9, 2}, /* group 0 5.2, ch 34-43 */
  743. {4, 1}, /* group 1 5.2, ch 44-70 */
  744. {4, 1}, /* group 2 5.2, ch 71-124 */
  745. {4, 1}, /* group 3 5.2, ch 125-200 */
  746. {3, 1} /* group 4 2.4, ch all */
  747. };
  748. static s32 get_min_power_index(s32 rate_power_index, u32 band)
  749. {
  750. if (!band) {
  751. if ((rate_power_index & 7) <= 4)
  752. return MIN_TX_GAIN_INDEX_52GHZ_EXT;
  753. }
  754. return MIN_TX_GAIN_INDEX;
  755. }
  756. struct gain_entry {
  757. u8 dsp;
  758. u8 radio;
  759. };
  760. static const struct gain_entry gain_table[2][108] = {
  761. /* 5.2GHz power gain index table */
  762. {
  763. {123, 0x3F}, /* highest txpower */
  764. {117, 0x3F},
  765. {110, 0x3F},
  766. {104, 0x3F},
  767. {98, 0x3F},
  768. {110, 0x3E},
  769. {104, 0x3E},
  770. {98, 0x3E},
  771. {110, 0x3D},
  772. {104, 0x3D},
  773. {98, 0x3D},
  774. {110, 0x3C},
  775. {104, 0x3C},
  776. {98, 0x3C},
  777. {110, 0x3B},
  778. {104, 0x3B},
  779. {98, 0x3B},
  780. {110, 0x3A},
  781. {104, 0x3A},
  782. {98, 0x3A},
  783. {110, 0x39},
  784. {104, 0x39},
  785. {98, 0x39},
  786. {110, 0x38},
  787. {104, 0x38},
  788. {98, 0x38},
  789. {110, 0x37},
  790. {104, 0x37},
  791. {98, 0x37},
  792. {110, 0x36},
  793. {104, 0x36},
  794. {98, 0x36},
  795. {110, 0x35},
  796. {104, 0x35},
  797. {98, 0x35},
  798. {110, 0x34},
  799. {104, 0x34},
  800. {98, 0x34},
  801. {110, 0x33},
  802. {104, 0x33},
  803. {98, 0x33},
  804. {110, 0x32},
  805. {104, 0x32},
  806. {98, 0x32},
  807. {110, 0x31},
  808. {104, 0x31},
  809. {98, 0x31},
  810. {110, 0x30},
  811. {104, 0x30},
  812. {98, 0x30},
  813. {110, 0x25},
  814. {104, 0x25},
  815. {98, 0x25},
  816. {110, 0x24},
  817. {104, 0x24},
  818. {98, 0x24},
  819. {110, 0x23},
  820. {104, 0x23},
  821. {98, 0x23},
  822. {110, 0x22},
  823. {104, 0x18},
  824. {98, 0x18},
  825. {110, 0x17},
  826. {104, 0x17},
  827. {98, 0x17},
  828. {110, 0x16},
  829. {104, 0x16},
  830. {98, 0x16},
  831. {110, 0x15},
  832. {104, 0x15},
  833. {98, 0x15},
  834. {110, 0x14},
  835. {104, 0x14},
  836. {98, 0x14},
  837. {110, 0x13},
  838. {104, 0x13},
  839. {98, 0x13},
  840. {110, 0x12},
  841. {104, 0x08},
  842. {98, 0x08},
  843. {110, 0x07},
  844. {104, 0x07},
  845. {98, 0x07},
  846. {110, 0x06},
  847. {104, 0x06},
  848. {98, 0x06},
  849. {110, 0x05},
  850. {104, 0x05},
  851. {98, 0x05},
  852. {110, 0x04},
  853. {104, 0x04},
  854. {98, 0x04},
  855. {110, 0x03},
  856. {104, 0x03},
  857. {98, 0x03},
  858. {110, 0x02},
  859. {104, 0x02},
  860. {98, 0x02},
  861. {110, 0x01},
  862. {104, 0x01},
  863. {98, 0x01},
  864. {110, 0x00},
  865. {104, 0x00},
  866. {98, 0x00},
  867. {93, 0x00},
  868. {88, 0x00},
  869. {83, 0x00},
  870. {78, 0x00},
  871. },
  872. /* 2.4GHz power gain index table */
  873. {
  874. {110, 0x3f}, /* highest txpower */
  875. {104, 0x3f},
  876. {98, 0x3f},
  877. {110, 0x3e},
  878. {104, 0x3e},
  879. {98, 0x3e},
  880. {110, 0x3d},
  881. {104, 0x3d},
  882. {98, 0x3d},
  883. {110, 0x3c},
  884. {104, 0x3c},
  885. {98, 0x3c},
  886. {110, 0x3b},
  887. {104, 0x3b},
  888. {98, 0x3b},
  889. {110, 0x3a},
  890. {104, 0x3a},
  891. {98, 0x3a},
  892. {110, 0x39},
  893. {104, 0x39},
  894. {98, 0x39},
  895. {110, 0x38},
  896. {104, 0x38},
  897. {98, 0x38},
  898. {110, 0x37},
  899. {104, 0x37},
  900. {98, 0x37},
  901. {110, 0x36},
  902. {104, 0x36},
  903. {98, 0x36},
  904. {110, 0x35},
  905. {104, 0x35},
  906. {98, 0x35},
  907. {110, 0x34},
  908. {104, 0x34},
  909. {98, 0x34},
  910. {110, 0x33},
  911. {104, 0x33},
  912. {98, 0x33},
  913. {110, 0x32},
  914. {104, 0x32},
  915. {98, 0x32},
  916. {110, 0x31},
  917. {104, 0x31},
  918. {98, 0x31},
  919. {110, 0x30},
  920. {104, 0x30},
  921. {98, 0x30},
  922. {110, 0x6},
  923. {104, 0x6},
  924. {98, 0x6},
  925. {110, 0x5},
  926. {104, 0x5},
  927. {98, 0x5},
  928. {110, 0x4},
  929. {104, 0x4},
  930. {98, 0x4},
  931. {110, 0x3},
  932. {104, 0x3},
  933. {98, 0x3},
  934. {110, 0x2},
  935. {104, 0x2},
  936. {98, 0x2},
  937. {110, 0x1},
  938. {104, 0x1},
  939. {98, 0x1},
  940. {110, 0x0},
  941. {104, 0x0},
  942. {98, 0x0},
  943. {97, 0},
  944. {96, 0},
  945. {95, 0},
  946. {94, 0},
  947. {93, 0},
  948. {92, 0},
  949. {91, 0},
  950. {90, 0},
  951. {89, 0},
  952. {88, 0},
  953. {87, 0},
  954. {86, 0},
  955. {85, 0},
  956. {84, 0},
  957. {83, 0},
  958. {82, 0},
  959. {81, 0},
  960. {80, 0},
  961. {79, 0},
  962. {78, 0},
  963. {77, 0},
  964. {76, 0},
  965. {75, 0},
  966. {74, 0},
  967. {73, 0},
  968. {72, 0},
  969. {71, 0},
  970. {70, 0},
  971. {69, 0},
  972. {68, 0},
  973. {67, 0},
  974. {66, 0},
  975. {65, 0},
  976. {64, 0},
  977. {63, 0},
  978. {62, 0},
  979. {61, 0},
  980. {60, 0},
  981. {59, 0},
  982. }
  983. };
  984. static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
  985. u8 is_ht40, u8 ctrl_chan_high,
  986. struct iwl4965_tx_power_db *tx_power_tbl)
  987. {
  988. u8 saturation_power;
  989. s32 target_power;
  990. s32 user_target_power;
  991. s32 power_limit;
  992. s32 current_temp;
  993. s32 reg_limit;
  994. s32 current_regulatory;
  995. s32 txatten_grp = CALIB_CH_GROUP_MAX;
  996. int i;
  997. int c;
  998. const struct iwl_channel_info *ch_info = NULL;
  999. struct iwl_eeprom_calib_ch_info ch_eeprom_info;
  1000. const struct iwl_eeprom_calib_measure *measurement;
  1001. s16 voltage;
  1002. s32 init_voltage;
  1003. s32 voltage_compensation;
  1004. s32 degrees_per_05db_num;
  1005. s32 degrees_per_05db_denom;
  1006. s32 factory_temp;
  1007. s32 temperature_comp[2];
  1008. s32 factory_gain_index[2];
  1009. s32 factory_actual_pwr[2];
  1010. s32 power_index;
  1011. /* tx_power_user_lmt is in dBm, convert to half-dBm (half-dB units
  1012. * are used for indexing into txpower table) */
  1013. user_target_power = 2 * priv->tx_power_user_lmt;
  1014. /* Get current (RXON) channel, band, width */
  1015. IWL_DEBUG_TXPOWER(priv, "chan %d band %d is_ht40 %d\n", channel, band,
  1016. is_ht40);
  1017. ch_info = iwl_get_channel_info(priv, priv->band, channel);
  1018. if (!is_channel_valid(ch_info))
  1019. return -EINVAL;
  1020. /* get txatten group, used to select 1) thermal txpower adjustment
  1021. * and 2) mimo txpower balance between Tx chains. */
  1022. txatten_grp = iwl4965_get_tx_atten_grp(channel);
  1023. if (txatten_grp < 0) {
  1024. IWL_ERR(priv, "Can't find txatten group for channel %d.\n",
  1025. channel);
  1026. return -EINVAL;
  1027. }
  1028. IWL_DEBUG_TXPOWER(priv, "channel %d belongs to txatten group %d\n",
  1029. channel, txatten_grp);
  1030. if (is_ht40) {
  1031. if (ctrl_chan_high)
  1032. channel -= 2;
  1033. else
  1034. channel += 2;
  1035. }
  1036. /* hardware txpower limits ...
  1037. * saturation (clipping distortion) txpowers are in half-dBm */
  1038. if (band)
  1039. saturation_power = priv->calib_info->saturation_power24;
  1040. else
  1041. saturation_power = priv->calib_info->saturation_power52;
  1042. if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
  1043. saturation_power > IWL_TX_POWER_SATURATION_MAX) {
  1044. if (band)
  1045. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
  1046. else
  1047. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
  1048. }
  1049. /* regulatory txpower limits ... reg_limit values are in half-dBm,
  1050. * max_power_avg values are in dBm, convert * 2 */
  1051. if (is_ht40)
  1052. reg_limit = ch_info->ht40_max_power_avg * 2;
  1053. else
  1054. reg_limit = ch_info->max_power_avg * 2;
  1055. if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
  1056. (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
  1057. if (band)
  1058. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
  1059. else
  1060. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
  1061. }
  1062. /* Interpolate txpower calibration values for this channel,
  1063. * based on factory calibration tests on spaced channels. */
  1064. iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
  1065. /* calculate tx gain adjustment based on power supply voltage */
  1066. voltage = le16_to_cpu(priv->calib_info->voltage);
  1067. init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
  1068. voltage_compensation =
  1069. iwl4965_get_voltage_compensation(voltage, init_voltage);
  1070. IWL_DEBUG_TXPOWER(priv, "curr volt %d eeprom volt %d volt comp %d\n",
  1071. init_voltage,
  1072. voltage, voltage_compensation);
  1073. /* get current temperature (Celsius) */
  1074. current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
  1075. current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
  1076. current_temp = KELVIN_TO_CELSIUS(current_temp);
  1077. /* select thermal txpower adjustment params, based on channel group
  1078. * (same frequency group used for mimo txatten adjustment) */
  1079. degrees_per_05db_num =
  1080. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
  1081. degrees_per_05db_denom =
  1082. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
  1083. /* get per-chain txpower values from factory measurements */
  1084. for (c = 0; c < 2; c++) {
  1085. measurement = &ch_eeprom_info.measurements[c][1];
  1086. /* txgain adjustment (in half-dB steps) based on difference
  1087. * between factory and current temperature */
  1088. factory_temp = measurement->temperature;
  1089. iwl4965_math_div_round((current_temp - factory_temp) *
  1090. degrees_per_05db_denom,
  1091. degrees_per_05db_num,
  1092. &temperature_comp[c]);
  1093. factory_gain_index[c] = measurement->gain_idx;
  1094. factory_actual_pwr[c] = measurement->actual_pow;
  1095. IWL_DEBUG_TXPOWER(priv, "chain = %d\n", c);
  1096. IWL_DEBUG_TXPOWER(priv, "fctry tmp %d, "
  1097. "curr tmp %d, comp %d steps\n",
  1098. factory_temp, current_temp,
  1099. temperature_comp[c]);
  1100. IWL_DEBUG_TXPOWER(priv, "fctry idx %d, fctry pwr %d\n",
  1101. factory_gain_index[c],
  1102. factory_actual_pwr[c]);
  1103. }
  1104. /* for each of 33 bit-rates (including 1 for CCK) */
  1105. for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
  1106. u8 is_mimo_rate;
  1107. union iwl4965_tx_power_dual_stream tx_power;
  1108. /* for mimo, reduce each chain's txpower by half
  1109. * (3dB, 6 steps), so total output power is regulatory
  1110. * compliant. */
  1111. if (i & 0x8) {
  1112. current_regulatory = reg_limit -
  1113. IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
  1114. is_mimo_rate = 1;
  1115. } else {
  1116. current_regulatory = reg_limit;
  1117. is_mimo_rate = 0;
  1118. }
  1119. /* find txpower limit, either hardware or regulatory */
  1120. power_limit = saturation_power - back_off_table[i];
  1121. if (power_limit > current_regulatory)
  1122. power_limit = current_regulatory;
  1123. /* reduce user's txpower request if necessary
  1124. * for this rate on this channel */
  1125. target_power = user_target_power;
  1126. if (target_power > power_limit)
  1127. target_power = power_limit;
  1128. IWL_DEBUG_TXPOWER(priv, "rate %d sat %d reg %d usr %d tgt %d\n",
  1129. i, saturation_power - back_off_table[i],
  1130. current_regulatory, user_target_power,
  1131. target_power);
  1132. /* for each of 2 Tx chains (radio transmitters) */
  1133. for (c = 0; c < 2; c++) {
  1134. s32 atten_value;
  1135. if (is_mimo_rate)
  1136. atten_value =
  1137. (s32)le32_to_cpu(priv->card_alive_init.
  1138. tx_atten[txatten_grp][c]);
  1139. else
  1140. atten_value = 0;
  1141. /* calculate index; higher index means lower txpower */
  1142. power_index = (u8) (factory_gain_index[c] -
  1143. (target_power -
  1144. factory_actual_pwr[c]) -
  1145. temperature_comp[c] -
  1146. voltage_compensation +
  1147. atten_value);
  1148. /* IWL_DEBUG_TXPOWER(priv, "calculated txpower index %d\n",
  1149. power_index); */
  1150. if (power_index < get_min_power_index(i, band))
  1151. power_index = get_min_power_index(i, band);
  1152. /* adjust 5 GHz index to support negative indexes */
  1153. if (!band)
  1154. power_index += 9;
  1155. /* CCK, rate 32, reduce txpower for CCK */
  1156. if (i == POWER_TABLE_CCK_ENTRY)
  1157. power_index +=
  1158. IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
  1159. /* stay within the table! */
  1160. if (power_index > 107) {
  1161. IWL_WARN(priv, "txpower index %d > 107\n",
  1162. power_index);
  1163. power_index = 107;
  1164. }
  1165. if (power_index < 0) {
  1166. IWL_WARN(priv, "txpower index %d < 0\n",
  1167. power_index);
  1168. power_index = 0;
  1169. }
  1170. /* fill txpower command for this rate/chain */
  1171. tx_power.s.radio_tx_gain[c] =
  1172. gain_table[band][power_index].radio;
  1173. tx_power.s.dsp_predis_atten[c] =
  1174. gain_table[band][power_index].dsp;
  1175. IWL_DEBUG_TXPOWER(priv, "chain %d mimo %d index %d "
  1176. "gain 0x%02x dsp %d\n",
  1177. c, atten_value, power_index,
  1178. tx_power.s.radio_tx_gain[c],
  1179. tx_power.s.dsp_predis_atten[c]);
  1180. } /* for each chain */
  1181. tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
  1182. } /* for each rate */
  1183. return 0;
  1184. }
  1185. /**
  1186. * iwl4965_send_tx_power - Configure the TXPOWER level user limit
  1187. *
  1188. * Uses the active RXON for channel, band, and characteristics (ht40, high)
  1189. * The power limit is taken from priv->tx_power_user_lmt.
  1190. */
  1191. static int iwl4965_send_tx_power(struct iwl_priv *priv)
  1192. {
  1193. struct iwl4965_txpowertable_cmd cmd = { 0 };
  1194. int ret;
  1195. u8 band = 0;
  1196. bool is_ht40 = false;
  1197. u8 ctrl_chan_high = 0;
  1198. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1199. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1200. /* If this gets hit a lot, switch it to a BUG() and catch
  1201. * the stack trace to find out who is calling this during
  1202. * a scan. */
  1203. IWL_WARN(priv, "TX Power requested while scanning!\n");
  1204. return -EAGAIN;
  1205. }
  1206. band = priv->band == IEEE80211_BAND_2GHZ;
  1207. is_ht40 = is_ht40_channel(ctx->active.flags);
  1208. if (is_ht40 && (ctx->active.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  1209. ctrl_chan_high = 1;
  1210. cmd.band = band;
  1211. cmd.channel = ctx->active.channel;
  1212. ret = iwl4965_fill_txpower_tbl(priv, band,
  1213. le16_to_cpu(ctx->active.channel),
  1214. is_ht40, ctrl_chan_high, &cmd.tx_power);
  1215. if (ret)
  1216. goto out;
  1217. ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
  1218. out:
  1219. return ret;
  1220. }
  1221. static int iwl4965_send_rxon_assoc(struct iwl_priv *priv,
  1222. struct iwl_rxon_context *ctx)
  1223. {
  1224. int ret = 0;
  1225. struct iwl4965_rxon_assoc_cmd rxon_assoc;
  1226. const struct iwl_rxon_cmd *rxon1 = &ctx->staging;
  1227. const struct iwl_rxon_cmd *rxon2 = &ctx->active;
  1228. if ((rxon1->flags == rxon2->flags) &&
  1229. (rxon1->filter_flags == rxon2->filter_flags) &&
  1230. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  1231. (rxon1->ofdm_ht_single_stream_basic_rates ==
  1232. rxon2->ofdm_ht_single_stream_basic_rates) &&
  1233. (rxon1->ofdm_ht_dual_stream_basic_rates ==
  1234. rxon2->ofdm_ht_dual_stream_basic_rates) &&
  1235. (rxon1->rx_chain == rxon2->rx_chain) &&
  1236. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  1237. IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
  1238. return 0;
  1239. }
  1240. rxon_assoc.flags = ctx->staging.flags;
  1241. rxon_assoc.filter_flags = ctx->staging.filter_flags;
  1242. rxon_assoc.ofdm_basic_rates = ctx->staging.ofdm_basic_rates;
  1243. rxon_assoc.cck_basic_rates = ctx->staging.cck_basic_rates;
  1244. rxon_assoc.reserved = 0;
  1245. rxon_assoc.ofdm_ht_single_stream_basic_rates =
  1246. ctx->staging.ofdm_ht_single_stream_basic_rates;
  1247. rxon_assoc.ofdm_ht_dual_stream_basic_rates =
  1248. ctx->staging.ofdm_ht_dual_stream_basic_rates;
  1249. rxon_assoc.rx_chain_select_flags = ctx->staging.rx_chain;
  1250. ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
  1251. sizeof(rxon_assoc), &rxon_assoc, NULL);
  1252. if (ret)
  1253. return ret;
  1254. return ret;
  1255. }
  1256. static int iwl4965_hw_channel_switch(struct iwl_priv *priv,
  1257. struct ieee80211_channel_switch *ch_switch)
  1258. {
  1259. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1260. int rc;
  1261. u8 band = 0;
  1262. bool is_ht40 = false;
  1263. u8 ctrl_chan_high = 0;
  1264. struct iwl4965_channel_switch_cmd cmd;
  1265. const struct iwl_channel_info *ch_info;
  1266. u32 switch_time_in_usec, ucode_switch_time;
  1267. u16 ch;
  1268. u32 tsf_low;
  1269. u8 switch_count;
  1270. u16 beacon_interval = le16_to_cpu(ctx->timing.beacon_interval);
  1271. struct ieee80211_vif *vif = ctx->vif;
  1272. band = priv->band == IEEE80211_BAND_2GHZ;
  1273. is_ht40 = is_ht40_channel(ctx->staging.flags);
  1274. if (is_ht40 &&
  1275. (ctx->staging.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  1276. ctrl_chan_high = 1;
  1277. cmd.band = band;
  1278. cmd.expect_beacon = 0;
  1279. ch = ch_switch->channel->hw_value;
  1280. cmd.channel = cpu_to_le16(ch);
  1281. cmd.rxon_flags = ctx->staging.flags;
  1282. cmd.rxon_filter_flags = ctx->staging.filter_flags;
  1283. switch_count = ch_switch->count;
  1284. tsf_low = ch_switch->timestamp & 0x0ffffffff;
  1285. /*
  1286. * calculate the ucode channel switch time
  1287. * adding TSF as one of the factor for when to switch
  1288. */
  1289. if ((priv->ucode_beacon_time > tsf_low) && beacon_interval) {
  1290. if (switch_count > ((priv->ucode_beacon_time - tsf_low) /
  1291. beacon_interval)) {
  1292. switch_count -= (priv->ucode_beacon_time -
  1293. tsf_low) / beacon_interval;
  1294. } else
  1295. switch_count = 0;
  1296. }
  1297. if (switch_count <= 1)
  1298. cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
  1299. else {
  1300. switch_time_in_usec =
  1301. vif->bss_conf.beacon_int * switch_count * TIME_UNIT;
  1302. ucode_switch_time = iwl_usecs_to_beacons(priv,
  1303. switch_time_in_usec,
  1304. beacon_interval);
  1305. cmd.switch_time = iwl_add_beacon_time(priv,
  1306. priv->ucode_beacon_time,
  1307. ucode_switch_time,
  1308. beacon_interval);
  1309. }
  1310. IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
  1311. cmd.switch_time);
  1312. ch_info = iwl_get_channel_info(priv, priv->band, ch);
  1313. if (ch_info)
  1314. cmd.expect_beacon = is_channel_radar(ch_info);
  1315. else {
  1316. IWL_ERR(priv, "invalid channel switch from %u to %u\n",
  1317. ctx->active.channel, ch);
  1318. return -EFAULT;
  1319. }
  1320. rc = iwl4965_fill_txpower_tbl(priv, band, ch, is_ht40,
  1321. ctrl_chan_high, &cmd.tx_power);
  1322. if (rc) {
  1323. IWL_DEBUG_11H(priv, "error:%d fill txpower_tbl\n", rc);
  1324. return rc;
  1325. }
  1326. priv->switch_rxon.channel = cmd.channel;
  1327. priv->switch_rxon.switch_in_progress = true;
  1328. return iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
  1329. }
  1330. /**
  1331. * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  1332. */
  1333. static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
  1334. struct iwl_tx_queue *txq,
  1335. u16 byte_cnt)
  1336. {
  1337. struct iwl4965_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr;
  1338. int txq_id = txq->q.id;
  1339. int write_ptr = txq->q.write_ptr;
  1340. int len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  1341. __le16 bc_ent;
  1342. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  1343. bc_ent = cpu_to_le16(len & 0xFFF);
  1344. /* Set up byte count within first 256 entries */
  1345. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  1346. /* If within first 64 entries, duplicate at end */
  1347. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  1348. scd_bc_tbl[txq_id].
  1349. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  1350. }
  1351. /**
  1352. * sign_extend - Sign extend a value using specified bit as sign-bit
  1353. *
  1354. * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
  1355. * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
  1356. *
  1357. * @param oper value to sign extend
  1358. * @param index 0 based bit index (0<=index<32) to sign bit
  1359. */
  1360. static s32 sign_extend(u32 oper, int index)
  1361. {
  1362. u8 shift = 31 - index;
  1363. return (s32)(oper << shift) >> shift;
  1364. }
  1365. /**
  1366. * iwl4965_hw_get_temperature - return the calibrated temperature (in Kelvin)
  1367. * @statistics: Provides the temperature reading from the uCode
  1368. *
  1369. * A return of <0 indicates bogus data in the statistics
  1370. */
  1371. static int iwl4965_hw_get_temperature(struct iwl_priv *priv)
  1372. {
  1373. s32 temperature;
  1374. s32 vt;
  1375. s32 R1, R2, R3;
  1376. u32 R4;
  1377. if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
  1378. (priv->_agn.statistics.flag &
  1379. STATISTICS_REPLY_FLG_HT40_MODE_MSK)) {
  1380. IWL_DEBUG_TEMP(priv, "Running HT40 temperature calibration\n");
  1381. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  1382. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  1383. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  1384. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
  1385. } else {
  1386. IWL_DEBUG_TEMP(priv, "Running temperature calibration\n");
  1387. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  1388. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  1389. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  1390. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
  1391. }
  1392. /*
  1393. * Temperature is only 23 bits, so sign extend out to 32.
  1394. *
  1395. * NOTE If we haven't received a statistics notification yet
  1396. * with an updated temperature, use R4 provided to us in the
  1397. * "initialize" ALIVE response.
  1398. */
  1399. if (!test_bit(STATUS_TEMPERATURE, &priv->status))
  1400. vt = sign_extend(R4, 23);
  1401. else
  1402. vt = sign_extend(le32_to_cpu(priv->_agn.statistics.
  1403. general.common.temperature), 23);
  1404. IWL_DEBUG_TEMP(priv, "Calib values R[1-3]: %d %d %d R4: %d\n", R1, R2, R3, vt);
  1405. if (R3 == R1) {
  1406. IWL_ERR(priv, "Calibration conflict R1 == R3\n");
  1407. return -1;
  1408. }
  1409. /* Calculate temperature in degrees Kelvin, adjust by 97%.
  1410. * Add offset to center the adjustment around 0 degrees Centigrade. */
  1411. temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
  1412. temperature /= (R3 - R1);
  1413. temperature = (temperature * 97) / 100 + TEMPERATURE_CALIB_KELVIN_OFFSET;
  1414. IWL_DEBUG_TEMP(priv, "Calibrated temperature: %dK, %dC\n",
  1415. temperature, KELVIN_TO_CELSIUS(temperature));
  1416. return temperature;
  1417. }
  1418. /* Adjust Txpower only if temperature variance is greater than threshold. */
  1419. #define IWL_TEMPERATURE_THRESHOLD 3
  1420. /**
  1421. * iwl4965_is_temp_calib_needed - determines if new calibration is needed
  1422. *
  1423. * If the temperature changed has changed sufficiently, then a recalibration
  1424. * is needed.
  1425. *
  1426. * Assumes caller will replace priv->last_temperature once calibration
  1427. * executed.
  1428. */
  1429. static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
  1430. {
  1431. int temp_diff;
  1432. if (!test_bit(STATUS_STATISTICS, &priv->status)) {
  1433. IWL_DEBUG_TEMP(priv, "Temperature not updated -- no statistics.\n");
  1434. return 0;
  1435. }
  1436. temp_diff = priv->temperature - priv->last_temperature;
  1437. /* get absolute value */
  1438. if (temp_diff < 0) {
  1439. IWL_DEBUG_POWER(priv, "Getting cooler, delta %d\n", temp_diff);
  1440. temp_diff = -temp_diff;
  1441. } else if (temp_diff == 0)
  1442. IWL_DEBUG_POWER(priv, "Temperature unchanged\n");
  1443. else
  1444. IWL_DEBUG_POWER(priv, "Getting warmer, delta %d\n", temp_diff);
  1445. if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
  1446. IWL_DEBUG_POWER(priv, " => thermal txpower calib not needed\n");
  1447. return 0;
  1448. }
  1449. IWL_DEBUG_POWER(priv, " => thermal txpower calib needed\n");
  1450. return 1;
  1451. }
  1452. static void iwl4965_temperature_calib(struct iwl_priv *priv)
  1453. {
  1454. s32 temp;
  1455. temp = iwl4965_hw_get_temperature(priv);
  1456. if (temp < 0)
  1457. return;
  1458. if (priv->temperature != temp) {
  1459. if (priv->temperature)
  1460. IWL_DEBUG_TEMP(priv, "Temperature changed "
  1461. "from %dC to %dC\n",
  1462. KELVIN_TO_CELSIUS(priv->temperature),
  1463. KELVIN_TO_CELSIUS(temp));
  1464. else
  1465. IWL_DEBUG_TEMP(priv, "Temperature "
  1466. "initialized to %dC\n",
  1467. KELVIN_TO_CELSIUS(temp));
  1468. }
  1469. priv->temperature = temp;
  1470. iwl_tt_handler(priv);
  1471. set_bit(STATUS_TEMPERATURE, &priv->status);
  1472. if (!priv->disable_tx_power_cal &&
  1473. unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  1474. iwl4965_is_temp_calib_needed(priv))
  1475. queue_work(priv->workqueue, &priv->txpower_work);
  1476. }
  1477. /**
  1478. * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  1479. */
  1480. static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
  1481. u16 txq_id)
  1482. {
  1483. /* Simply stop the queue, but don't change any configuration;
  1484. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  1485. iwl_write_prph(priv,
  1486. IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
  1487. (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  1488. (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  1489. }
  1490. /**
  1491. * txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE
  1492. * priv->lock must be held by the caller
  1493. */
  1494. static int iwl4965_txq_agg_disable(struct iwl_priv *priv, u16 txq_id,
  1495. u16 ssn_idx, u8 tx_fifo)
  1496. {
  1497. if ((IWL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1498. (IWL49_FIRST_AMPDU_QUEUE +
  1499. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  1500. IWL_WARN(priv,
  1501. "queue number out of range: %d, must be %d to %d\n",
  1502. txq_id, IWL49_FIRST_AMPDU_QUEUE,
  1503. IWL49_FIRST_AMPDU_QUEUE +
  1504. priv->cfg->base_params->num_of_ampdu_queues - 1);
  1505. return -EINVAL;
  1506. }
  1507. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  1508. iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1509. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1510. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1511. /* supposes that ssn_idx is valid (!= 0xFFF) */
  1512. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  1513. iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1514. iwl_txq_ctx_deactivate(priv, txq_id);
  1515. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  1516. return 0;
  1517. }
  1518. /**
  1519. * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  1520. */
  1521. static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
  1522. u16 txq_id)
  1523. {
  1524. u32 tbl_dw_addr;
  1525. u32 tbl_dw;
  1526. u16 scd_q2ratid;
  1527. scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  1528. tbl_dw_addr = priv->scd_base_addr +
  1529. IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  1530. tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
  1531. if (txq_id & 0x1)
  1532. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  1533. else
  1534. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  1535. iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  1536. return 0;
  1537. }
  1538. /**
  1539. * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  1540. *
  1541. * NOTE: txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE,
  1542. * i.e. it must be one of the higher queues used for aggregation
  1543. */
  1544. static int iwl4965_txq_agg_enable(struct iwl_priv *priv, int txq_id,
  1545. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  1546. {
  1547. unsigned long flags;
  1548. u16 ra_tid;
  1549. int ret;
  1550. if ((IWL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1551. (IWL49_FIRST_AMPDU_QUEUE +
  1552. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  1553. IWL_WARN(priv,
  1554. "queue number out of range: %d, must be %d to %d\n",
  1555. txq_id, IWL49_FIRST_AMPDU_QUEUE,
  1556. IWL49_FIRST_AMPDU_QUEUE +
  1557. priv->cfg->base_params->num_of_ampdu_queues - 1);
  1558. return -EINVAL;
  1559. }
  1560. ra_tid = BUILD_RAxTID(sta_id, tid);
  1561. /* Modify device's station table to Tx this TID */
  1562. ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
  1563. if (ret)
  1564. return ret;
  1565. spin_lock_irqsave(&priv->lock, flags);
  1566. /* Stop this Tx queue before configuring it */
  1567. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  1568. /* Map receiver-address / traffic-ID to this queue */
  1569. iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  1570. /* Set this queue as a chain-building queue */
  1571. iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1572. /* Place first TFD at index corresponding to start sequence number.
  1573. * Assumes that ssn_idx is valid (!= 0xFFF) */
  1574. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1575. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1576. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  1577. /* Set up Tx window size and frame limit for this queue */
  1578. iwl_write_targ_mem(priv,
  1579. priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  1580. (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  1581. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1582. iwl_write_targ_mem(priv, priv->scd_base_addr +
  1583. IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1584. (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  1585. & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1586. iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1587. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  1588. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  1589. spin_unlock_irqrestore(&priv->lock, flags);
  1590. return 0;
  1591. }
  1592. static u16 iwl4965_get_hcmd_size(u8 cmd_id, u16 len)
  1593. {
  1594. switch (cmd_id) {
  1595. case REPLY_RXON:
  1596. return (u16) sizeof(struct iwl4965_rxon_cmd);
  1597. default:
  1598. return len;
  1599. }
  1600. }
  1601. static u16 iwl4965_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
  1602. {
  1603. struct iwl4965_addsta_cmd *addsta = (struct iwl4965_addsta_cmd *)data;
  1604. addsta->mode = cmd->mode;
  1605. memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
  1606. memcpy(&addsta->key, &cmd->key, sizeof(struct iwl4965_keyinfo));
  1607. addsta->station_flags = cmd->station_flags;
  1608. addsta->station_flags_msk = cmd->station_flags_msk;
  1609. addsta->tid_disable_tx = cmd->tid_disable_tx;
  1610. addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
  1611. addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
  1612. addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
  1613. addsta->sleep_tx_count = cmd->sleep_tx_count;
  1614. addsta->reserved1 = cpu_to_le16(0);
  1615. addsta->reserved2 = cpu_to_le16(0);
  1616. return (u16)sizeof(struct iwl4965_addsta_cmd);
  1617. }
  1618. static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
  1619. {
  1620. return le32_to_cpup(&tx_resp->u.status + tx_resp->frame_count) & MAX_SN;
  1621. }
  1622. /**
  1623. * iwl4965_tx_status_reply_tx - Handle Tx response for frames in aggregation queue
  1624. */
  1625. static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
  1626. struct iwl_ht_agg *agg,
  1627. struct iwl4965_tx_resp *tx_resp,
  1628. int txq_id, u16 start_idx)
  1629. {
  1630. u16 status;
  1631. struct agg_tx_status *frame_status = tx_resp->u.agg_status;
  1632. struct ieee80211_tx_info *info = NULL;
  1633. struct ieee80211_hdr *hdr = NULL;
  1634. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  1635. int i, sh, idx;
  1636. u16 seq;
  1637. if (agg->wait_for_ba)
  1638. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  1639. agg->frame_count = tx_resp->frame_count;
  1640. agg->start_idx = start_idx;
  1641. agg->rate_n_flags = rate_n_flags;
  1642. agg->bitmap = 0;
  1643. /* num frames attempted by Tx command */
  1644. if (agg->frame_count == 1) {
  1645. /* Only one frame was attempted; no block-ack will arrive */
  1646. status = le16_to_cpu(frame_status[0].status);
  1647. idx = start_idx;
  1648. /* FIXME: code repetition */
  1649. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  1650. agg->frame_count, agg->start_idx, idx);
  1651. info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb);
  1652. info->status.rates[0].count = tx_resp->failure_frame + 1;
  1653. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  1654. info->flags |= iwl_tx_status_to_mac80211(status);
  1655. iwlagn_hwrate_to_tx_control(priv, rate_n_flags, info);
  1656. /* FIXME: code repetition end */
  1657. IWL_DEBUG_TX_REPLY(priv, "1 Frame 0x%x failure :%d\n",
  1658. status & 0xff, tx_resp->failure_frame);
  1659. IWL_DEBUG_TX_REPLY(priv, "Rate Info rate_n_flags=%x\n", rate_n_flags);
  1660. agg->wait_for_ba = 0;
  1661. } else {
  1662. /* Two or more frames were attempted; expect block-ack */
  1663. u64 bitmap = 0;
  1664. int start = agg->start_idx;
  1665. /* Construct bit-map of pending frames within Tx window */
  1666. for (i = 0; i < agg->frame_count; i++) {
  1667. u16 sc;
  1668. status = le16_to_cpu(frame_status[i].status);
  1669. seq = le16_to_cpu(frame_status[i].sequence);
  1670. idx = SEQ_TO_INDEX(seq);
  1671. txq_id = SEQ_TO_QUEUE(seq);
  1672. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  1673. AGG_TX_STATE_ABORT_MSK))
  1674. continue;
  1675. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  1676. agg->frame_count, txq_id, idx);
  1677. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  1678. if (!hdr) {
  1679. IWL_ERR(priv,
  1680. "BUG_ON idx doesn't point to valid skb"
  1681. " idx=%d, txq_id=%d\n", idx, txq_id);
  1682. return -1;
  1683. }
  1684. sc = le16_to_cpu(hdr->seq_ctrl);
  1685. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  1686. IWL_ERR(priv,
  1687. "BUG_ON idx doesn't match seq control"
  1688. " idx=%d, seq_idx=%d, seq=%d\n",
  1689. idx, SEQ_TO_SN(sc), hdr->seq_ctrl);
  1690. return -1;
  1691. }
  1692. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  1693. i, idx, SEQ_TO_SN(sc));
  1694. sh = idx - start;
  1695. if (sh > 64) {
  1696. sh = (start - idx) + 0xff;
  1697. bitmap = bitmap << sh;
  1698. sh = 0;
  1699. start = idx;
  1700. } else if (sh < -64)
  1701. sh = 0xff - (start - idx);
  1702. else if (sh < 0) {
  1703. sh = start - idx;
  1704. start = idx;
  1705. bitmap = bitmap << sh;
  1706. sh = 0;
  1707. }
  1708. bitmap |= 1ULL << sh;
  1709. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  1710. start, (unsigned long long)bitmap);
  1711. }
  1712. agg->bitmap = bitmap;
  1713. agg->start_idx = start;
  1714. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  1715. agg->frame_count, agg->start_idx,
  1716. (unsigned long long)agg->bitmap);
  1717. if (bitmap)
  1718. agg->wait_for_ba = 1;
  1719. }
  1720. return 0;
  1721. }
  1722. static u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
  1723. {
  1724. int i;
  1725. int start = 0;
  1726. int ret = IWL_INVALID_STATION;
  1727. unsigned long flags;
  1728. if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
  1729. (priv->iw_mode == NL80211_IFTYPE_AP))
  1730. start = IWL_STA_ID;
  1731. if (is_broadcast_ether_addr(addr))
  1732. return priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id;
  1733. spin_lock_irqsave(&priv->sta_lock, flags);
  1734. for (i = start; i < priv->hw_params.max_stations; i++)
  1735. if (priv->stations[i].used &&
  1736. (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  1737. addr))) {
  1738. ret = i;
  1739. goto out;
  1740. }
  1741. IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
  1742. addr, priv->num_stations);
  1743. out:
  1744. /*
  1745. * It may be possible that more commands interacting with stations
  1746. * arrive before we completed processing the adding of
  1747. * station
  1748. */
  1749. if (ret != IWL_INVALID_STATION &&
  1750. (!(priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) ||
  1751. ((priv->stations[ret].used & IWL_STA_UCODE_ACTIVE) &&
  1752. (priv->stations[ret].used & IWL_STA_UCODE_INPROGRESS)))) {
  1753. IWL_ERR(priv, "Requested station info for sta %d before ready.\n",
  1754. ret);
  1755. ret = IWL_INVALID_STATION;
  1756. }
  1757. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1758. return ret;
  1759. }
  1760. static int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  1761. {
  1762. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  1763. return IWL_AP_ID;
  1764. } else {
  1765. u8 *da = ieee80211_get_DA(hdr);
  1766. return iwl_find_station(priv, da);
  1767. }
  1768. }
  1769. /**
  1770. * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
  1771. */
  1772. static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
  1773. struct iwl_rx_mem_buffer *rxb)
  1774. {
  1775. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1776. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1777. int txq_id = SEQ_TO_QUEUE(sequence);
  1778. int index = SEQ_TO_INDEX(sequence);
  1779. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1780. struct ieee80211_hdr *hdr;
  1781. struct ieee80211_tx_info *info;
  1782. struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  1783. u32 status = le32_to_cpu(tx_resp->u.status);
  1784. int uninitialized_var(tid);
  1785. int sta_id;
  1786. int freed;
  1787. u8 *qc = NULL;
  1788. unsigned long flags;
  1789. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  1790. IWL_ERR(priv, "Read index for DMA queue txq_id (%d) index %d "
  1791. "is out of range [0-%d] %d %d\n", txq_id,
  1792. index, txq->q.n_bd, txq->q.write_ptr,
  1793. txq->q.read_ptr);
  1794. return;
  1795. }
  1796. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb);
  1797. memset(&info->status, 0, sizeof(info->status));
  1798. hdr = iwl_tx_queue_get_hdr(priv, txq_id, index);
  1799. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1800. qc = ieee80211_get_qos_ctl(hdr);
  1801. tid = qc[0] & 0xf;
  1802. }
  1803. sta_id = iwl_get_ra_sta_id(priv, hdr);
  1804. if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
  1805. IWL_ERR(priv, "Station not known\n");
  1806. return;
  1807. }
  1808. spin_lock_irqsave(&priv->sta_lock, flags);
  1809. if (txq->sched_retry) {
  1810. const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
  1811. struct iwl_ht_agg *agg = NULL;
  1812. WARN_ON(!qc);
  1813. agg = &priv->stations[sta_id].tid[tid].agg;
  1814. iwl4965_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  1815. /* check if BAR is needed */
  1816. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  1817. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  1818. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  1819. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  1820. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim scd_ssn "
  1821. "%d index %d\n", scd_ssn , index);
  1822. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  1823. if (qc)
  1824. iwl_free_tfds_in_queue(priv, sta_id,
  1825. tid, freed);
  1826. if (priv->mac80211_registered &&
  1827. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  1828. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)) {
  1829. if (agg->state == IWL_AGG_OFF)
  1830. iwl_wake_queue(priv, txq_id);
  1831. else
  1832. iwl_wake_queue(priv, txq->swq_id);
  1833. }
  1834. }
  1835. } else {
  1836. info->status.rates[0].count = tx_resp->failure_frame + 1;
  1837. info->flags |= iwl_tx_status_to_mac80211(status);
  1838. iwlagn_hwrate_to_tx_control(priv,
  1839. le32_to_cpu(tx_resp->rate_n_flags),
  1840. info);
  1841. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) "
  1842. "rate_n_flags 0x%x retries %d\n",
  1843. txq_id,
  1844. iwl_get_tx_fail_reason(status), status,
  1845. le32_to_cpu(tx_resp->rate_n_flags),
  1846. tx_resp->failure_frame);
  1847. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  1848. if (qc && likely(sta_id != IWL_INVALID_STATION))
  1849. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  1850. else if (sta_id == IWL_INVALID_STATION)
  1851. IWL_DEBUG_TX_REPLY(priv, "Station not known\n");
  1852. if (priv->mac80211_registered &&
  1853. (iwl_queue_space(&txq->q) > txq->q.low_mark))
  1854. iwl_wake_queue(priv, txq_id);
  1855. }
  1856. if (qc && likely(sta_id != IWL_INVALID_STATION))
  1857. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  1858. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  1859. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1860. }
  1861. static int iwl4965_calc_rssi(struct iwl_priv *priv,
  1862. struct iwl_rx_phy_res *rx_resp)
  1863. {
  1864. /* data from PHY/DSP regarding signal strength, etc.,
  1865. * contents are always there, not configurable by host. */
  1866. struct iwl4965_rx_non_cfg_phy *ncphy =
  1867. (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  1868. u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL49_AGC_DB_MASK)
  1869. >> IWL49_AGC_DB_POS;
  1870. u32 valid_antennae =
  1871. (le16_to_cpu(rx_resp->phy_flags) & IWL49_RX_PHY_FLAGS_ANTENNAE_MASK)
  1872. >> IWL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
  1873. u8 max_rssi = 0;
  1874. u32 i;
  1875. /* Find max rssi among 3 possible receivers.
  1876. * These values are measured by the digital signal processor (DSP).
  1877. * They should stay fairly constant even as the signal strength varies,
  1878. * if the radio's automatic gain control (AGC) is working right.
  1879. * AGC value (see below) will provide the "interesting" info. */
  1880. for (i = 0; i < 3; i++)
  1881. if (valid_antennae & (1 << i))
  1882. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  1883. IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  1884. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  1885. max_rssi, agc);
  1886. /* dBm = max_rssi dB - agc dB - constant.
  1887. * Higher AGC (higher radio gain) means lower signal. */
  1888. return max_rssi - agc - IWLAGN_RSSI_OFFSET;
  1889. }
  1890. /* Set up 4965-specific Rx frame reply handlers */
  1891. static void iwl4965_rx_handler_setup(struct iwl_priv *priv)
  1892. {
  1893. /* Legacy Rx frames */
  1894. priv->rx_handlers[REPLY_RX] = iwlagn_rx_reply_rx;
  1895. /* Tx response */
  1896. priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
  1897. }
  1898. static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
  1899. {
  1900. INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
  1901. }
  1902. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
  1903. {
  1904. cancel_work_sync(&priv->txpower_work);
  1905. }
  1906. static struct iwl_hcmd_ops iwl4965_hcmd = {
  1907. .rxon_assoc = iwl4965_send_rxon_assoc,
  1908. .commit_rxon = iwl_commit_rxon,
  1909. .set_rxon_chain = iwl_set_rxon_chain,
  1910. .send_bt_config = iwl_send_bt_config,
  1911. };
  1912. static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
  1913. .get_hcmd_size = iwl4965_get_hcmd_size,
  1914. .build_addsta_hcmd = iwl4965_build_addsta_hcmd,
  1915. .chain_noise_reset = iwl4965_chain_noise_reset,
  1916. .gain_computation = iwl4965_gain_computation,
  1917. .tx_cmd_protection = iwlcore_tx_cmd_protection,
  1918. .calc_rssi = iwl4965_calc_rssi,
  1919. .request_scan = iwlagn_request_scan,
  1920. };
  1921. static struct iwl_lib_ops iwl4965_lib = {
  1922. .set_hw_params = iwl4965_hw_set_hw_params,
  1923. .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
  1924. .txq_set_sched = iwl4965_txq_set_sched,
  1925. .txq_agg_enable = iwl4965_txq_agg_enable,
  1926. .txq_agg_disable = iwl4965_txq_agg_disable,
  1927. .txq_attach_buf_to_tfd = iwl_hw_txq_attach_buf_to_tfd,
  1928. .txq_free_tfd = iwl_hw_txq_free_tfd,
  1929. .txq_init = iwl_hw_tx_queue_init,
  1930. .rx_handler_setup = iwl4965_rx_handler_setup,
  1931. .setup_deferred_work = iwl4965_setup_deferred_work,
  1932. .cancel_deferred_work = iwl4965_cancel_deferred_work,
  1933. .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
  1934. .alive_notify = iwl4965_alive_notify,
  1935. .init_alive_start = iwl4965_init_alive_start,
  1936. .load_ucode = iwl4965_load_bsm,
  1937. .dump_nic_event_log = iwl_dump_nic_event_log,
  1938. .dump_nic_error_log = iwl_dump_nic_error_log,
  1939. .dump_fh = iwl_dump_fh,
  1940. .set_channel_switch = iwl4965_hw_channel_switch,
  1941. .apm_ops = {
  1942. .init = iwl_apm_init,
  1943. .stop = iwl_apm_stop,
  1944. .config = iwl4965_nic_config,
  1945. .set_pwr_src = iwl_set_pwr_src,
  1946. },
  1947. .eeprom_ops = {
  1948. .regulatory_bands = {
  1949. EEPROM_REGULATORY_BAND_1_CHANNELS,
  1950. EEPROM_REGULATORY_BAND_2_CHANNELS,
  1951. EEPROM_REGULATORY_BAND_3_CHANNELS,
  1952. EEPROM_REGULATORY_BAND_4_CHANNELS,
  1953. EEPROM_REGULATORY_BAND_5_CHANNELS,
  1954. EEPROM_4965_REGULATORY_BAND_24_HT40_CHANNELS,
  1955. EEPROM_4965_REGULATORY_BAND_52_HT40_CHANNELS
  1956. },
  1957. .verify_signature = iwlcore_eeprom_verify_signature,
  1958. .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
  1959. .release_semaphore = iwlcore_eeprom_release_semaphore,
  1960. .calib_version = iwl4965_eeprom_calib_version,
  1961. .query_addr = iwlcore_eeprom_query_addr,
  1962. },
  1963. .send_tx_power = iwl4965_send_tx_power,
  1964. .update_chain_flags = iwl_update_chain_flags,
  1965. .post_associate = iwl_post_associate,
  1966. .config_ap = iwl_config_ap,
  1967. .isr = iwl_isr_legacy,
  1968. .temp_ops = {
  1969. .temperature = iwl4965_temperature_calib,
  1970. .set_ct_kill = iwl4965_set_ct_threshold,
  1971. },
  1972. .manage_ibss_station = iwlagn_manage_ibss_station,
  1973. .update_bcast_stations = iwl_update_bcast_stations,
  1974. .debugfs_ops = {
  1975. .rx_stats_read = iwl_ucode_rx_stats_read,
  1976. .tx_stats_read = iwl_ucode_tx_stats_read,
  1977. .general_stats_read = iwl_ucode_general_stats_read,
  1978. .bt_stats_read = iwl_ucode_bt_stats_read,
  1979. .reply_tx_error = iwl_reply_tx_error_read,
  1980. },
  1981. .recover_from_tx_stall = iwl_bg_monitor_recover,
  1982. .check_plcp_health = iwl_good_plcp_health,
  1983. };
  1984. static const struct iwl_ops iwl4965_ops = {
  1985. .lib = &iwl4965_lib,
  1986. .hcmd = &iwl4965_hcmd,
  1987. .utils = &iwl4965_hcmd_utils,
  1988. .led = &iwlagn_led_ops,
  1989. };
  1990. static struct iwl_base_params iwl4965_base_params = {
  1991. .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
  1992. .num_of_queues = IWL49_NUM_QUEUES,
  1993. .num_of_ampdu_queues = IWL49_NUM_AMPDU_QUEUES,
  1994. .pll_cfg_val = 0,
  1995. .set_l0s = true,
  1996. .use_bsm = true,
  1997. .use_isr_legacy = true,
  1998. .broken_powersave = true,
  1999. .led_compensation = 61,
  2000. .chain_noise_num_beacons = IWL4965_CAL_NUM_BEACONS,
  2001. .plcp_delta_threshold = IWL_MAX_PLCP_ERR_THRESHOLD_DEF,
  2002. .monitor_recover_period = IWL_DEF_MONITORING_PERIOD,
  2003. .temperature_kelvin = true,
  2004. .max_event_log_size = 512,
  2005. .tx_power_by_driver = true,
  2006. .ucode_tracing = true,
  2007. .sensitivity_calib_by_driver = true,
  2008. .chain_noise_calib_by_driver = true,
  2009. };
  2010. struct iwl_cfg iwl4965_agn_cfg = {
  2011. .name = "Intel(R) Wireless WiFi Link 4965AGN",
  2012. .fw_name_pre = IWL4965_FW_PRE,
  2013. .ucode_api_max = IWL4965_UCODE_API_MAX,
  2014. .ucode_api_min = IWL4965_UCODE_API_MIN,
  2015. .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
  2016. .valid_tx_ant = ANT_AB,
  2017. .valid_rx_ant = ANT_ABC,
  2018. .eeprom_ver = EEPROM_4965_EEPROM_VERSION,
  2019. .eeprom_calib_ver = EEPROM_4965_TX_POWER_VERSION,
  2020. .ops = &iwl4965_ops,
  2021. .mod_params = &iwlagn_mod_params,
  2022. .base_params = &iwl4965_base_params,
  2023. /*
  2024. * Force use of chains B and C for scan RX on 5 GHz band
  2025. * because the device has off-channel reception on chain A.
  2026. */
  2027. .scan_rx_antennas[IEEE80211_BAND_5GHZ] = ANT_BC,
  2028. };
  2029. /* Module firmware */
  2030. MODULE_FIRMWARE(IWL4965_MODULE_FIRMWARE(IWL4965_UCODE_API_MAX));