iwl-4965.c 67 KB

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