iwl-4965.c 68 KB

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