iwl-4965.c 66 KB

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