iwl-4965.c 110 KB

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