iwl-4965.c 98 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439
  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. /* module parameters */
  48. static struct iwl_mod_params iwl4965_mod_params = {
  49. .num_of_queues = IWL49_NUM_QUEUES,
  50. .enable_qos = 1,
  51. .amsdu_size_8K = 1,
  52. .restart_fw = 1,
  53. /* the rest are 0 by default */
  54. };
  55. /* check contents of special bootstrap uCode SRAM */
  56. static int iwl4965_verify_bsm(struct iwl_priv *priv)
  57. {
  58. __le32 *image = priv->ucode_boot.v_addr;
  59. u32 len = priv->ucode_boot.len;
  60. u32 reg;
  61. u32 val;
  62. IWL_DEBUG_INFO("Begin verify bsm\n");
  63. /* verify BSM SRAM contents */
  64. val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
  65. for (reg = BSM_SRAM_LOWER_BOUND;
  66. reg < BSM_SRAM_LOWER_BOUND + len;
  67. reg += sizeof(u32), image++) {
  68. val = iwl_read_prph(priv, reg);
  69. if (val != le32_to_cpu(*image)) {
  70. IWL_ERROR("BSM uCode verification failed at "
  71. "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
  72. BSM_SRAM_LOWER_BOUND,
  73. reg - BSM_SRAM_LOWER_BOUND, len,
  74. val, le32_to_cpu(*image));
  75. return -EIO;
  76. }
  77. }
  78. IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
  79. return 0;
  80. }
  81. /**
  82. * iwl4965_load_bsm - Load bootstrap instructions
  83. *
  84. * BSM operation:
  85. *
  86. * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
  87. * in special SRAM that does not power down during RFKILL. When powering back
  88. * up after power-saving sleeps (or during initial uCode load), the BSM loads
  89. * the bootstrap program into the on-board processor, and starts it.
  90. *
  91. * The bootstrap program loads (via DMA) instructions and data for a new
  92. * program from host DRAM locations indicated by the host driver in the
  93. * BSM_DRAM_* registers. Once the new program is loaded, it starts
  94. * automatically.
  95. *
  96. * When initializing the NIC, the host driver points the BSM to the
  97. * "initialize" uCode image. This uCode sets up some internal data, then
  98. * notifies host via "initialize alive" that it is complete.
  99. *
  100. * The host then replaces the BSM_DRAM_* pointer values to point to the
  101. * normal runtime uCode instructions and a backup uCode data cache buffer
  102. * (filled initially with starting data values for the on-board processor),
  103. * then triggers the "initialize" uCode to load and launch the runtime uCode,
  104. * which begins normal operation.
  105. *
  106. * When doing a power-save shutdown, runtime uCode saves data SRAM into
  107. * the backup data cache in DRAM before SRAM is powered down.
  108. *
  109. * When powering back up, the BSM loads the bootstrap program. This reloads
  110. * the runtime uCode instructions and the backup data cache into SRAM,
  111. * and re-launches the runtime uCode from where it left off.
  112. */
  113. static int iwl4965_load_bsm(struct iwl_priv *priv)
  114. {
  115. __le32 *image = priv->ucode_boot.v_addr;
  116. u32 len = priv->ucode_boot.len;
  117. dma_addr_t pinst;
  118. dma_addr_t pdata;
  119. u32 inst_len;
  120. u32 data_len;
  121. int i;
  122. u32 done;
  123. u32 reg_offset;
  124. int ret;
  125. IWL_DEBUG_INFO("Begin load bsm\n");
  126. priv->ucode_type = UCODE_RT;
  127. /* make sure bootstrap program is no larger than BSM's SRAM size */
  128. if (len > IWL_MAX_BSM_SIZE)
  129. return -EINVAL;
  130. /* Tell bootstrap uCode where to find the "Initialize" uCode
  131. * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
  132. * NOTE: iwl_init_alive_start() will replace these values,
  133. * after the "initialize" uCode has run, to point to
  134. * runtime/protocol instructions and backup data cache.
  135. */
  136. pinst = priv->ucode_init.p_addr >> 4;
  137. pdata = priv->ucode_init_data.p_addr >> 4;
  138. inst_len = priv->ucode_init.len;
  139. data_len = priv->ucode_init_data.len;
  140. ret = iwl_grab_nic_access(priv);
  141. if (ret)
  142. return ret;
  143. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  144. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  145. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
  146. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
  147. /* Fill BSM memory with bootstrap instructions */
  148. for (reg_offset = BSM_SRAM_LOWER_BOUND;
  149. reg_offset < BSM_SRAM_LOWER_BOUND + len;
  150. reg_offset += sizeof(u32), image++)
  151. _iwl_write_prph(priv, reg_offset, le32_to_cpu(*image));
  152. ret = iwl4965_verify_bsm(priv);
  153. if (ret) {
  154. iwl_release_nic_access(priv);
  155. return ret;
  156. }
  157. /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
  158. iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
  159. iwl_write_prph(priv, BSM_WR_MEM_DST_REG, RTC_INST_LOWER_BOUND);
  160. iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
  161. /* Load bootstrap code into instruction SRAM now,
  162. * to prepare to load "initialize" uCode */
  163. iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START);
  164. /* Wait for load of bootstrap uCode to finish */
  165. for (i = 0; i < 100; i++) {
  166. done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
  167. if (!(done & BSM_WR_CTRL_REG_BIT_START))
  168. break;
  169. udelay(10);
  170. }
  171. if (i < 100)
  172. IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
  173. else {
  174. IWL_ERROR("BSM write did not complete!\n");
  175. return -EIO;
  176. }
  177. /* Enable future boot loads whenever power management unit triggers it
  178. * (e.g. when powering back up after power-save shutdown) */
  179. iwl_write_prph(priv, BSM_WR_CTRL_REG, BSM_WR_CTRL_REG_BIT_START_EN);
  180. iwl_release_nic_access(priv);
  181. return 0;
  182. }
  183. /**
  184. * iwl4965_set_ucode_ptrs - Set uCode address location
  185. *
  186. * Tell initialization uCode where to find runtime uCode.
  187. *
  188. * BSM registers initially contain pointers to initialization uCode.
  189. * We need to replace them to load runtime uCode inst and data,
  190. * and to save runtime data when powering down.
  191. */
  192. static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
  193. {
  194. dma_addr_t pinst;
  195. dma_addr_t pdata;
  196. unsigned long flags;
  197. int ret = 0;
  198. /* bits 35:4 for 4965 */
  199. pinst = priv->ucode_code.p_addr >> 4;
  200. pdata = priv->ucode_data_backup.p_addr >> 4;
  201. spin_lock_irqsave(&priv->lock, flags);
  202. ret = iwl_grab_nic_access(priv);
  203. if (ret) {
  204. spin_unlock_irqrestore(&priv->lock, flags);
  205. return ret;
  206. }
  207. /* Tell bootstrap uCode where to find image to load */
  208. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  209. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  210. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  211. priv->ucode_data.len);
  212. /* Inst bytecount must be last to set up, bit 31 signals uCode
  213. * that all new ptr/size info is in place */
  214. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  215. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  216. iwl_release_nic_access(priv);
  217. spin_unlock_irqrestore(&priv->lock, flags);
  218. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  219. return ret;
  220. }
  221. /**
  222. * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
  223. *
  224. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  225. *
  226. * The 4965 "initialize" ALIVE reply contains calibration data for:
  227. * Voltage, temperature, and MIMO tx gain correction, now stored in priv
  228. * (3945 does not contain this data).
  229. *
  230. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  231. */
  232. static void iwl4965_init_alive_start(struct iwl_priv *priv)
  233. {
  234. /* Check alive response for "valid" sign from uCode */
  235. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  236. /* We had an error bringing up the hardware, so take it
  237. * all the way back down so we can try again */
  238. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  239. goto restart;
  240. }
  241. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  242. * This is a paranoid check, because we would not have gotten the
  243. * "initialize" alive if code weren't properly loaded. */
  244. if (iwl_verify_ucode(priv)) {
  245. /* Runtime instruction load was bad;
  246. * take it all the way back down so we can try again */
  247. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  248. goto restart;
  249. }
  250. /* Calculate temperature */
  251. priv->temperature = iwl4965_get_temperature(priv);
  252. /* Send pointers to protocol/runtime uCode image ... init code will
  253. * load and launch runtime uCode, which will send us another "Alive"
  254. * notification. */
  255. IWL_DEBUG_INFO("Initialization Alive received.\n");
  256. if (iwl4965_set_ucode_ptrs(priv)) {
  257. /* Runtime instruction load won't happen;
  258. * take it all the way back down so we can try again */
  259. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  260. goto restart;
  261. }
  262. return;
  263. restart:
  264. queue_work(priv->workqueue, &priv->restart);
  265. }
  266. static int is_fat_channel(__le32 rxon_flags)
  267. {
  268. return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
  269. (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
  270. }
  271. /*
  272. * EEPROM handlers
  273. */
  274. static int iwl4965_eeprom_check_version(struct iwl_priv *priv)
  275. {
  276. u16 eeprom_ver;
  277. u16 calib_ver;
  278. eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
  279. calib_ver = iwl_eeprom_query16(priv, EEPROM_4965_CALIB_VERSION_OFFSET);
  280. if (eeprom_ver < EEPROM_4965_EEPROM_VERSION ||
  281. calib_ver < EEPROM_4965_TX_POWER_VERSION)
  282. goto err;
  283. return 0;
  284. err:
  285. IWL_ERROR("Unsuported EEPROM VER=0x%x < 0x%x CALIB=0x%x < 0x%x\n",
  286. eeprom_ver, EEPROM_4965_EEPROM_VERSION,
  287. calib_ver, EEPROM_4965_TX_POWER_VERSION);
  288. return -EINVAL;
  289. }
  290. int iwl4965_set_pwr_src(struct iwl_priv *priv, enum iwl_pwr_src src)
  291. {
  292. int ret;
  293. unsigned long flags;
  294. spin_lock_irqsave(&priv->lock, flags);
  295. ret = iwl_grab_nic_access(priv);
  296. if (ret) {
  297. spin_unlock_irqrestore(&priv->lock, flags);
  298. return ret;
  299. }
  300. if (src == IWL_PWR_SRC_VAUX) {
  301. u32 val;
  302. ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
  303. &val);
  304. if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT) {
  305. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  306. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  307. ~APMG_PS_CTRL_MSK_PWR_SRC);
  308. }
  309. } else {
  310. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  311. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  312. ~APMG_PS_CTRL_MSK_PWR_SRC);
  313. }
  314. iwl_release_nic_access(priv);
  315. spin_unlock_irqrestore(&priv->lock, flags);
  316. return ret;
  317. }
  318. /*
  319. * Activate/Deactivat Tx DMA/FIFO channels according tx fifos mask
  320. * must be called under priv->lock and mac access
  321. */
  322. static void iwl4965_txq_set_sched(struct iwl_priv *priv, u32 mask)
  323. {
  324. iwl_write_prph(priv, IWL49_SCD_TXFACT, mask);
  325. }
  326. static int iwl4965_apm_init(struct iwl_priv *priv)
  327. {
  328. int ret = 0;
  329. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  330. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  331. /* disable L0s without affecting L1 :don't wait for ICH L0s bug W/A) */
  332. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  333. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  334. /* set "initialization complete" bit to move adapter
  335. * D0U* --> D0A* state */
  336. iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  337. /* wait for clock stabilization */
  338. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  339. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  340. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  341. if (ret < 0) {
  342. IWL_DEBUG_INFO("Failed to init the card\n");
  343. goto out;
  344. }
  345. ret = iwl_grab_nic_access(priv);
  346. if (ret)
  347. goto out;
  348. /* enable DMA */
  349. iwl_write_prph(priv, APMG_CLK_CTRL_REG, APMG_CLK_VAL_DMA_CLK_RQT |
  350. APMG_CLK_VAL_BSM_CLK_RQT);
  351. udelay(20);
  352. /* disable L1-Active */
  353. iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  354. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  355. iwl_release_nic_access(priv);
  356. out:
  357. return ret;
  358. }
  359. static void iwl4965_nic_config(struct iwl_priv *priv)
  360. {
  361. unsigned long flags;
  362. u32 val;
  363. u16 radio_cfg;
  364. u8 val_link;
  365. spin_lock_irqsave(&priv->lock, flags);
  366. if ((priv->rev_id & 0x80) == 0x80 && (priv->rev_id & 0x7f) < 8) {
  367. pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
  368. /* Enable No Snoop field */
  369. pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
  370. val & ~(1 << 11));
  371. }
  372. pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
  373. /* L1 is enabled by BIOS */
  374. if ((val_link & PCI_LINK_VAL_L1_EN) == PCI_LINK_VAL_L1_EN)
  375. /* diable L0S disabled L1A enabled */
  376. iwl_set_bit(priv, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  377. else
  378. /* L0S enabled L1A disabled */
  379. iwl_clear_bit(priv, CSR_GIO_REG, CSR_GIO_REG_VAL_L0S_ENABLED);
  380. radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
  381. /* write radio config values to register */
  382. if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) == EEPROM_4965_RF_CFG_TYPE_MAX)
  383. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  384. EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
  385. EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
  386. EEPROM_RF_CFG_DASH_MSK(radio_cfg));
  387. /* set CSR_HW_CONFIG_REG for uCode use */
  388. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  389. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  390. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
  391. priv->calib_info = (struct iwl_eeprom_calib_info *)
  392. iwl_eeprom_query_addr(priv, EEPROM_4965_CALIB_TXPOWER_OFFSET);
  393. spin_unlock_irqrestore(&priv->lock, flags);
  394. }
  395. static int iwl4965_apm_stop_master(struct iwl_priv *priv)
  396. {
  397. int ret = 0;
  398. unsigned long flags;
  399. spin_lock_irqsave(&priv->lock, flags);
  400. /* set stop master bit */
  401. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  402. ret = iwl_poll_bit(priv, CSR_RESET,
  403. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  404. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  405. if (ret < 0)
  406. goto out;
  407. out:
  408. spin_unlock_irqrestore(&priv->lock, flags);
  409. IWL_DEBUG_INFO("stop master\n");
  410. return ret;
  411. }
  412. static void iwl4965_apm_stop(struct iwl_priv *priv)
  413. {
  414. unsigned long flags;
  415. iwl4965_apm_stop_master(priv);
  416. spin_lock_irqsave(&priv->lock, flags);
  417. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  418. udelay(10);
  419. iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  420. spin_unlock_irqrestore(&priv->lock, flags);
  421. }
  422. static int iwl4965_apm_reset(struct iwl_priv *priv)
  423. {
  424. int ret = 0;
  425. unsigned long flags;
  426. iwl4965_apm_stop_master(priv);
  427. spin_lock_irqsave(&priv->lock, flags);
  428. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  429. udelay(10);
  430. /* FIXME: put here L1A -L0S w/a */
  431. iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  432. ret = iwl_poll_bit(priv, CSR_RESET,
  433. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  434. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
  435. if (ret)
  436. goto out;
  437. udelay(10);
  438. ret = iwl_grab_nic_access(priv);
  439. if (ret)
  440. goto out;
  441. /* Enable DMA and BSM Clock */
  442. iwl_write_prph(priv, APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT |
  443. APMG_CLK_VAL_BSM_CLK_RQT);
  444. udelay(10);
  445. /* disable L1A */
  446. iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  447. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  448. iwl_release_nic_access(priv);
  449. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  450. wake_up_interruptible(&priv->wait_command_queue);
  451. out:
  452. spin_unlock_irqrestore(&priv->lock, flags);
  453. return ret;
  454. }
  455. #define REG_RECALIB_PERIOD (60)
  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. 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. 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.sw_crypto = priv->cfg->mod_params->sw_crypto;
  691. priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  692. priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  693. if (priv->cfg->mod_params->amsdu_size_8K)
  694. priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_8K;
  695. else
  696. priv->hw_params.rx_buf_size = IWL_RX_BUF_SIZE_4K;
  697. priv->hw_params.max_pkt_size = priv->hw_params.rx_buf_size - 256;
  698. priv->hw_params.max_stations = IWL4965_STATION_COUNT;
  699. priv->hw_params.bcast_sta_id = IWL4965_BROADCAST_ID;
  700. priv->hw_params.max_data_size = IWL49_RTC_DATA_SIZE;
  701. priv->hw_params.max_inst_size = IWL49_RTC_INST_SIZE;
  702. priv->hw_params.max_bsm_size = BSM_SRAM_SIZE;
  703. priv->hw_params.fat_channel = BIT(IEEE80211_BAND_5GHZ);
  704. priv->hw_params.tx_chains_num = 2;
  705. priv->hw_params.rx_chains_num = 2;
  706. priv->hw_params.valid_tx_ant = ANT_A | ANT_B;
  707. priv->hw_params.valid_rx_ant = ANT_A | ANT_B;
  708. priv->hw_params.ct_kill_threshold = CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD);
  709. priv->hw_params.sens = &iwl4965_sensitivity;
  710. return 0;
  711. }
  712. /* set card power command */
  713. static int iwl4965_set_power(struct iwl_priv *priv,
  714. void *cmd)
  715. {
  716. int ret = 0;
  717. ret = iwl_send_cmd_pdu_async(priv, POWER_TABLE_CMD,
  718. sizeof(struct iwl4965_powertable_cmd),
  719. cmd, NULL);
  720. return ret;
  721. }
  722. static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
  723. {
  724. s32 sign = 1;
  725. if (num < 0) {
  726. sign = -sign;
  727. num = -num;
  728. }
  729. if (denom < 0) {
  730. sign = -sign;
  731. denom = -denom;
  732. }
  733. *res = 1;
  734. *res = ((num * 2 + denom) / (denom * 2)) * sign;
  735. return 1;
  736. }
  737. /**
  738. * iwl4965_get_voltage_compensation - Power supply voltage comp for txpower
  739. *
  740. * Determines power supply voltage compensation for txpower calculations.
  741. * Returns number of 1/2-dB steps to subtract from gain table index,
  742. * to compensate for difference between power supply voltage during
  743. * factory measurements, vs. current power supply voltage.
  744. *
  745. * Voltage indication is higher for lower voltage.
  746. * Lower voltage requires more gain (lower gain table index).
  747. */
  748. static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
  749. s32 current_voltage)
  750. {
  751. s32 comp = 0;
  752. if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
  753. (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
  754. return 0;
  755. iwl4965_math_div_round(current_voltage - eeprom_voltage,
  756. TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
  757. if (current_voltage > eeprom_voltage)
  758. comp *= 2;
  759. if ((comp < -2) || (comp > 2))
  760. comp = 0;
  761. return comp;
  762. }
  763. static s32 iwl4965_get_tx_atten_grp(u16 channel)
  764. {
  765. if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
  766. channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
  767. return CALIB_CH_GROUP_5;
  768. if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
  769. channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
  770. return CALIB_CH_GROUP_1;
  771. if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
  772. channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
  773. return CALIB_CH_GROUP_2;
  774. if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
  775. channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
  776. return CALIB_CH_GROUP_3;
  777. if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
  778. channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
  779. return CALIB_CH_GROUP_4;
  780. IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
  781. return -1;
  782. }
  783. static u32 iwl4965_get_sub_band(const struct iwl_priv *priv, u32 channel)
  784. {
  785. s32 b = -1;
  786. for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
  787. if (priv->calib_info->band_info[b].ch_from == 0)
  788. continue;
  789. if ((channel >= priv->calib_info->band_info[b].ch_from)
  790. && (channel <= priv->calib_info->band_info[b].ch_to))
  791. break;
  792. }
  793. return b;
  794. }
  795. static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
  796. {
  797. s32 val;
  798. if (x2 == x1)
  799. return y1;
  800. else {
  801. iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
  802. return val + y2;
  803. }
  804. }
  805. /**
  806. * iwl4965_interpolate_chan - Interpolate factory measurements for one channel
  807. *
  808. * Interpolates factory measurements from the two sample channels within a
  809. * sub-band, to apply to channel of interest. Interpolation is proportional to
  810. * differences in channel frequencies, which is proportional to differences
  811. * in channel number.
  812. */
  813. static int iwl4965_interpolate_chan(struct iwl_priv *priv, u32 channel,
  814. struct iwl_eeprom_calib_ch_info *chan_info)
  815. {
  816. s32 s = -1;
  817. u32 c;
  818. u32 m;
  819. const struct iwl_eeprom_calib_measure *m1;
  820. const struct iwl_eeprom_calib_measure *m2;
  821. struct iwl_eeprom_calib_measure *omeas;
  822. u32 ch_i1;
  823. u32 ch_i2;
  824. s = iwl4965_get_sub_band(priv, channel);
  825. if (s >= EEPROM_TX_POWER_BANDS) {
  826. IWL_ERROR("Tx Power can not find channel %d ", channel);
  827. return -1;
  828. }
  829. ch_i1 = priv->calib_info->band_info[s].ch1.ch_num;
  830. ch_i2 = priv->calib_info->band_info[s].ch2.ch_num;
  831. chan_info->ch_num = (u8) channel;
  832. IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
  833. channel, s, ch_i1, ch_i2);
  834. for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
  835. for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
  836. m1 = &(priv->calib_info->band_info[s].ch1.
  837. measurements[c][m]);
  838. m2 = &(priv->calib_info->band_info[s].ch2.
  839. measurements[c][m]);
  840. omeas = &(chan_info->measurements[c][m]);
  841. omeas->actual_pow =
  842. (u8) iwl4965_interpolate_value(channel, ch_i1,
  843. m1->actual_pow,
  844. ch_i2,
  845. m2->actual_pow);
  846. omeas->gain_idx =
  847. (u8) iwl4965_interpolate_value(channel, ch_i1,
  848. m1->gain_idx, ch_i2,
  849. m2->gain_idx);
  850. omeas->temperature =
  851. (u8) iwl4965_interpolate_value(channel, ch_i1,
  852. m1->temperature,
  853. ch_i2,
  854. m2->temperature);
  855. omeas->pa_det =
  856. (s8) iwl4965_interpolate_value(channel, ch_i1,
  857. m1->pa_det, ch_i2,
  858. m2->pa_det);
  859. IWL_DEBUG_TXPOWER
  860. ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
  861. m1->actual_pow, m2->actual_pow, omeas->actual_pow);
  862. IWL_DEBUG_TXPOWER
  863. ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
  864. m1->gain_idx, m2->gain_idx, omeas->gain_idx);
  865. IWL_DEBUG_TXPOWER
  866. ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
  867. m1->pa_det, m2->pa_det, omeas->pa_det);
  868. IWL_DEBUG_TXPOWER
  869. ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
  870. m1->temperature, m2->temperature,
  871. omeas->temperature);
  872. }
  873. }
  874. return 0;
  875. }
  876. /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
  877. * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
  878. static s32 back_off_table[] = {
  879. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
  880. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
  881. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
  882. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
  883. 10 /* CCK */
  884. };
  885. /* Thermal compensation values for txpower for various frequency ranges ...
  886. * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
  887. static struct iwl4965_txpower_comp_entry {
  888. s32 degrees_per_05db_a;
  889. s32 degrees_per_05db_a_denom;
  890. } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
  891. {9, 2}, /* group 0 5.2, ch 34-43 */
  892. {4, 1}, /* group 1 5.2, ch 44-70 */
  893. {4, 1}, /* group 2 5.2, ch 71-124 */
  894. {4, 1}, /* group 3 5.2, ch 125-200 */
  895. {3, 1} /* group 4 2.4, ch all */
  896. };
  897. static s32 get_min_power_index(s32 rate_power_index, u32 band)
  898. {
  899. if (!band) {
  900. if ((rate_power_index & 7) <= 4)
  901. return MIN_TX_GAIN_INDEX_52GHZ_EXT;
  902. }
  903. return MIN_TX_GAIN_INDEX;
  904. }
  905. struct gain_entry {
  906. u8 dsp;
  907. u8 radio;
  908. };
  909. static const struct gain_entry gain_table[2][108] = {
  910. /* 5.2GHz power gain index table */
  911. {
  912. {123, 0x3F}, /* highest txpower */
  913. {117, 0x3F},
  914. {110, 0x3F},
  915. {104, 0x3F},
  916. {98, 0x3F},
  917. {110, 0x3E},
  918. {104, 0x3E},
  919. {98, 0x3E},
  920. {110, 0x3D},
  921. {104, 0x3D},
  922. {98, 0x3D},
  923. {110, 0x3C},
  924. {104, 0x3C},
  925. {98, 0x3C},
  926. {110, 0x3B},
  927. {104, 0x3B},
  928. {98, 0x3B},
  929. {110, 0x3A},
  930. {104, 0x3A},
  931. {98, 0x3A},
  932. {110, 0x39},
  933. {104, 0x39},
  934. {98, 0x39},
  935. {110, 0x38},
  936. {104, 0x38},
  937. {98, 0x38},
  938. {110, 0x37},
  939. {104, 0x37},
  940. {98, 0x37},
  941. {110, 0x36},
  942. {104, 0x36},
  943. {98, 0x36},
  944. {110, 0x35},
  945. {104, 0x35},
  946. {98, 0x35},
  947. {110, 0x34},
  948. {104, 0x34},
  949. {98, 0x34},
  950. {110, 0x33},
  951. {104, 0x33},
  952. {98, 0x33},
  953. {110, 0x32},
  954. {104, 0x32},
  955. {98, 0x32},
  956. {110, 0x31},
  957. {104, 0x31},
  958. {98, 0x31},
  959. {110, 0x30},
  960. {104, 0x30},
  961. {98, 0x30},
  962. {110, 0x25},
  963. {104, 0x25},
  964. {98, 0x25},
  965. {110, 0x24},
  966. {104, 0x24},
  967. {98, 0x24},
  968. {110, 0x23},
  969. {104, 0x23},
  970. {98, 0x23},
  971. {110, 0x22},
  972. {104, 0x18},
  973. {98, 0x18},
  974. {110, 0x17},
  975. {104, 0x17},
  976. {98, 0x17},
  977. {110, 0x16},
  978. {104, 0x16},
  979. {98, 0x16},
  980. {110, 0x15},
  981. {104, 0x15},
  982. {98, 0x15},
  983. {110, 0x14},
  984. {104, 0x14},
  985. {98, 0x14},
  986. {110, 0x13},
  987. {104, 0x13},
  988. {98, 0x13},
  989. {110, 0x12},
  990. {104, 0x08},
  991. {98, 0x08},
  992. {110, 0x07},
  993. {104, 0x07},
  994. {98, 0x07},
  995. {110, 0x06},
  996. {104, 0x06},
  997. {98, 0x06},
  998. {110, 0x05},
  999. {104, 0x05},
  1000. {98, 0x05},
  1001. {110, 0x04},
  1002. {104, 0x04},
  1003. {98, 0x04},
  1004. {110, 0x03},
  1005. {104, 0x03},
  1006. {98, 0x03},
  1007. {110, 0x02},
  1008. {104, 0x02},
  1009. {98, 0x02},
  1010. {110, 0x01},
  1011. {104, 0x01},
  1012. {98, 0x01},
  1013. {110, 0x00},
  1014. {104, 0x00},
  1015. {98, 0x00},
  1016. {93, 0x00},
  1017. {88, 0x00},
  1018. {83, 0x00},
  1019. {78, 0x00},
  1020. },
  1021. /* 2.4GHz power gain index table */
  1022. {
  1023. {110, 0x3f}, /* highest txpower */
  1024. {104, 0x3f},
  1025. {98, 0x3f},
  1026. {110, 0x3e},
  1027. {104, 0x3e},
  1028. {98, 0x3e},
  1029. {110, 0x3d},
  1030. {104, 0x3d},
  1031. {98, 0x3d},
  1032. {110, 0x3c},
  1033. {104, 0x3c},
  1034. {98, 0x3c},
  1035. {110, 0x3b},
  1036. {104, 0x3b},
  1037. {98, 0x3b},
  1038. {110, 0x3a},
  1039. {104, 0x3a},
  1040. {98, 0x3a},
  1041. {110, 0x39},
  1042. {104, 0x39},
  1043. {98, 0x39},
  1044. {110, 0x38},
  1045. {104, 0x38},
  1046. {98, 0x38},
  1047. {110, 0x37},
  1048. {104, 0x37},
  1049. {98, 0x37},
  1050. {110, 0x36},
  1051. {104, 0x36},
  1052. {98, 0x36},
  1053. {110, 0x35},
  1054. {104, 0x35},
  1055. {98, 0x35},
  1056. {110, 0x34},
  1057. {104, 0x34},
  1058. {98, 0x34},
  1059. {110, 0x33},
  1060. {104, 0x33},
  1061. {98, 0x33},
  1062. {110, 0x32},
  1063. {104, 0x32},
  1064. {98, 0x32},
  1065. {110, 0x31},
  1066. {104, 0x31},
  1067. {98, 0x31},
  1068. {110, 0x30},
  1069. {104, 0x30},
  1070. {98, 0x30},
  1071. {110, 0x6},
  1072. {104, 0x6},
  1073. {98, 0x6},
  1074. {110, 0x5},
  1075. {104, 0x5},
  1076. {98, 0x5},
  1077. {110, 0x4},
  1078. {104, 0x4},
  1079. {98, 0x4},
  1080. {110, 0x3},
  1081. {104, 0x3},
  1082. {98, 0x3},
  1083. {110, 0x2},
  1084. {104, 0x2},
  1085. {98, 0x2},
  1086. {110, 0x1},
  1087. {104, 0x1},
  1088. {98, 0x1},
  1089. {110, 0x0},
  1090. {104, 0x0},
  1091. {98, 0x0},
  1092. {97, 0},
  1093. {96, 0},
  1094. {95, 0},
  1095. {94, 0},
  1096. {93, 0},
  1097. {92, 0},
  1098. {91, 0},
  1099. {90, 0},
  1100. {89, 0},
  1101. {88, 0},
  1102. {87, 0},
  1103. {86, 0},
  1104. {85, 0},
  1105. {84, 0},
  1106. {83, 0},
  1107. {82, 0},
  1108. {81, 0},
  1109. {80, 0},
  1110. {79, 0},
  1111. {78, 0},
  1112. {77, 0},
  1113. {76, 0},
  1114. {75, 0},
  1115. {74, 0},
  1116. {73, 0},
  1117. {72, 0},
  1118. {71, 0},
  1119. {70, 0},
  1120. {69, 0},
  1121. {68, 0},
  1122. {67, 0},
  1123. {66, 0},
  1124. {65, 0},
  1125. {64, 0},
  1126. {63, 0},
  1127. {62, 0},
  1128. {61, 0},
  1129. {60, 0},
  1130. {59, 0},
  1131. }
  1132. };
  1133. static int iwl4965_fill_txpower_tbl(struct iwl_priv *priv, u8 band, u16 channel,
  1134. u8 is_fat, u8 ctrl_chan_high,
  1135. struct iwl4965_tx_power_db *tx_power_tbl)
  1136. {
  1137. u8 saturation_power;
  1138. s32 target_power;
  1139. s32 user_target_power;
  1140. s32 power_limit;
  1141. s32 current_temp;
  1142. s32 reg_limit;
  1143. s32 current_regulatory;
  1144. s32 txatten_grp = CALIB_CH_GROUP_MAX;
  1145. int i;
  1146. int c;
  1147. const struct iwl_channel_info *ch_info = NULL;
  1148. struct iwl_eeprom_calib_ch_info ch_eeprom_info;
  1149. const struct iwl_eeprom_calib_measure *measurement;
  1150. s16 voltage;
  1151. s32 init_voltage;
  1152. s32 voltage_compensation;
  1153. s32 degrees_per_05db_num;
  1154. s32 degrees_per_05db_denom;
  1155. s32 factory_temp;
  1156. s32 temperature_comp[2];
  1157. s32 factory_gain_index[2];
  1158. s32 factory_actual_pwr[2];
  1159. s32 power_index;
  1160. /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
  1161. * are used for indexing into txpower table) */
  1162. user_target_power = 2 * priv->tx_power_user_lmt;
  1163. /* Get current (RXON) channel, band, width */
  1164. IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
  1165. is_fat);
  1166. ch_info = iwl_get_channel_info(priv, priv->band, channel);
  1167. if (!is_channel_valid(ch_info))
  1168. return -EINVAL;
  1169. /* get txatten group, used to select 1) thermal txpower adjustment
  1170. * and 2) mimo txpower balance between Tx chains. */
  1171. txatten_grp = iwl4965_get_tx_atten_grp(channel);
  1172. if (txatten_grp < 0)
  1173. return -EINVAL;
  1174. IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
  1175. channel, txatten_grp);
  1176. if (is_fat) {
  1177. if (ctrl_chan_high)
  1178. channel -= 2;
  1179. else
  1180. channel += 2;
  1181. }
  1182. /* hardware txpower limits ...
  1183. * saturation (clipping distortion) txpowers are in half-dBm */
  1184. if (band)
  1185. saturation_power = priv->calib_info->saturation_power24;
  1186. else
  1187. saturation_power = priv->calib_info->saturation_power52;
  1188. if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
  1189. saturation_power > IWL_TX_POWER_SATURATION_MAX) {
  1190. if (band)
  1191. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
  1192. else
  1193. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
  1194. }
  1195. /* regulatory txpower limits ... reg_limit values are in half-dBm,
  1196. * max_power_avg values are in dBm, convert * 2 */
  1197. if (is_fat)
  1198. reg_limit = ch_info->fat_max_power_avg * 2;
  1199. else
  1200. reg_limit = ch_info->max_power_avg * 2;
  1201. if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
  1202. (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
  1203. if (band)
  1204. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
  1205. else
  1206. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
  1207. }
  1208. /* Interpolate txpower calibration values for this channel,
  1209. * based on factory calibration tests on spaced channels. */
  1210. iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
  1211. /* calculate tx gain adjustment based on power supply voltage */
  1212. voltage = priv->calib_info->voltage;
  1213. init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
  1214. voltage_compensation =
  1215. iwl4965_get_voltage_compensation(voltage, init_voltage);
  1216. IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
  1217. init_voltage,
  1218. voltage, voltage_compensation);
  1219. /* get current temperature (Celsius) */
  1220. current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
  1221. current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
  1222. current_temp = KELVIN_TO_CELSIUS(current_temp);
  1223. /* select thermal txpower adjustment params, based on channel group
  1224. * (same frequency group used for mimo txatten adjustment) */
  1225. degrees_per_05db_num =
  1226. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
  1227. degrees_per_05db_denom =
  1228. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
  1229. /* get per-chain txpower values from factory measurements */
  1230. for (c = 0; c < 2; c++) {
  1231. measurement = &ch_eeprom_info.measurements[c][1];
  1232. /* txgain adjustment (in half-dB steps) based on difference
  1233. * between factory and current temperature */
  1234. factory_temp = measurement->temperature;
  1235. iwl4965_math_div_round((current_temp - factory_temp) *
  1236. degrees_per_05db_denom,
  1237. degrees_per_05db_num,
  1238. &temperature_comp[c]);
  1239. factory_gain_index[c] = measurement->gain_idx;
  1240. factory_actual_pwr[c] = measurement->actual_pow;
  1241. IWL_DEBUG_TXPOWER("chain = %d\n", c);
  1242. IWL_DEBUG_TXPOWER("fctry tmp %d, "
  1243. "curr tmp %d, comp %d steps\n",
  1244. factory_temp, current_temp,
  1245. temperature_comp[c]);
  1246. IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
  1247. factory_gain_index[c],
  1248. factory_actual_pwr[c]);
  1249. }
  1250. /* for each of 33 bit-rates (including 1 for CCK) */
  1251. for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
  1252. u8 is_mimo_rate;
  1253. union iwl4965_tx_power_dual_stream tx_power;
  1254. /* for mimo, reduce each chain's txpower by half
  1255. * (3dB, 6 steps), so total output power is regulatory
  1256. * compliant. */
  1257. if (i & 0x8) {
  1258. current_regulatory = reg_limit -
  1259. IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
  1260. is_mimo_rate = 1;
  1261. } else {
  1262. current_regulatory = reg_limit;
  1263. is_mimo_rate = 0;
  1264. }
  1265. /* find txpower limit, either hardware or regulatory */
  1266. power_limit = saturation_power - back_off_table[i];
  1267. if (power_limit > current_regulatory)
  1268. power_limit = current_regulatory;
  1269. /* reduce user's txpower request if necessary
  1270. * for this rate on this channel */
  1271. target_power = user_target_power;
  1272. if (target_power > power_limit)
  1273. target_power = power_limit;
  1274. IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
  1275. i, saturation_power - back_off_table[i],
  1276. current_regulatory, user_target_power,
  1277. target_power);
  1278. /* for each of 2 Tx chains (radio transmitters) */
  1279. for (c = 0; c < 2; c++) {
  1280. s32 atten_value;
  1281. if (is_mimo_rate)
  1282. atten_value =
  1283. (s32)le32_to_cpu(priv->card_alive_init.
  1284. tx_atten[txatten_grp][c]);
  1285. else
  1286. atten_value = 0;
  1287. /* calculate index; higher index means lower txpower */
  1288. power_index = (u8) (factory_gain_index[c] -
  1289. (target_power -
  1290. factory_actual_pwr[c]) -
  1291. temperature_comp[c] -
  1292. voltage_compensation +
  1293. atten_value);
  1294. /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
  1295. power_index); */
  1296. if (power_index < get_min_power_index(i, band))
  1297. power_index = get_min_power_index(i, band);
  1298. /* adjust 5 GHz index to support negative indexes */
  1299. if (!band)
  1300. power_index += 9;
  1301. /* CCK, rate 32, reduce txpower for CCK */
  1302. if (i == POWER_TABLE_CCK_ENTRY)
  1303. power_index +=
  1304. IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
  1305. /* stay within the table! */
  1306. if (power_index > 107) {
  1307. IWL_WARNING("txpower index %d > 107\n",
  1308. power_index);
  1309. power_index = 107;
  1310. }
  1311. if (power_index < 0) {
  1312. IWL_WARNING("txpower index %d < 0\n",
  1313. power_index);
  1314. power_index = 0;
  1315. }
  1316. /* fill txpower command for this rate/chain */
  1317. tx_power.s.radio_tx_gain[c] =
  1318. gain_table[band][power_index].radio;
  1319. tx_power.s.dsp_predis_atten[c] =
  1320. gain_table[band][power_index].dsp;
  1321. IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
  1322. "gain 0x%02x dsp %d\n",
  1323. c, atten_value, power_index,
  1324. tx_power.s.radio_tx_gain[c],
  1325. tx_power.s.dsp_predis_atten[c]);
  1326. }/* for each chain */
  1327. tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
  1328. }/* for each rate */
  1329. return 0;
  1330. }
  1331. /**
  1332. * iwl4965_send_tx_power - Configure the TXPOWER level user limit
  1333. *
  1334. * Uses the active RXON for channel, band, and characteristics (fat, high)
  1335. * The power limit is taken from priv->tx_power_user_lmt.
  1336. */
  1337. static int iwl4965_send_tx_power(struct iwl_priv *priv)
  1338. {
  1339. struct iwl4965_txpowertable_cmd cmd = { 0 };
  1340. int ret;
  1341. u8 band = 0;
  1342. u8 is_fat = 0;
  1343. u8 ctrl_chan_high = 0;
  1344. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1345. /* If this gets hit a lot, switch it to a BUG() and catch
  1346. * the stack trace to find out who is calling this during
  1347. * a scan. */
  1348. IWL_WARNING("TX Power requested while scanning!\n");
  1349. return -EAGAIN;
  1350. }
  1351. band = priv->band == IEEE80211_BAND_2GHZ;
  1352. is_fat = is_fat_channel(priv->active_rxon.flags);
  1353. if (is_fat &&
  1354. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  1355. ctrl_chan_high = 1;
  1356. cmd.band = band;
  1357. cmd.channel = priv->active_rxon.channel;
  1358. ret = iwl4965_fill_txpower_tbl(priv, band,
  1359. le16_to_cpu(priv->active_rxon.channel),
  1360. is_fat, ctrl_chan_high, &cmd.tx_power);
  1361. if (ret)
  1362. goto out;
  1363. ret = iwl_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
  1364. out:
  1365. return ret;
  1366. }
  1367. static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
  1368. {
  1369. int ret = 0;
  1370. struct iwl4965_rxon_assoc_cmd rxon_assoc;
  1371. const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
  1372. const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
  1373. if ((rxon1->flags == rxon2->flags) &&
  1374. (rxon1->filter_flags == rxon2->filter_flags) &&
  1375. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  1376. (rxon1->ofdm_ht_single_stream_basic_rates ==
  1377. rxon2->ofdm_ht_single_stream_basic_rates) &&
  1378. (rxon1->ofdm_ht_dual_stream_basic_rates ==
  1379. rxon2->ofdm_ht_dual_stream_basic_rates) &&
  1380. (rxon1->rx_chain == rxon2->rx_chain) &&
  1381. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  1382. IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
  1383. return 0;
  1384. }
  1385. rxon_assoc.flags = priv->staging_rxon.flags;
  1386. rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
  1387. rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
  1388. rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
  1389. rxon_assoc.reserved = 0;
  1390. rxon_assoc.ofdm_ht_single_stream_basic_rates =
  1391. priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
  1392. rxon_assoc.ofdm_ht_dual_stream_basic_rates =
  1393. priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
  1394. rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
  1395. ret = iwl_send_cmd_pdu_async(priv, REPLY_RXON_ASSOC,
  1396. sizeof(rxon_assoc), &rxon_assoc, NULL);
  1397. if (ret)
  1398. return ret;
  1399. return ret;
  1400. }
  1401. int iwl4965_hw_channel_switch(struct iwl_priv *priv, u16 channel)
  1402. {
  1403. int rc;
  1404. u8 band = 0;
  1405. u8 is_fat = 0;
  1406. u8 ctrl_chan_high = 0;
  1407. struct iwl4965_channel_switch_cmd cmd = { 0 };
  1408. const struct iwl_channel_info *ch_info;
  1409. band = priv->band == IEEE80211_BAND_2GHZ;
  1410. ch_info = iwl_get_channel_info(priv, priv->band, channel);
  1411. is_fat = is_fat_channel(priv->staging_rxon.flags);
  1412. if (is_fat &&
  1413. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  1414. ctrl_chan_high = 1;
  1415. cmd.band = band;
  1416. cmd.expect_beacon = 0;
  1417. cmd.channel = cpu_to_le16(channel);
  1418. cmd.rxon_flags = priv->active_rxon.flags;
  1419. cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
  1420. cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
  1421. if (ch_info)
  1422. cmd.expect_beacon = is_channel_radar(ch_info);
  1423. else
  1424. cmd.expect_beacon = 1;
  1425. rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
  1426. ctrl_chan_high, &cmd.tx_power);
  1427. if (rc) {
  1428. IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
  1429. return rc;
  1430. }
  1431. rc = iwl_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
  1432. return rc;
  1433. }
  1434. static int iwl4965_shared_mem_rx_idx(struct iwl_priv *priv)
  1435. {
  1436. struct iwl4965_shared *s = priv->shared_virt;
  1437. return le32_to_cpu(s->rb_closed) & 0xFFF;
  1438. }
  1439. int iwl4965_hw_get_temperature(struct iwl_priv *priv)
  1440. {
  1441. return priv->temperature;
  1442. }
  1443. unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
  1444. struct iwl_frame *frame, u8 rate)
  1445. {
  1446. struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
  1447. unsigned int frame_size;
  1448. tx_beacon_cmd = &frame->u.beacon;
  1449. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  1450. tx_beacon_cmd->tx.sta_id = priv->hw_params.bcast_sta_id;
  1451. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1452. frame_size = iwl4965_fill_beacon_frame(priv,
  1453. tx_beacon_cmd->frame,
  1454. iwl_bcast_addr,
  1455. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  1456. BUG_ON(frame_size > MAX_MPDU_SIZE);
  1457. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  1458. if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
  1459. tx_beacon_cmd->tx.rate_n_flags =
  1460. iwl_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
  1461. else
  1462. tx_beacon_cmd->tx.rate_n_flags =
  1463. iwl_hw_set_rate_n_flags(rate, 0);
  1464. tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
  1465. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
  1466. return (sizeof(*tx_beacon_cmd) + frame_size);
  1467. }
  1468. static int iwl4965_alloc_shared_mem(struct iwl_priv *priv)
  1469. {
  1470. priv->shared_virt = pci_alloc_consistent(priv->pci_dev,
  1471. sizeof(struct iwl4965_shared),
  1472. &priv->shared_phys);
  1473. if (!priv->shared_virt)
  1474. return -ENOMEM;
  1475. memset(priv->shared_virt, 0, sizeof(struct iwl4965_shared));
  1476. priv->rb_closed_offset = offsetof(struct iwl4965_shared, rb_closed);
  1477. return 0;
  1478. }
  1479. static void iwl4965_free_shared_mem(struct iwl_priv *priv)
  1480. {
  1481. if (priv->shared_virt)
  1482. pci_free_consistent(priv->pci_dev,
  1483. sizeof(struct iwl4965_shared),
  1484. priv->shared_virt,
  1485. priv->shared_phys);
  1486. }
  1487. /**
  1488. * iwl4965_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  1489. */
  1490. static void iwl4965_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
  1491. struct iwl_tx_queue *txq,
  1492. u16 byte_cnt)
  1493. {
  1494. int len;
  1495. int txq_id = txq->q.id;
  1496. struct iwl4965_shared *shared_data = priv->shared_virt;
  1497. len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  1498. /* Set up byte count within first 256 entries */
  1499. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  1500. tfd_offset[txq->q.write_ptr], byte_cnt, len);
  1501. /* If within first 64 entries, duplicate at end */
  1502. if (txq->q.write_ptr < IWL49_MAX_WIN_SIZE)
  1503. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  1504. tfd_offset[IWL49_QUEUE_SIZE + txq->q.write_ptr],
  1505. byte_cnt, len);
  1506. }
  1507. /**
  1508. * sign_extend - Sign extend a value using specified bit as sign-bit
  1509. *
  1510. * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
  1511. * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
  1512. *
  1513. * @param oper value to sign extend
  1514. * @param index 0 based bit index (0<=index<32) to sign bit
  1515. */
  1516. static s32 sign_extend(u32 oper, int index)
  1517. {
  1518. u8 shift = 31 - index;
  1519. return (s32)(oper << shift) >> shift;
  1520. }
  1521. /**
  1522. * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
  1523. * @statistics: Provides the temperature reading from the uCode
  1524. *
  1525. * A return of <0 indicates bogus data in the statistics
  1526. */
  1527. int iwl4965_get_temperature(const struct iwl_priv *priv)
  1528. {
  1529. s32 temperature;
  1530. s32 vt;
  1531. s32 R1, R2, R3;
  1532. u32 R4;
  1533. if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
  1534. (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
  1535. IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
  1536. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  1537. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  1538. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  1539. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
  1540. } else {
  1541. IWL_DEBUG_TEMP("Running temperature calibration\n");
  1542. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  1543. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  1544. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  1545. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
  1546. }
  1547. /*
  1548. * Temperature is only 23 bits, so sign extend out to 32.
  1549. *
  1550. * NOTE If we haven't received a statistics notification yet
  1551. * with an updated temperature, use R4 provided to us in the
  1552. * "initialize" ALIVE response.
  1553. */
  1554. if (!test_bit(STATUS_TEMPERATURE, &priv->status))
  1555. vt = sign_extend(R4, 23);
  1556. else
  1557. vt = sign_extend(
  1558. le32_to_cpu(priv->statistics.general.temperature), 23);
  1559. IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
  1560. R1, R2, R3, vt);
  1561. if (R3 == R1) {
  1562. IWL_ERROR("Calibration conflict R1 == R3\n");
  1563. return -1;
  1564. }
  1565. /* Calculate temperature in degrees Kelvin, adjust by 97%.
  1566. * Add offset to center the adjustment around 0 degrees Centigrade. */
  1567. temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
  1568. temperature /= (R3 - R1);
  1569. temperature = (temperature * 97) / 100 +
  1570. TEMPERATURE_CALIB_KELVIN_OFFSET;
  1571. IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
  1572. KELVIN_TO_CELSIUS(temperature));
  1573. return temperature;
  1574. }
  1575. /* Adjust Txpower only if temperature variance is greater than threshold. */
  1576. #define IWL_TEMPERATURE_THRESHOLD 3
  1577. /**
  1578. * iwl4965_is_temp_calib_needed - determines if new calibration is needed
  1579. *
  1580. * If the temperature changed has changed sufficiently, then a recalibration
  1581. * is needed.
  1582. *
  1583. * Assumes caller will replace priv->last_temperature once calibration
  1584. * executed.
  1585. */
  1586. static int iwl4965_is_temp_calib_needed(struct iwl_priv *priv)
  1587. {
  1588. int temp_diff;
  1589. if (!test_bit(STATUS_STATISTICS, &priv->status)) {
  1590. IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
  1591. return 0;
  1592. }
  1593. temp_diff = priv->temperature - priv->last_temperature;
  1594. /* get absolute value */
  1595. if (temp_diff < 0) {
  1596. IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
  1597. temp_diff = -temp_diff;
  1598. } else if (temp_diff == 0)
  1599. IWL_DEBUG_POWER("Same temp, \n");
  1600. else
  1601. IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
  1602. if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
  1603. IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
  1604. return 0;
  1605. }
  1606. IWL_DEBUG_POWER("Thermal txpower calib needed\n");
  1607. return 1;
  1608. }
  1609. /* Calculate noise level, based on measurements during network silence just
  1610. * before arriving beacon. This measurement can be done only if we know
  1611. * exactly when to expect beacons, therefore only when we're associated. */
  1612. static void iwl4965_rx_calc_noise(struct iwl_priv *priv)
  1613. {
  1614. struct statistics_rx_non_phy *rx_info
  1615. = &(priv->statistics.rx.general);
  1616. int num_active_rx = 0;
  1617. int total_silence = 0;
  1618. int bcn_silence_a =
  1619. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  1620. int bcn_silence_b =
  1621. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  1622. int bcn_silence_c =
  1623. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  1624. if (bcn_silence_a) {
  1625. total_silence += bcn_silence_a;
  1626. num_active_rx++;
  1627. }
  1628. if (bcn_silence_b) {
  1629. total_silence += bcn_silence_b;
  1630. num_active_rx++;
  1631. }
  1632. if (bcn_silence_c) {
  1633. total_silence += bcn_silence_c;
  1634. num_active_rx++;
  1635. }
  1636. /* Average among active antennas */
  1637. if (num_active_rx)
  1638. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  1639. else
  1640. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  1641. IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  1642. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  1643. priv->last_rx_noise);
  1644. }
  1645. void iwl4965_hw_rx_statistics(struct iwl_priv *priv,
  1646. struct iwl_rx_mem_buffer *rxb)
  1647. {
  1648. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1649. int change;
  1650. s32 temp;
  1651. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  1652. (int)sizeof(priv->statistics), pkt->len);
  1653. change = ((priv->statistics.general.temperature !=
  1654. pkt->u.stats.general.temperature) ||
  1655. ((priv->statistics.flag &
  1656. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  1657. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  1658. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  1659. set_bit(STATUS_STATISTICS, &priv->status);
  1660. /* Reschedule the statistics timer to occur in
  1661. * REG_RECALIB_PERIOD seconds to ensure we get a
  1662. * thermal update even if the uCode doesn't give
  1663. * us one */
  1664. mod_timer(&priv->statistics_periodic, jiffies +
  1665. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  1666. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  1667. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  1668. iwl4965_rx_calc_noise(priv);
  1669. queue_work(priv->workqueue, &priv->run_time_calib_work);
  1670. }
  1671. iwl_leds_background(priv);
  1672. /* If the hardware hasn't reported a change in
  1673. * temperature then don't bother computing a
  1674. * calibrated temperature value */
  1675. if (!change)
  1676. return;
  1677. temp = iwl4965_get_temperature(priv);
  1678. if (temp < 0)
  1679. return;
  1680. if (priv->temperature != temp) {
  1681. if (priv->temperature)
  1682. IWL_DEBUG_TEMP("Temperature changed "
  1683. "from %dC to %dC\n",
  1684. KELVIN_TO_CELSIUS(priv->temperature),
  1685. KELVIN_TO_CELSIUS(temp));
  1686. else
  1687. IWL_DEBUG_TEMP("Temperature "
  1688. "initialized to %dC\n",
  1689. KELVIN_TO_CELSIUS(temp));
  1690. }
  1691. priv->temperature = temp;
  1692. set_bit(STATUS_TEMPERATURE, &priv->status);
  1693. if (!priv->disable_tx_power_cal &&
  1694. unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  1695. iwl4965_is_temp_calib_needed(priv))
  1696. queue_work(priv->workqueue, &priv->txpower_work);
  1697. }
  1698. static void iwl4965_add_radiotap(struct iwl_priv *priv,
  1699. struct sk_buff *skb,
  1700. struct iwl4965_rx_phy_res *rx_start,
  1701. struct ieee80211_rx_status *stats,
  1702. u32 ampdu_status)
  1703. {
  1704. s8 signal = stats->signal;
  1705. s8 noise = 0;
  1706. int rate = stats->rate_idx;
  1707. u64 tsf = stats->mactime;
  1708. __le16 antenna;
  1709. __le16 phy_flags_hw = rx_start->phy_flags;
  1710. struct iwl4965_rt_rx_hdr {
  1711. struct ieee80211_radiotap_header rt_hdr;
  1712. __le64 rt_tsf; /* TSF */
  1713. u8 rt_flags; /* radiotap packet flags */
  1714. u8 rt_rate; /* rate in 500kb/s */
  1715. __le16 rt_channelMHz; /* channel in MHz */
  1716. __le16 rt_chbitmask; /* channel bitfield */
  1717. s8 rt_dbmsignal; /* signal in dBm, kluged to signed */
  1718. s8 rt_dbmnoise;
  1719. u8 rt_antenna; /* antenna number */
  1720. } __attribute__ ((packed)) *iwl4965_rt;
  1721. /* TODO: We won't have enough headroom for HT frames. Fix it later. */
  1722. if (skb_headroom(skb) < sizeof(*iwl4965_rt)) {
  1723. if (net_ratelimit())
  1724. printk(KERN_ERR "not enough headroom [%d] for "
  1725. "radiotap head [%zd]\n",
  1726. skb_headroom(skb), sizeof(*iwl4965_rt));
  1727. return;
  1728. }
  1729. /* put radiotap header in front of 802.11 header and data */
  1730. iwl4965_rt = (void *)skb_push(skb, sizeof(*iwl4965_rt));
  1731. /* initialise radiotap header */
  1732. iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  1733. iwl4965_rt->rt_hdr.it_pad = 0;
  1734. /* total header + data */
  1735. put_unaligned(cpu_to_le16(sizeof(*iwl4965_rt)),
  1736. &iwl4965_rt->rt_hdr.it_len);
  1737. /* Indicate all the fields we add to the radiotap header */
  1738. put_unaligned(cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
  1739. (1 << IEEE80211_RADIOTAP_FLAGS) |
  1740. (1 << IEEE80211_RADIOTAP_RATE) |
  1741. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  1742. (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
  1743. (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
  1744. (1 << IEEE80211_RADIOTAP_ANTENNA)),
  1745. &iwl4965_rt->rt_hdr.it_present);
  1746. /* Zero the flags, we'll add to them as we go */
  1747. iwl4965_rt->rt_flags = 0;
  1748. put_unaligned(cpu_to_le64(tsf), &iwl4965_rt->rt_tsf);
  1749. iwl4965_rt->rt_dbmsignal = signal;
  1750. iwl4965_rt->rt_dbmnoise = noise;
  1751. /* Convert the channel frequency and set the flags */
  1752. put_unaligned(cpu_to_le16(stats->freq), &iwl4965_rt->rt_channelMHz);
  1753. if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
  1754. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  1755. IEEE80211_CHAN_5GHZ),
  1756. &iwl4965_rt->rt_chbitmask);
  1757. else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
  1758. put_unaligned(cpu_to_le16(IEEE80211_CHAN_CCK |
  1759. IEEE80211_CHAN_2GHZ),
  1760. &iwl4965_rt->rt_chbitmask);
  1761. else /* 802.11g */
  1762. put_unaligned(cpu_to_le16(IEEE80211_CHAN_OFDM |
  1763. IEEE80211_CHAN_2GHZ),
  1764. &iwl4965_rt->rt_chbitmask);
  1765. if (rate == -1)
  1766. iwl4965_rt->rt_rate = 0;
  1767. else
  1768. iwl4965_rt->rt_rate = iwl_rates[rate].ieee;
  1769. /*
  1770. * "antenna number"
  1771. *
  1772. * It seems that the antenna field in the phy flags value
  1773. * is actually a bitfield. This is undefined by radiotap,
  1774. * it wants an actual antenna number but I always get "7"
  1775. * for most legacy frames I receive indicating that the
  1776. * same frame was received on all three RX chains.
  1777. *
  1778. * I think this field should be removed in favour of a
  1779. * new 802.11n radiotap field "RX chains" that is defined
  1780. * as a bitmask.
  1781. */
  1782. antenna = phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK;
  1783. iwl4965_rt->rt_antenna = le16_to_cpu(antenna) >> 4;
  1784. /* set the preamble flag if appropriate */
  1785. if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  1786. iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
  1787. stats->flag |= RX_FLAG_RADIOTAP;
  1788. }
  1789. static void iwl_update_rx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  1790. {
  1791. /* 0 - mgmt, 1 - cnt, 2 - data */
  1792. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  1793. priv->rx_stats[idx].cnt++;
  1794. priv->rx_stats[idx].bytes += len;
  1795. }
  1796. /*
  1797. * returns non-zero if packet should be dropped
  1798. */
  1799. static int iwl4965_set_decrypted_flag(struct iwl_priv *priv,
  1800. struct ieee80211_hdr *hdr,
  1801. u32 decrypt_res,
  1802. struct ieee80211_rx_status *stats)
  1803. {
  1804. u16 fc = le16_to_cpu(hdr->frame_control);
  1805. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  1806. return 0;
  1807. if (!(fc & IEEE80211_FCTL_PROTECTED))
  1808. return 0;
  1809. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  1810. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  1811. case RX_RES_STATUS_SEC_TYPE_TKIP:
  1812. /* The uCode has got a bad phase 1 Key, pushes the packet.
  1813. * Decryption will be done in SW. */
  1814. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  1815. RX_RES_STATUS_BAD_KEY_TTAK)
  1816. break;
  1817. case RX_RES_STATUS_SEC_TYPE_WEP:
  1818. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  1819. RX_RES_STATUS_BAD_ICV_MIC) {
  1820. /* bad ICV, the packet is destroyed since the
  1821. * decryption is inplace, drop it */
  1822. IWL_DEBUG_RX("Packet destroyed\n");
  1823. return -1;
  1824. }
  1825. case RX_RES_STATUS_SEC_TYPE_CCMP:
  1826. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  1827. RX_RES_STATUS_DECRYPT_OK) {
  1828. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  1829. stats->flag |= RX_FLAG_DECRYPTED;
  1830. }
  1831. break;
  1832. default:
  1833. break;
  1834. }
  1835. return 0;
  1836. }
  1837. static u32 iwl4965_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  1838. {
  1839. u32 decrypt_out = 0;
  1840. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  1841. RX_RES_STATUS_STATION_FOUND)
  1842. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  1843. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  1844. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  1845. /* packet was not encrypted */
  1846. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  1847. RX_RES_STATUS_SEC_TYPE_NONE)
  1848. return decrypt_out;
  1849. /* packet was encrypted with unknown alg */
  1850. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  1851. RX_RES_STATUS_SEC_TYPE_ERR)
  1852. return decrypt_out;
  1853. /* decryption was not done in HW */
  1854. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  1855. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  1856. return decrypt_out;
  1857. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  1858. case RX_RES_STATUS_SEC_TYPE_CCMP:
  1859. /* alg is CCM: check MIC only */
  1860. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  1861. /* Bad MIC */
  1862. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  1863. else
  1864. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  1865. break;
  1866. case RX_RES_STATUS_SEC_TYPE_TKIP:
  1867. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  1868. /* Bad TTAK */
  1869. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  1870. break;
  1871. }
  1872. /* fall through if TTAK OK */
  1873. default:
  1874. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  1875. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  1876. else
  1877. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  1878. break;
  1879. };
  1880. IWL_DEBUG_RX("decrypt_in:0x%x decrypt_out = 0x%x\n",
  1881. decrypt_in, decrypt_out);
  1882. return decrypt_out;
  1883. }
  1884. static void iwl4965_handle_data_packet(struct iwl_priv *priv, int is_data,
  1885. int include_phy,
  1886. struct iwl_rx_mem_buffer *rxb,
  1887. struct ieee80211_rx_status *stats)
  1888. {
  1889. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1890. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  1891. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  1892. struct ieee80211_hdr *hdr;
  1893. u16 len;
  1894. __le32 *rx_end;
  1895. unsigned int skblen;
  1896. u32 ampdu_status;
  1897. u32 ampdu_status_legacy;
  1898. if (!include_phy && priv->last_phy_res[0])
  1899. rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  1900. if (!rx_start) {
  1901. IWL_ERROR("MPDU frame without a PHY data\n");
  1902. return;
  1903. }
  1904. if (include_phy) {
  1905. hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
  1906. rx_start->cfg_phy_cnt);
  1907. len = le16_to_cpu(rx_start->byte_count);
  1908. rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
  1909. sizeof(struct iwl4965_rx_phy_res) +
  1910. rx_start->cfg_phy_cnt + len);
  1911. } else {
  1912. struct iwl4965_rx_mpdu_res_start *amsdu =
  1913. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  1914. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  1915. sizeof(struct iwl4965_rx_mpdu_res_start));
  1916. len = le16_to_cpu(amsdu->byte_count);
  1917. rx_start->byte_count = amsdu->byte_count;
  1918. rx_end = (__le32 *) (((u8 *) hdr) + len);
  1919. }
  1920. /* In monitor mode allow 802.11 ACk frames (10 bytes) */
  1921. if (len > priv->hw_params.max_pkt_size ||
  1922. len < ((priv->iw_mode == IEEE80211_IF_TYPE_MNTR) ? 10 : 16)) {
  1923. IWL_WARNING("byte count out of range [16,4K] : %d\n", len);
  1924. return;
  1925. }
  1926. ampdu_status = le32_to_cpu(*rx_end);
  1927. skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
  1928. if (!include_phy) {
  1929. /* New status scheme, need to translate */
  1930. ampdu_status_legacy = ampdu_status;
  1931. ampdu_status = iwl4965_translate_rx_status(priv, ampdu_status);
  1932. }
  1933. /* start from MAC */
  1934. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  1935. skb_put(rxb->skb, len); /* end where data ends */
  1936. /* We only process data packets if the interface is open */
  1937. if (unlikely(!priv->is_open)) {
  1938. IWL_DEBUG_DROP_LIMIT
  1939. ("Dropping packet while interface is not open.\n");
  1940. return;
  1941. }
  1942. stats->flag = 0;
  1943. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  1944. /* in case of HW accelerated crypto and bad decryption, drop */
  1945. if (!priv->hw_params.sw_crypto &&
  1946. iwl4965_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  1947. return;
  1948. if (priv->add_radiotap)
  1949. iwl4965_add_radiotap(priv, rxb->skb, rx_start, stats, ampdu_status);
  1950. iwl_update_rx_stats(priv, le16_to_cpu(hdr->frame_control), len);
  1951. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  1952. priv->alloc_rxb_skb--;
  1953. rxb->skb = NULL;
  1954. }
  1955. /* Calc max signal level (dBm) among 3 possible receivers */
  1956. static int iwl4965_calc_rssi(struct iwl_priv *priv,
  1957. struct iwl4965_rx_phy_res *rx_resp)
  1958. {
  1959. /* data from PHY/DSP regarding signal strength, etc.,
  1960. * contents are always there, not configurable by host. */
  1961. struct iwl4965_rx_non_cfg_phy *ncphy =
  1962. (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
  1963. u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
  1964. >> IWL_AGC_DB_POS;
  1965. u32 valid_antennae =
  1966. (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
  1967. >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
  1968. u8 max_rssi = 0;
  1969. u32 i;
  1970. /* Find max rssi among 3 possible receivers.
  1971. * These values are measured by the digital signal processor (DSP).
  1972. * They should stay fairly constant even as the signal strength varies,
  1973. * if the radio's automatic gain control (AGC) is working right.
  1974. * AGC value (see below) will provide the "interesting" info. */
  1975. for (i = 0; i < 3; i++)
  1976. if (valid_antennae & (1 << i))
  1977. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  1978. IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  1979. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  1980. max_rssi, agc);
  1981. /* dBm = max_rssi dB - agc dB - constant.
  1982. * Higher AGC (higher radio gain) means lower signal. */
  1983. return (max_rssi - agc - IWL_RSSI_OFFSET);
  1984. }
  1985. static void iwl4965_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
  1986. {
  1987. unsigned long flags;
  1988. spin_lock_irqsave(&priv->sta_lock, flags);
  1989. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  1990. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1991. priv->stations[sta_id].sta.sta.modify_mask = 0;
  1992. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1993. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1994. iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  1995. }
  1996. static void iwl4965_update_ps_mode(struct iwl_priv *priv, u16 ps_bit, u8 *addr)
  1997. {
  1998. /* FIXME: need locking over ps_status ??? */
  1999. u8 sta_id = iwl_find_station(priv, addr);
  2000. if (sta_id != IWL_INVALID_STATION) {
  2001. u8 sta_awake = priv->stations[sta_id].
  2002. ps_status == STA_PS_STATUS_WAKE;
  2003. if (sta_awake && ps_bit)
  2004. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  2005. else if (!sta_awake && !ps_bit) {
  2006. iwl4965_sta_modify_ps_wake(priv, sta_id);
  2007. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  2008. }
  2009. }
  2010. }
  2011. #ifdef CONFIG_IWLWIFI_DEBUG
  2012. /**
  2013. * iwl4965_dbg_report_frame - dump frame to syslog during debug sessions
  2014. *
  2015. * You may hack this function to show different aspects of received frames,
  2016. * including selective frame dumps.
  2017. * group100 parameter selects whether to show 1 out of 100 good frames.
  2018. *
  2019. * TODO: This was originally written for 3945, need to audit for
  2020. * proper operation with 4965.
  2021. */
  2022. static void iwl4965_dbg_report_frame(struct iwl_priv *priv,
  2023. struct iwl_rx_packet *pkt,
  2024. struct ieee80211_hdr *header, int group100)
  2025. {
  2026. u32 to_us;
  2027. u32 print_summary = 0;
  2028. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  2029. u32 hundred = 0;
  2030. u32 dataframe = 0;
  2031. u16 fc;
  2032. u16 seq_ctl;
  2033. u16 channel;
  2034. u16 phy_flags;
  2035. int rate_sym;
  2036. u16 length;
  2037. u16 status;
  2038. u16 bcn_tmr;
  2039. u32 tsf_low;
  2040. u64 tsf;
  2041. u8 rssi;
  2042. u8 agc;
  2043. u16 sig_avg;
  2044. u16 noise_diff;
  2045. struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  2046. struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  2047. struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
  2048. u8 *data = IWL_RX_DATA(pkt);
  2049. if (likely(!(priv->debug_level & IWL_DL_RX)))
  2050. return;
  2051. /* MAC header */
  2052. fc = le16_to_cpu(header->frame_control);
  2053. seq_ctl = le16_to_cpu(header->seq_ctrl);
  2054. /* metadata */
  2055. channel = le16_to_cpu(rx_hdr->channel);
  2056. phy_flags = le16_to_cpu(rx_hdr->phy_flags);
  2057. rate_sym = rx_hdr->rate;
  2058. length = le16_to_cpu(rx_hdr->len);
  2059. /* end-of-frame status and timestamp */
  2060. status = le32_to_cpu(rx_end->status);
  2061. bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
  2062. tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
  2063. tsf = le64_to_cpu(rx_end->timestamp);
  2064. /* signal statistics */
  2065. rssi = rx_stats->rssi;
  2066. agc = rx_stats->agc;
  2067. sig_avg = le16_to_cpu(rx_stats->sig_avg);
  2068. noise_diff = le16_to_cpu(rx_stats->noise_diff);
  2069. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  2070. /* if data frame is to us and all is good,
  2071. * (optionally) print summary for only 1 out of every 100 */
  2072. if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
  2073. (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  2074. dataframe = 1;
  2075. if (!group100)
  2076. print_summary = 1; /* print each frame */
  2077. else if (priv->framecnt_to_us < 100) {
  2078. priv->framecnt_to_us++;
  2079. print_summary = 0;
  2080. } else {
  2081. priv->framecnt_to_us = 0;
  2082. print_summary = 1;
  2083. hundred = 1;
  2084. }
  2085. } else {
  2086. /* print summary for all other frames */
  2087. print_summary = 1;
  2088. }
  2089. if (print_summary) {
  2090. char *title;
  2091. int rate_idx;
  2092. u32 bitrate;
  2093. if (hundred)
  2094. title = "100Frames";
  2095. else if (fc & IEEE80211_FCTL_RETRY)
  2096. title = "Retry";
  2097. else if (ieee80211_is_assoc_response(fc))
  2098. title = "AscRsp";
  2099. else if (ieee80211_is_reassoc_response(fc))
  2100. title = "RasRsp";
  2101. else if (ieee80211_is_probe_response(fc)) {
  2102. title = "PrbRsp";
  2103. print_dump = 1; /* dump frame contents */
  2104. } else if (ieee80211_is_beacon(fc)) {
  2105. title = "Beacon";
  2106. print_dump = 1; /* dump frame contents */
  2107. } else if (ieee80211_is_atim(fc))
  2108. title = "ATIM";
  2109. else if (ieee80211_is_auth(fc))
  2110. title = "Auth";
  2111. else if (ieee80211_is_deauth(fc))
  2112. title = "DeAuth";
  2113. else if (ieee80211_is_disassoc(fc))
  2114. title = "DisAssoc";
  2115. else
  2116. title = "Frame";
  2117. rate_idx = iwl_hwrate_to_plcp_idx(rate_sym);
  2118. if (unlikely(rate_idx == -1))
  2119. bitrate = 0;
  2120. else
  2121. bitrate = iwl_rates[rate_idx].ieee / 2;
  2122. /* print frame summary.
  2123. * MAC addresses show just the last byte (for brevity),
  2124. * but you can hack it to show more, if you'd like to. */
  2125. if (dataframe)
  2126. IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
  2127. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  2128. title, fc, header->addr1[5],
  2129. length, rssi, channel, bitrate);
  2130. else {
  2131. /* src/dst addresses assume managed mode */
  2132. IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
  2133. "src=0x%02x, rssi=%u, tim=%lu usec, "
  2134. "phy=0x%02x, chnl=%d\n",
  2135. title, fc, header->addr1[5],
  2136. header->addr3[5], rssi,
  2137. tsf_low - priv->scan_start_tsf,
  2138. phy_flags, channel);
  2139. }
  2140. }
  2141. if (print_dump)
  2142. iwl_print_hex_dump(priv, IWL_DL_RX, data, length);
  2143. }
  2144. #else
  2145. static inline void iwl4965_dbg_report_frame(struct iwl_priv *priv,
  2146. struct iwl_rx_packet *pkt,
  2147. struct ieee80211_hdr *header,
  2148. int group100)
  2149. {
  2150. }
  2151. #endif
  2152. /* Called for REPLY_RX (legacy ABG frames), or
  2153. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  2154. void iwl4965_rx_reply_rx(struct iwl_priv *priv,
  2155. struct iwl_rx_mem_buffer *rxb)
  2156. {
  2157. struct ieee80211_hdr *header;
  2158. struct ieee80211_rx_status rx_status;
  2159. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  2160. /* Use phy data (Rx signal strength, etc.) contained within
  2161. * this rx packet for legacy frames,
  2162. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  2163. int include_phy = (pkt->hdr.cmd == REPLY_RX);
  2164. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  2165. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
  2166. (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  2167. __le32 *rx_end;
  2168. unsigned int len = 0;
  2169. u16 fc;
  2170. u8 network_packet;
  2171. rx_status.mactime = le64_to_cpu(rx_start->timestamp);
  2172. rx_status.freq =
  2173. ieee80211_channel_to_frequency(le16_to_cpu(rx_start->channel));
  2174. rx_status.band = (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  2175. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  2176. rx_status.rate_idx =
  2177. iwl_hwrate_to_plcp_idx(le32_to_cpu(rx_start->rate_n_flags));
  2178. if (rx_status.band == IEEE80211_BAND_5GHZ)
  2179. rx_status.rate_idx -= IWL_FIRST_OFDM_RATE;
  2180. rx_status.antenna = 0;
  2181. rx_status.flag = 0;
  2182. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  2183. IWL_DEBUG_DROP("dsp size out of range [0,20]: %d/n",
  2184. rx_start->cfg_phy_cnt);
  2185. return;
  2186. }
  2187. if (!include_phy) {
  2188. if (priv->last_phy_res[0])
  2189. rx_start = (struct iwl4965_rx_phy_res *)
  2190. &priv->last_phy_res[1];
  2191. else
  2192. rx_start = NULL;
  2193. }
  2194. if (!rx_start) {
  2195. IWL_ERROR("MPDU frame without a PHY data\n");
  2196. return;
  2197. }
  2198. if (include_phy) {
  2199. header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
  2200. + rx_start->cfg_phy_cnt);
  2201. len = le16_to_cpu(rx_start->byte_count);
  2202. rx_end = (__le32 *)(pkt->u.raw + rx_start->cfg_phy_cnt +
  2203. sizeof(struct iwl4965_rx_phy_res) + len);
  2204. } else {
  2205. struct iwl4965_rx_mpdu_res_start *amsdu =
  2206. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  2207. header = (void *)(pkt->u.raw +
  2208. sizeof(struct iwl4965_rx_mpdu_res_start));
  2209. len = le16_to_cpu(amsdu->byte_count);
  2210. rx_end = (__le32 *) (pkt->u.raw +
  2211. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  2212. }
  2213. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  2214. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  2215. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  2216. le32_to_cpu(*rx_end));
  2217. return;
  2218. }
  2219. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  2220. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  2221. rx_status.signal = iwl4965_calc_rssi(priv, rx_start);
  2222. /* Meaningful noise values are available only from beacon statistics,
  2223. * which are gathered only when associated, and indicate noise
  2224. * only for the associated network channel ...
  2225. * Ignore these noise values while scanning (other channels) */
  2226. if (iwl_is_associated(priv) &&
  2227. !test_bit(STATUS_SCANNING, &priv->status)) {
  2228. rx_status.noise = priv->last_rx_noise;
  2229. rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal,
  2230. rx_status.noise);
  2231. } else {
  2232. rx_status.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  2233. rx_status.qual = iwl4965_calc_sig_qual(rx_status.signal, 0);
  2234. }
  2235. /* Reset beacon noise level if not associated. */
  2236. if (!iwl_is_associated(priv))
  2237. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  2238. /* Set "1" to report good data frames in groups of 100 */
  2239. /* FIXME: need to optimze the call: */
  2240. iwl4965_dbg_report_frame(priv, pkt, header, 1);
  2241. IWL_DEBUG_STATS_LIMIT("Rssi %d, noise %d, qual %d, TSF %llu\n",
  2242. rx_status.signal, rx_status.noise, rx_status.signal,
  2243. (unsigned long long)rx_status.mactime);
  2244. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  2245. iwl4965_handle_data_packet(priv, 1, include_phy,
  2246. rxb, &rx_status);
  2247. return;
  2248. }
  2249. network_packet = iwl4965_is_network_packet(priv, header);
  2250. if (network_packet) {
  2251. priv->last_rx_rssi = rx_status.signal;
  2252. priv->last_beacon_time = priv->ucode_beacon_time;
  2253. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  2254. }
  2255. fc = le16_to_cpu(header->frame_control);
  2256. switch (fc & IEEE80211_FCTL_FTYPE) {
  2257. case IEEE80211_FTYPE_MGMT:
  2258. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  2259. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  2260. header->addr2);
  2261. iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &rx_status);
  2262. break;
  2263. case IEEE80211_FTYPE_CTL:
  2264. switch (fc & IEEE80211_FCTL_STYPE) {
  2265. case IEEE80211_STYPE_BACK_REQ:
  2266. IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
  2267. iwl4965_handle_data_packet(priv, 0, include_phy,
  2268. rxb, &rx_status);
  2269. break;
  2270. default:
  2271. break;
  2272. }
  2273. break;
  2274. case IEEE80211_FTYPE_DATA: {
  2275. DECLARE_MAC_BUF(mac1);
  2276. DECLARE_MAC_BUF(mac2);
  2277. DECLARE_MAC_BUF(mac3);
  2278. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  2279. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  2280. header->addr2);
  2281. if (unlikely(!network_packet))
  2282. IWL_DEBUG_DROP("Dropping (non network): "
  2283. "%s, %s, %s\n",
  2284. print_mac(mac1, header->addr1),
  2285. print_mac(mac2, header->addr2),
  2286. print_mac(mac3, header->addr3));
  2287. else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
  2288. IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
  2289. print_mac(mac1, header->addr1),
  2290. print_mac(mac2, header->addr2),
  2291. print_mac(mac3, header->addr3));
  2292. else
  2293. iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
  2294. &rx_status);
  2295. break;
  2296. }
  2297. default:
  2298. break;
  2299. }
  2300. }
  2301. /**
  2302. * iwl4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  2303. *
  2304. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  2305. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  2306. */
  2307. static int iwl4965_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  2308. struct iwl_ht_agg *agg,
  2309. struct iwl4965_compressed_ba_resp*
  2310. ba_resp)
  2311. {
  2312. int i, sh, ack;
  2313. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  2314. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2315. u64 bitmap;
  2316. int successes = 0;
  2317. struct ieee80211_tx_info *info;
  2318. if (unlikely(!agg->wait_for_ba)) {
  2319. IWL_ERROR("Received BA when not expected\n");
  2320. return -EINVAL;
  2321. }
  2322. /* Mark that the expected block-ack response arrived */
  2323. agg->wait_for_ba = 0;
  2324. IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  2325. /* Calculate shift to align block-ack bits with our Tx window bits */
  2326. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
  2327. if (sh < 0) /* tbw something is wrong with indices */
  2328. sh += 0x100;
  2329. /* don't use 64-bit values for now */
  2330. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  2331. if (agg->frame_count > (64 - sh)) {
  2332. IWL_DEBUG_TX_REPLY("more frames than bitmap size");
  2333. return -1;
  2334. }
  2335. /* check for success or failure according to the
  2336. * transmitted bitmap and block-ack bitmap */
  2337. bitmap &= agg->bitmap;
  2338. /* For each frame attempted in aggregation,
  2339. * update driver's record of tx frame's status. */
  2340. for (i = 0; i < agg->frame_count ; i++) {
  2341. ack = bitmap & (1 << i);
  2342. successes += !!ack;
  2343. IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  2344. ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
  2345. agg->start_idx + i);
  2346. }
  2347. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
  2348. memset(&info->status, 0, sizeof(info->status));
  2349. info->flags = IEEE80211_TX_STAT_ACK;
  2350. info->flags |= IEEE80211_TX_STAT_AMPDU;
  2351. info->status.ampdu_ack_map = successes;
  2352. info->status.ampdu_ack_len = agg->frame_count;
  2353. iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  2354. IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
  2355. return 0;
  2356. }
  2357. /**
  2358. * iwl4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  2359. */
  2360. static void iwl4965_tx_queue_stop_scheduler(struct iwl_priv *priv,
  2361. u16 txq_id)
  2362. {
  2363. /* Simply stop the queue, but don't change any configuration;
  2364. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  2365. iwl_write_prph(priv,
  2366. IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
  2367. (0 << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  2368. (1 << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  2369. }
  2370. /**
  2371. * txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE
  2372. * priv->lock must be held by the caller
  2373. */
  2374. static int iwl4965_txq_agg_disable(struct iwl_priv *priv, u16 txq_id,
  2375. u16 ssn_idx, u8 tx_fifo)
  2376. {
  2377. int ret = 0;
  2378. if (IWL49_FIRST_AMPDU_QUEUE > txq_id) {
  2379. IWL_WARNING("queue number too small: %d, must be > %d\n",
  2380. txq_id, IWL49_FIRST_AMPDU_QUEUE);
  2381. return -EINVAL;
  2382. }
  2383. ret = iwl_grab_nic_access(priv);
  2384. if (ret)
  2385. return ret;
  2386. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  2387. iwl_clear_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  2388. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  2389. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  2390. /* supposes that ssn_idx is valid (!= 0xFFF) */
  2391. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  2392. iwl_clear_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  2393. iwl_txq_ctx_deactivate(priv, txq_id);
  2394. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  2395. iwl_release_nic_access(priv);
  2396. return 0;
  2397. }
  2398. /**
  2399. * iwl4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  2400. *
  2401. * Handles block-acknowledge notification from device, which reports success
  2402. * of frames sent via aggregation.
  2403. */
  2404. static void iwl4965_rx_reply_compressed_ba(struct iwl_priv *priv,
  2405. struct iwl_rx_mem_buffer *rxb)
  2406. {
  2407. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  2408. struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  2409. int index;
  2410. struct iwl_tx_queue *txq = NULL;
  2411. struct iwl_ht_agg *agg;
  2412. DECLARE_MAC_BUF(mac);
  2413. /* "flow" corresponds to Tx queue */
  2414. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2415. /* "ssn" is start of block-ack Tx window, corresponds to index
  2416. * (in Tx queue's circular buffer) of first TFD/frame in window */
  2417. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  2418. if (scd_flow >= priv->hw_params.max_txq_num) {
  2419. IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
  2420. return;
  2421. }
  2422. txq = &priv->txq[scd_flow];
  2423. agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
  2424. /* Find index just before block-ack window */
  2425. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  2426. /* TODO: Need to get this copy more safely - now good for debug */
  2427. IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
  2428. "sta_id = %d\n",
  2429. agg->wait_for_ba,
  2430. print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
  2431. ba_resp->sta_id);
  2432. IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  2433. "%d, scd_ssn = %d\n",
  2434. ba_resp->tid,
  2435. ba_resp->seq_ctl,
  2436. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  2437. ba_resp->scd_flow,
  2438. ba_resp->scd_ssn);
  2439. IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
  2440. agg->start_idx,
  2441. (unsigned long long)agg->bitmap);
  2442. /* Update driver's record of ACK vs. not for each frame in window */
  2443. iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  2444. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  2445. * block-ack window (we assume that they've been successfully
  2446. * transmitted ... if not, it's too late anyway). */
  2447. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  2448. /* calculate mac80211 ampdu sw queue to wake */
  2449. int ampdu_q =
  2450. scd_flow - priv->hw_params.first_ampdu_q + priv->hw->queues;
  2451. int freed = iwl_tx_queue_reclaim(priv, scd_flow, index);
  2452. priv->stations[ba_resp->sta_id].
  2453. tid[ba_resp->tid].tfds_in_queue -= freed;
  2454. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  2455. priv->mac80211_registered &&
  2456. agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
  2457. ieee80211_wake_queue(priv->hw, ampdu_q);
  2458. iwl_txq_check_empty(priv, ba_resp->sta_id,
  2459. ba_resp->tid, scd_flow);
  2460. }
  2461. }
  2462. /**
  2463. * iwl4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  2464. */
  2465. static int iwl4965_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
  2466. u16 txq_id)
  2467. {
  2468. u32 tbl_dw_addr;
  2469. u32 tbl_dw;
  2470. u16 scd_q2ratid;
  2471. scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  2472. tbl_dw_addr = priv->scd_base_addr +
  2473. IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  2474. tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
  2475. if (txq_id & 0x1)
  2476. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  2477. else
  2478. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  2479. iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  2480. return 0;
  2481. }
  2482. /**
  2483. * iwl4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  2484. *
  2485. * NOTE: txq_id must be greater than IWL49_FIRST_AMPDU_QUEUE,
  2486. * i.e. it must be one of the higher queues used for aggregation
  2487. */
  2488. static int iwl4965_txq_agg_enable(struct iwl_priv *priv, int txq_id,
  2489. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  2490. {
  2491. unsigned long flags;
  2492. int ret;
  2493. u16 ra_tid;
  2494. if (IWL49_FIRST_AMPDU_QUEUE > txq_id)
  2495. IWL_WARNING("queue number too small: %d, must be > %d\n",
  2496. txq_id, IWL49_FIRST_AMPDU_QUEUE);
  2497. ra_tid = BUILD_RAxTID(sta_id, tid);
  2498. /* Modify device's station table to Tx this TID */
  2499. iwl_sta_modify_enable_tid_tx(priv, sta_id, tid);
  2500. spin_lock_irqsave(&priv->lock, flags);
  2501. ret = iwl_grab_nic_access(priv);
  2502. if (ret) {
  2503. spin_unlock_irqrestore(&priv->lock, flags);
  2504. return ret;
  2505. }
  2506. /* Stop this Tx queue before configuring it */
  2507. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  2508. /* Map receiver-address / traffic-ID to this queue */
  2509. iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  2510. /* Set this queue as a chain-building queue */
  2511. iwl_set_bits_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  2512. /* Place first TFD at index corresponding to start sequence number.
  2513. * Assumes that ssn_idx is valid (!= 0xFFF) */
  2514. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  2515. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  2516. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  2517. /* Set up Tx window size and frame limit for this queue */
  2518. iwl_write_targ_mem(priv,
  2519. priv->scd_base_addr + IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  2520. (SCD_WIN_SIZE << IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  2521. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  2522. iwl_write_targ_mem(priv, priv->scd_base_addr +
  2523. IWL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  2524. (SCD_FRAME_LIMIT << IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  2525. & IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  2526. iwl_set_bits_prph(priv, IWL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  2527. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  2528. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  2529. iwl_release_nic_access(priv);
  2530. spin_unlock_irqrestore(&priv->lock, flags);
  2531. return 0;
  2532. }
  2533. static int iwl4965_rx_agg_start(struct iwl_priv *priv,
  2534. const u8 *addr, int tid, u16 ssn)
  2535. {
  2536. unsigned long flags;
  2537. int sta_id;
  2538. sta_id = iwl_find_station(priv, addr);
  2539. if (sta_id == IWL_INVALID_STATION)
  2540. return -ENXIO;
  2541. spin_lock_irqsave(&priv->sta_lock, flags);
  2542. priv->stations[sta_id].sta.station_flags_msk = 0;
  2543. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  2544. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  2545. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  2546. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2547. spin_unlock_irqrestore(&priv->sta_lock, flags);
  2548. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  2549. CMD_ASYNC);
  2550. }
  2551. static int iwl4965_rx_agg_stop(struct iwl_priv *priv,
  2552. const u8 *addr, int tid)
  2553. {
  2554. unsigned long flags;
  2555. int sta_id;
  2556. sta_id = iwl_find_station(priv, addr);
  2557. if (sta_id == IWL_INVALID_STATION)
  2558. return -ENXIO;
  2559. spin_lock_irqsave(&priv->sta_lock, flags);
  2560. priv->stations[sta_id].sta.station_flags_msk = 0;
  2561. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  2562. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  2563. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2564. spin_unlock_irqrestore(&priv->sta_lock, flags);
  2565. return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
  2566. CMD_ASYNC);
  2567. }
  2568. int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
  2569. enum ieee80211_ampdu_mlme_action action,
  2570. const u8 *addr, u16 tid, u16 *ssn)
  2571. {
  2572. struct iwl_priv *priv = hw->priv;
  2573. DECLARE_MAC_BUF(mac);
  2574. IWL_DEBUG_HT("A-MPDU action on addr %s tid %d\n",
  2575. print_mac(mac, addr), tid);
  2576. switch (action) {
  2577. case IEEE80211_AMPDU_RX_START:
  2578. IWL_DEBUG_HT("start Rx\n");
  2579. return iwl4965_rx_agg_start(priv, addr, tid, *ssn);
  2580. case IEEE80211_AMPDU_RX_STOP:
  2581. IWL_DEBUG_HT("stop Rx\n");
  2582. return iwl4965_rx_agg_stop(priv, addr, tid);
  2583. case IEEE80211_AMPDU_TX_START:
  2584. IWL_DEBUG_HT("start Tx\n");
  2585. return iwl_tx_agg_start(priv, addr, tid, ssn);
  2586. case IEEE80211_AMPDU_TX_STOP:
  2587. IWL_DEBUG_HT("stop Tx\n");
  2588. return iwl_tx_agg_stop(priv, addr, tid);
  2589. default:
  2590. IWL_DEBUG_HT("unknown\n");
  2591. return -EINVAL;
  2592. break;
  2593. }
  2594. return 0;
  2595. }
  2596. static u16 iwl4965_get_hcmd_size(u8 cmd_id, u16 len)
  2597. {
  2598. switch (cmd_id) {
  2599. case REPLY_RXON:
  2600. return (u16) sizeof(struct iwl4965_rxon_cmd);
  2601. default:
  2602. return len;
  2603. }
  2604. }
  2605. static u16 iwl4965_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
  2606. {
  2607. struct iwl4965_addsta_cmd *addsta = (struct iwl4965_addsta_cmd *)data;
  2608. addsta->mode = cmd->mode;
  2609. memcpy(&addsta->sta, &cmd->sta, sizeof(struct sta_id_modify));
  2610. memcpy(&addsta->key, &cmd->key, sizeof(struct iwl4965_keyinfo));
  2611. addsta->station_flags = cmd->station_flags;
  2612. addsta->station_flags_msk = cmd->station_flags_msk;
  2613. addsta->tid_disable_tx = cmd->tid_disable_tx;
  2614. addsta->add_immediate_ba_tid = cmd->add_immediate_ba_tid;
  2615. addsta->remove_immediate_ba_tid = cmd->remove_immediate_ba_tid;
  2616. addsta->add_immediate_ba_ssn = cmd->add_immediate_ba_ssn;
  2617. addsta->reserved1 = __constant_cpu_to_le16(0);
  2618. addsta->reserved2 = __constant_cpu_to_le32(0);
  2619. return (u16)sizeof(struct iwl4965_addsta_cmd);
  2620. }
  2621. static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
  2622. {
  2623. return le32_to_cpup(&tx_resp->u.status + tx_resp->frame_count) & MAX_SN;
  2624. }
  2625. /**
  2626. * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
  2627. */
  2628. static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
  2629. struct iwl_ht_agg *agg,
  2630. struct iwl4965_tx_resp *tx_resp,
  2631. int txq_id, u16 start_idx)
  2632. {
  2633. u16 status;
  2634. struct agg_tx_status *frame_status = tx_resp->u.agg_status;
  2635. struct ieee80211_tx_info *info = NULL;
  2636. struct ieee80211_hdr *hdr = NULL;
  2637. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  2638. int i, sh, idx;
  2639. u16 seq;
  2640. if (agg->wait_for_ba)
  2641. IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
  2642. agg->frame_count = tx_resp->frame_count;
  2643. agg->start_idx = start_idx;
  2644. agg->rate_n_flags = rate_n_flags;
  2645. agg->bitmap = 0;
  2646. /* # frames attempted by Tx command */
  2647. if (agg->frame_count == 1) {
  2648. /* Only one frame was attempted; no block-ack will arrive */
  2649. status = le16_to_cpu(frame_status[0].status);
  2650. idx = start_idx;
  2651. /* FIXME: code repetition */
  2652. IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
  2653. agg->frame_count, agg->start_idx, idx);
  2654. info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
  2655. info->status.retry_count = tx_resp->failure_frame;
  2656. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  2657. info->flags |= iwl_is_tx_success(status)?
  2658. IEEE80211_TX_STAT_ACK : 0;
  2659. iwl_hwrate_to_tx_control(priv, rate_n_flags, info);
  2660. /* FIXME: code repetition end */
  2661. IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
  2662. status & 0xff, tx_resp->failure_frame);
  2663. IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n", rate_n_flags);
  2664. agg->wait_for_ba = 0;
  2665. } else {
  2666. /* Two or more frames were attempted; expect block-ack */
  2667. u64 bitmap = 0;
  2668. int start = agg->start_idx;
  2669. /* Construct bit-map of pending frames within Tx window */
  2670. for (i = 0; i < agg->frame_count; i++) {
  2671. u16 sc;
  2672. status = le16_to_cpu(frame_status[i].status);
  2673. seq = le16_to_cpu(frame_status[i].sequence);
  2674. idx = SEQ_TO_INDEX(seq);
  2675. txq_id = SEQ_TO_QUEUE(seq);
  2676. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  2677. AGG_TX_STATE_ABORT_MSK))
  2678. continue;
  2679. IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
  2680. agg->frame_count, txq_id, idx);
  2681. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  2682. sc = le16_to_cpu(hdr->seq_ctrl);
  2683. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  2684. IWL_ERROR("BUG_ON idx doesn't match seq control"
  2685. " idx=%d, seq_idx=%d, seq=%d\n",
  2686. idx, SEQ_TO_SN(sc),
  2687. hdr->seq_ctrl);
  2688. return -1;
  2689. }
  2690. IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
  2691. i, idx, SEQ_TO_SN(sc));
  2692. sh = idx - start;
  2693. if (sh > 64) {
  2694. sh = (start - idx) + 0xff;
  2695. bitmap = bitmap << sh;
  2696. sh = 0;
  2697. start = idx;
  2698. } else if (sh < -64)
  2699. sh = 0xff - (start - idx);
  2700. else if (sh < 0) {
  2701. sh = start - idx;
  2702. start = idx;
  2703. bitmap = bitmap << sh;
  2704. sh = 0;
  2705. }
  2706. bitmap |= (1 << sh);
  2707. IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
  2708. start, (u32)(bitmap & 0xFFFFFFFF));
  2709. }
  2710. agg->bitmap = bitmap;
  2711. agg->start_idx = start;
  2712. IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
  2713. agg->frame_count, agg->start_idx,
  2714. (unsigned long long)agg->bitmap);
  2715. if (bitmap)
  2716. agg->wait_for_ba = 1;
  2717. }
  2718. return 0;
  2719. }
  2720. /**
  2721. * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
  2722. */
  2723. static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
  2724. struct iwl_rx_mem_buffer *rxb)
  2725. {
  2726. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  2727. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2728. int txq_id = SEQ_TO_QUEUE(sequence);
  2729. int index = SEQ_TO_INDEX(sequence);
  2730. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  2731. struct ieee80211_tx_info *info;
  2732. struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  2733. u32 status = le32_to_cpu(tx_resp->u.status);
  2734. int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
  2735. u16 fc;
  2736. struct ieee80211_hdr *hdr;
  2737. u8 *qc = NULL;
  2738. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  2739. IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
  2740. "is out of range [0-%d] %d %d\n", txq_id,
  2741. index, txq->q.n_bd, txq->q.write_ptr,
  2742. txq->q.read_ptr);
  2743. return;
  2744. }
  2745. info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
  2746. memset(&info->status, 0, sizeof(info->status));
  2747. hdr = iwl_tx_queue_get_hdr(priv, txq_id, index);
  2748. fc = le16_to_cpu(hdr->frame_control);
  2749. if (ieee80211_is_qos_data(fc)) {
  2750. qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
  2751. tid = qc[0] & 0xf;
  2752. }
  2753. sta_id = iwl_get_ra_sta_id(priv, hdr);
  2754. if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
  2755. IWL_ERROR("Station not known\n");
  2756. return;
  2757. }
  2758. if (txq->sched_retry) {
  2759. const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
  2760. struct iwl_ht_agg *agg = NULL;
  2761. if (!qc)
  2762. return;
  2763. agg = &priv->stations[sta_id].tid[tid].agg;
  2764. iwl4965_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  2765. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status)) {
  2766. /* TODO: send BAR */
  2767. }
  2768. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  2769. int freed, ampdu_q;
  2770. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  2771. IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
  2772. "%d index %d\n", scd_ssn , index);
  2773. freed = iwl_tx_queue_reclaim(priv, txq_id, index);
  2774. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  2775. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  2776. txq_id >= 0 && priv->mac80211_registered &&
  2777. agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
  2778. /* calculate mac80211 ampdu sw queue to wake */
  2779. ampdu_q = txq_id - IWL49_FIRST_AMPDU_QUEUE +
  2780. priv->hw->queues;
  2781. if (agg->state == IWL_AGG_OFF)
  2782. ieee80211_wake_queue(priv->hw, txq_id);
  2783. else
  2784. ieee80211_wake_queue(priv->hw, ampdu_q);
  2785. }
  2786. iwl_txq_check_empty(priv, sta_id, tid, txq_id);
  2787. }
  2788. } else {
  2789. info->status.retry_count = tx_resp->failure_frame;
  2790. info->flags |=
  2791. iwl_is_tx_success(status) ? IEEE80211_TX_STAT_ACK : 0;
  2792. iwl_hwrate_to_tx_control(priv,
  2793. le32_to_cpu(tx_resp->rate_n_flags),
  2794. info);
  2795. IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags "
  2796. "0x%x retries %d\n", txq_id,
  2797. iwl_get_tx_fail_reason(status),
  2798. status, le32_to_cpu(tx_resp->rate_n_flags),
  2799. tx_resp->failure_frame);
  2800. IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
  2801. if (index != -1) {
  2802. int freed = iwl_tx_queue_reclaim(priv, txq_id, index);
  2803. if (tid != MAX_TID_COUNT)
  2804. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  2805. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  2806. (txq_id >= 0) && priv->mac80211_registered)
  2807. ieee80211_wake_queue(priv->hw, txq_id);
  2808. if (tid != MAX_TID_COUNT)
  2809. iwl_txq_check_empty(priv, sta_id, tid, txq_id);
  2810. }
  2811. }
  2812. if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
  2813. IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
  2814. }
  2815. /* Set up 4965-specific Rx frame reply handlers */
  2816. static void iwl4965_rx_handler_setup(struct iwl_priv *priv)
  2817. {
  2818. /* Legacy Rx frames */
  2819. priv->rx_handlers[REPLY_RX] = iwl4965_rx_reply_rx;
  2820. /* Tx response */
  2821. priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
  2822. /* block ack */
  2823. priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
  2824. }
  2825. static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
  2826. {
  2827. INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
  2828. }
  2829. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
  2830. {
  2831. cancel_work_sync(&priv->txpower_work);
  2832. }
  2833. static struct iwl_hcmd_ops iwl4965_hcmd = {
  2834. .rxon_assoc = iwl4965_send_rxon_assoc,
  2835. };
  2836. static struct iwl_hcmd_utils_ops iwl4965_hcmd_utils = {
  2837. .get_hcmd_size = iwl4965_get_hcmd_size,
  2838. .build_addsta_hcmd = iwl4965_build_addsta_hcmd,
  2839. .chain_noise_reset = iwl4965_chain_noise_reset,
  2840. .gain_computation = iwl4965_gain_computation,
  2841. };
  2842. static struct iwl_lib_ops iwl4965_lib = {
  2843. .set_hw_params = iwl4965_hw_set_hw_params,
  2844. .alloc_shared_mem = iwl4965_alloc_shared_mem,
  2845. .free_shared_mem = iwl4965_free_shared_mem,
  2846. .shared_mem_rx_idx = iwl4965_shared_mem_rx_idx,
  2847. .txq_update_byte_cnt_tbl = iwl4965_txq_update_byte_cnt_tbl,
  2848. .txq_set_sched = iwl4965_txq_set_sched,
  2849. .txq_agg_enable = iwl4965_txq_agg_enable,
  2850. .txq_agg_disable = iwl4965_txq_agg_disable,
  2851. .rx_handler_setup = iwl4965_rx_handler_setup,
  2852. .setup_deferred_work = iwl4965_setup_deferred_work,
  2853. .cancel_deferred_work = iwl4965_cancel_deferred_work,
  2854. .is_valid_rtc_data_addr = iwl4965_hw_valid_rtc_data_addr,
  2855. .alive_notify = iwl4965_alive_notify,
  2856. .init_alive_start = iwl4965_init_alive_start,
  2857. .load_ucode = iwl4965_load_bsm,
  2858. .apm_ops = {
  2859. .init = iwl4965_apm_init,
  2860. .reset = iwl4965_apm_reset,
  2861. .stop = iwl4965_apm_stop,
  2862. .config = iwl4965_nic_config,
  2863. .set_pwr_src = iwl4965_set_pwr_src,
  2864. },
  2865. .eeprom_ops = {
  2866. .regulatory_bands = {
  2867. EEPROM_REGULATORY_BAND_1_CHANNELS,
  2868. EEPROM_REGULATORY_BAND_2_CHANNELS,
  2869. EEPROM_REGULATORY_BAND_3_CHANNELS,
  2870. EEPROM_REGULATORY_BAND_4_CHANNELS,
  2871. EEPROM_REGULATORY_BAND_5_CHANNELS,
  2872. EEPROM_4965_REGULATORY_BAND_24_FAT_CHANNELS,
  2873. EEPROM_4965_REGULATORY_BAND_52_FAT_CHANNELS
  2874. },
  2875. .verify_signature = iwlcore_eeprom_verify_signature,
  2876. .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
  2877. .release_semaphore = iwlcore_eeprom_release_semaphore,
  2878. .check_version = iwl4965_eeprom_check_version,
  2879. .query_addr = iwlcore_eeprom_query_addr,
  2880. },
  2881. .radio_kill_sw = iwl4965_radio_kill_sw,
  2882. .set_power = iwl4965_set_power,
  2883. .send_tx_power = iwl4965_send_tx_power,
  2884. .update_chain_flags = iwl4965_update_chain_flags,
  2885. };
  2886. static struct iwl_ops iwl4965_ops = {
  2887. .lib = &iwl4965_lib,
  2888. .hcmd = &iwl4965_hcmd,
  2889. .utils = &iwl4965_hcmd_utils,
  2890. };
  2891. struct iwl_cfg iwl4965_agn_cfg = {
  2892. .name = "4965AGN",
  2893. .fw_name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode",
  2894. .sku = IWL_SKU_A|IWL_SKU_G|IWL_SKU_N,
  2895. .eeprom_size = IWL4965_EEPROM_IMG_SIZE,
  2896. .ops = &iwl4965_ops,
  2897. .mod_params = &iwl4965_mod_params,
  2898. };
  2899. module_param_named(antenna, iwl4965_mod_params.antenna, int, 0444);
  2900. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  2901. module_param_named(disable, iwl4965_mod_params.disable, int, 0444);
  2902. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  2903. module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, 0444);
  2904. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])\n");
  2905. module_param_named(debug, iwl4965_mod_params.debug, int, 0444);
  2906. MODULE_PARM_DESC(debug, "debug output mask");
  2907. module_param_named(
  2908. disable_hw_scan, iwl4965_mod_params.disable_hw_scan, int, 0444);
  2909. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  2910. module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, 0444);
  2911. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  2912. /* QoS */
  2913. module_param_named(qos_enable, iwl4965_mod_params.enable_qos, int, 0444);
  2914. MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
  2915. module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K, int, 0444);
  2916. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  2917. module_param_named(fw_restart4965, iwl4965_mod_params.restart_fw, int, 0444);
  2918. MODULE_PARM_DESC(fw_restart4965, "restart firmware in case of error");