iwl-4965.c 69 KB

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