iwl-4965.c 122 KB

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