iwl-4965.c 66 KB

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