iwl-4965.c 69 KB

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