iwl-4965.c 68 KB

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