iwl-4965.c 98 KB

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