iwl-4965.c 67 KB

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