iwl-4965.c 69 KB

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