iwl-4965.c 67 KB

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