iwl-4965.c 65 KB

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