iwl-4965.c 67 KB

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