iwl-4965.c 68 KB

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