iwl-4965.c 131 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 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 "iwl-4965.h"
  39. #include "iwl-helpers.h"
  40. static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv);
  41. #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  42. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  43. IWL_RATE_SISO_##s##M_PLCP, \
  44. IWL_RATE_MIMO_##s##M_PLCP, \
  45. IWL_RATE_##r##M_IEEE, \
  46. IWL_RATE_##ip##M_INDEX, \
  47. IWL_RATE_##in##M_INDEX, \
  48. IWL_RATE_##rp##M_INDEX, \
  49. IWL_RATE_##rn##M_INDEX, \
  50. IWL_RATE_##pp##M_INDEX, \
  51. IWL_RATE_##np##M_INDEX }
  52. /*
  53. * Parameter order:
  54. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  55. *
  56. * If there isn't a valid next or previous rate then INV is used which
  57. * maps to IWL_RATE_INVALID
  58. *
  59. */
  60. const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
  61. IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  62. IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  63. IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  64. IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  65. IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  66. IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  67. IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  68. IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  69. IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  70. IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  71. IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  72. IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  73. IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  74. };
  75. static int is_fat_channel(__le32 rxon_flags)
  76. {
  77. return (rxon_flags & RXON_FLG_CHANNEL_MODE_PURE_40_MSK) ||
  78. (rxon_flags & RXON_FLG_CHANNEL_MODE_MIXED_MSK);
  79. }
  80. static u8 is_single_stream(struct iwl4965_priv *priv)
  81. {
  82. #ifdef CONFIG_IWL4965_HT
  83. if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht ||
  84. (priv->active_rate_ht[1] == 0) ||
  85. (priv->ps_mode == IWL_MIMO_PS_STATIC))
  86. return 1;
  87. #else
  88. return 1;
  89. #endif /*CONFIG_IWL4965_HT */
  90. return 0;
  91. }
  92. /*
  93. * Determine how many receiver/antenna chains to use.
  94. * More provides better reception via diversity. Fewer saves power.
  95. * MIMO (dual stream) requires at least 2, but works better with 3.
  96. * This does not determine *which* chains to use, just how many.
  97. */
  98. static int iwl4965_get_rx_chain_counter(struct iwl4965_priv *priv,
  99. u8 *idle_state, u8 *rx_state)
  100. {
  101. u8 is_single = is_single_stream(priv);
  102. u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
  103. /* # of Rx chains to use when expecting MIMO. */
  104. if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
  105. *rx_state = 2;
  106. else
  107. *rx_state = 3;
  108. /* # Rx chains when idling and maybe trying to save power */
  109. switch (priv->ps_mode) {
  110. case IWL_MIMO_PS_STATIC:
  111. case IWL_MIMO_PS_DYNAMIC:
  112. *idle_state = (is_cam) ? 2 : 1;
  113. break;
  114. case IWL_MIMO_PS_NONE:
  115. *idle_state = (is_cam) ? *rx_state : 1;
  116. break;
  117. default:
  118. *idle_state = 1;
  119. break;
  120. }
  121. return 0;
  122. }
  123. int iwl4965_hw_rxq_stop(struct iwl4965_priv *priv)
  124. {
  125. int rc;
  126. unsigned long flags;
  127. spin_lock_irqsave(&priv->lock, flags);
  128. rc = iwl4965_grab_nic_access(priv);
  129. if (rc) {
  130. spin_unlock_irqrestore(&priv->lock, flags);
  131. return rc;
  132. }
  133. /* stop HW */
  134. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  135. rc = iwl4965_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  136. (1 << 24), 1000);
  137. if (rc < 0)
  138. IWL_ERROR("Can't stop Rx DMA.\n");
  139. iwl4965_release_nic_access(priv);
  140. spin_unlock_irqrestore(&priv->lock, flags);
  141. return 0;
  142. }
  143. u8 iwl4965_hw_find_station(struct iwl4965_priv *priv, const u8 *addr)
  144. {
  145. int i;
  146. int start = 0;
  147. int ret = IWL_INVALID_STATION;
  148. unsigned long flags;
  149. DECLARE_MAC_BUF(mac);
  150. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) ||
  151. (priv->iw_mode == IEEE80211_IF_TYPE_AP))
  152. start = IWL_STA_ID;
  153. if (is_broadcast_ether_addr(addr))
  154. return IWL4965_BROADCAST_ID;
  155. spin_lock_irqsave(&priv->sta_lock, flags);
  156. for (i = start; i < priv->hw_setting.max_stations; i++)
  157. if ((priv->stations[i].used) &&
  158. (!compare_ether_addr
  159. (priv->stations[i].sta.sta.addr, addr))) {
  160. ret = i;
  161. goto out;
  162. }
  163. IWL_DEBUG_ASSOC_LIMIT("can not find STA %s total %d\n",
  164. print_mac(mac, addr), priv->num_stations);
  165. out:
  166. spin_unlock_irqrestore(&priv->sta_lock, flags);
  167. return ret;
  168. }
  169. static int iwl4965_nic_set_pwr_src(struct iwl4965_priv *priv, int pwr_max)
  170. {
  171. int ret;
  172. unsigned long flags;
  173. spin_lock_irqsave(&priv->lock, flags);
  174. ret = iwl4965_grab_nic_access(priv);
  175. if (ret) {
  176. spin_unlock_irqrestore(&priv->lock, flags);
  177. return ret;
  178. }
  179. if (!pwr_max) {
  180. u32 val;
  181. ret = pci_read_config_dword(priv->pci_dev, PCI_POWER_SOURCE,
  182. &val);
  183. if (val & PCI_CFG_PMC_PME_FROM_D3COLD_SUPPORT)
  184. iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  185. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  186. ~APMG_PS_CTRL_MSK_PWR_SRC);
  187. } else
  188. iwl4965_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  189. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  190. ~APMG_PS_CTRL_MSK_PWR_SRC);
  191. iwl4965_release_nic_access(priv);
  192. spin_unlock_irqrestore(&priv->lock, flags);
  193. return ret;
  194. }
  195. static int iwl4965_rx_init(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
  196. {
  197. int rc;
  198. unsigned long flags;
  199. spin_lock_irqsave(&priv->lock, flags);
  200. rc = iwl4965_grab_nic_access(priv);
  201. if (rc) {
  202. spin_unlock_irqrestore(&priv->lock, flags);
  203. return rc;
  204. }
  205. /* stop HW */
  206. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  207. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  208. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  209. rxq->dma_addr >> 8);
  210. iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  211. (priv->hw_setting.shared_phys +
  212. offsetof(struct iwl4965_shared, val0)) >> 4);
  213. iwl4965_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  214. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  215. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  216. IWL_FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K |
  217. /*0x10 << 4 | */
  218. (RX_QUEUE_SIZE_LOG <<
  219. FH_RCSR_RX_CONFIG_RBDCB_SIZE_BITSHIFT));
  220. /*
  221. * iwl4965_write32(priv,CSR_INT_COAL_REG,0);
  222. */
  223. iwl4965_release_nic_access(priv);
  224. spin_unlock_irqrestore(&priv->lock, flags);
  225. return 0;
  226. }
  227. static int iwl4965_kw_init(struct iwl4965_priv *priv)
  228. {
  229. unsigned long flags;
  230. int rc;
  231. spin_lock_irqsave(&priv->lock, flags);
  232. rc = iwl4965_grab_nic_access(priv);
  233. if (rc)
  234. goto out;
  235. iwl4965_write_direct32(priv, IWL_FH_KW_MEM_ADDR_REG,
  236. priv->kw.dma_addr >> 4);
  237. iwl4965_release_nic_access(priv);
  238. out:
  239. spin_unlock_irqrestore(&priv->lock, flags);
  240. return rc;
  241. }
  242. static int iwl4965_kw_alloc(struct iwl4965_priv *priv)
  243. {
  244. struct pci_dev *dev = priv->pci_dev;
  245. struct iwl4965_kw *kw = &priv->kw;
  246. kw->size = IWL4965_KW_SIZE; /* TBW need set somewhere else */
  247. kw->v_addr = pci_alloc_consistent(dev, kw->size, &kw->dma_addr);
  248. if (!kw->v_addr)
  249. return -ENOMEM;
  250. return 0;
  251. }
  252. #define CHECK_AND_PRINT(x) ((eeprom_ch->flags & EEPROM_CHANNEL_##x) \
  253. ? # x " " : "")
  254. int iwl4965_set_fat_chan_info(struct iwl4965_priv *priv, int phymode, u16 channel,
  255. const struct iwl4965_eeprom_channel *eeprom_ch,
  256. u8 fat_extension_channel)
  257. {
  258. struct iwl4965_channel_info *ch_info;
  259. ch_info = (struct iwl4965_channel_info *)
  260. iwl4965_get_channel_info(priv, phymode, channel);
  261. if (!is_channel_valid(ch_info))
  262. return -1;
  263. IWL_DEBUG_INFO("FAT Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
  264. " %ddBm): Ad-Hoc %ssupported\n",
  265. ch_info->channel,
  266. is_channel_a_band(ch_info) ?
  267. "5.2" : "2.4",
  268. CHECK_AND_PRINT(IBSS),
  269. CHECK_AND_PRINT(ACTIVE),
  270. CHECK_AND_PRINT(RADAR),
  271. CHECK_AND_PRINT(WIDE),
  272. CHECK_AND_PRINT(NARROW),
  273. CHECK_AND_PRINT(DFS),
  274. eeprom_ch->flags,
  275. eeprom_ch->max_power_avg,
  276. ((eeprom_ch->flags & EEPROM_CHANNEL_IBSS)
  277. && !(eeprom_ch->flags & EEPROM_CHANNEL_RADAR)) ?
  278. "" : "not ");
  279. ch_info->fat_eeprom = *eeprom_ch;
  280. ch_info->fat_max_power_avg = eeprom_ch->max_power_avg;
  281. ch_info->fat_curr_txpow = eeprom_ch->max_power_avg;
  282. ch_info->fat_min_power = 0;
  283. ch_info->fat_scan_power = eeprom_ch->max_power_avg;
  284. ch_info->fat_flags = eeprom_ch->flags;
  285. ch_info->fat_extension_channel = fat_extension_channel;
  286. return 0;
  287. }
  288. static void iwl4965_kw_free(struct iwl4965_priv *priv)
  289. {
  290. struct pci_dev *dev = priv->pci_dev;
  291. struct iwl4965_kw *kw = &priv->kw;
  292. if (kw->v_addr) {
  293. pci_free_consistent(dev, kw->size, kw->v_addr, kw->dma_addr);
  294. memset(kw, 0, sizeof(*kw));
  295. }
  296. }
  297. /**
  298. * iwl4965_txq_ctx_reset - Reset TX queue context
  299. * Destroys all DMA structures and initialise them again
  300. *
  301. * @param priv
  302. * @return error code
  303. */
  304. static int iwl4965_txq_ctx_reset(struct iwl4965_priv *priv)
  305. {
  306. int rc = 0;
  307. int txq_id, slots_num;
  308. unsigned long flags;
  309. iwl4965_kw_free(priv);
  310. iwl4965_hw_txq_ctx_free(priv);
  311. /* Tx CMD queue */
  312. rc = iwl4965_kw_alloc(priv);
  313. if (rc) {
  314. IWL_ERROR("Keep Warm allocation failed");
  315. goto error_kw;
  316. }
  317. spin_lock_irqsave(&priv->lock, flags);
  318. rc = iwl4965_grab_nic_access(priv);
  319. if (unlikely(rc)) {
  320. IWL_ERROR("TX reset failed");
  321. spin_unlock_irqrestore(&priv->lock, flags);
  322. goto error_reset;
  323. }
  324. iwl4965_write_prph(priv, KDR_SCD_TXFACT, 0);
  325. iwl4965_release_nic_access(priv);
  326. spin_unlock_irqrestore(&priv->lock, flags);
  327. rc = iwl4965_kw_init(priv);
  328. if (rc) {
  329. IWL_ERROR("kw_init failed\n");
  330. goto error_reset;
  331. }
  332. /* Tx queue(s) */
  333. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
  334. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  335. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  336. rc = iwl4965_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  337. txq_id);
  338. if (rc) {
  339. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  340. goto error;
  341. }
  342. }
  343. return rc;
  344. error:
  345. iwl4965_hw_txq_ctx_free(priv);
  346. error_reset:
  347. iwl4965_kw_free(priv);
  348. error_kw:
  349. return rc;
  350. }
  351. int iwl4965_hw_nic_init(struct iwl4965_priv *priv)
  352. {
  353. int rc;
  354. unsigned long flags;
  355. struct iwl4965_rx_queue *rxq = &priv->rxq;
  356. u8 rev_id;
  357. u32 val;
  358. u8 val_link;
  359. iwl4965_power_init_handle(priv);
  360. /* nic_init */
  361. spin_lock_irqsave(&priv->lock, flags);
  362. iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  363. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  364. iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  365. rc = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
  366. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  367. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  368. if (rc < 0) {
  369. spin_unlock_irqrestore(&priv->lock, flags);
  370. IWL_DEBUG_INFO("Failed to init the card\n");
  371. return rc;
  372. }
  373. rc = iwl4965_grab_nic_access(priv);
  374. if (rc) {
  375. spin_unlock_irqrestore(&priv->lock, flags);
  376. return rc;
  377. }
  378. iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
  379. iwl4965_write_prph(priv, APMG_CLK_CTRL_REG,
  380. APMG_CLK_VAL_DMA_CLK_RQT |
  381. APMG_CLK_VAL_BSM_CLK_RQT);
  382. iwl4965_read_prph(priv, APMG_CLK_CTRL_REG);
  383. udelay(20);
  384. iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  385. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  386. iwl4965_release_nic_access(priv);
  387. iwl4965_write32(priv, CSR_INT_COALESCING, 512 / 32);
  388. spin_unlock_irqrestore(&priv->lock, flags);
  389. /* Determine HW type */
  390. rc = pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &rev_id);
  391. if (rc)
  392. return rc;
  393. IWL_DEBUG_INFO("HW Revision ID = 0x%X\n", rev_id);
  394. iwl4965_nic_set_pwr_src(priv, 1);
  395. spin_lock_irqsave(&priv->lock, flags);
  396. if ((rev_id & 0x80) == 0x80 && (rev_id & 0x7f) < 8) {
  397. pci_read_config_dword(priv->pci_dev, PCI_REG_WUM8, &val);
  398. /* Enable No Snoop field */
  399. pci_write_config_dword(priv->pci_dev, PCI_REG_WUM8,
  400. val & ~(1 << 11));
  401. }
  402. spin_unlock_irqrestore(&priv->lock, flags);
  403. /* Read the EEPROM */
  404. rc = iwl4965_eeprom_init(priv);
  405. if (rc)
  406. return rc;
  407. if (priv->eeprom.calib_version < EEPROM_TX_POWER_VERSION_NEW) {
  408. IWL_ERROR("Older EEPROM detected! Aborting.\n");
  409. return -EINVAL;
  410. }
  411. pci_read_config_byte(priv->pci_dev, PCI_LINK_CTRL, &val_link);
  412. /* disable L1 entry -- workaround for pre-B1 */
  413. pci_write_config_byte(priv->pci_dev, PCI_LINK_CTRL, val_link & ~0x02);
  414. spin_lock_irqsave(&priv->lock, flags);
  415. /* set CSR_HW_CONFIG_REG for uCode use */
  416. iwl4965_set_bit(priv, CSR_SW_VER, CSR_HW_IF_CONFIG_REG_BIT_KEDRON_R |
  417. CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
  418. CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
  419. rc = iwl4965_grab_nic_access(priv);
  420. if (rc < 0) {
  421. spin_unlock_irqrestore(&priv->lock, flags);
  422. IWL_DEBUG_INFO("Failed to init the card\n");
  423. return rc;
  424. }
  425. iwl4965_read_prph(priv, APMG_PS_CTRL_REG);
  426. iwl4965_set_bits_prph(priv, APMG_PS_CTRL_REG,
  427. APMG_PS_CTRL_VAL_RESET_REQ);
  428. udelay(5);
  429. iwl4965_clear_bits_prph(priv, APMG_PS_CTRL_REG,
  430. APMG_PS_CTRL_VAL_RESET_REQ);
  431. iwl4965_release_nic_access(priv);
  432. spin_unlock_irqrestore(&priv->lock, flags);
  433. iwl4965_hw_card_show_info(priv);
  434. /* end nic_init */
  435. /* Allocate the RX queue, or reset if it is already allocated */
  436. if (!rxq->bd) {
  437. rc = iwl4965_rx_queue_alloc(priv);
  438. if (rc) {
  439. IWL_ERROR("Unable to initialize Rx queue\n");
  440. return -ENOMEM;
  441. }
  442. } else
  443. iwl4965_rx_queue_reset(priv, rxq);
  444. iwl4965_rx_replenish(priv);
  445. iwl4965_rx_init(priv, rxq);
  446. spin_lock_irqsave(&priv->lock, flags);
  447. rxq->need_update = 1;
  448. iwl4965_rx_queue_update_write_ptr(priv, rxq);
  449. spin_unlock_irqrestore(&priv->lock, flags);
  450. rc = iwl4965_txq_ctx_reset(priv);
  451. if (rc)
  452. return rc;
  453. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_SW_RF_KILL_ENABLE)
  454. IWL_DEBUG_RF_KILL("SW RF KILL supported in EEPROM.\n");
  455. if (priv->eeprom.sku_cap & EEPROM_SKU_CAP_HW_RF_KILL_ENABLE)
  456. IWL_DEBUG_RF_KILL("HW RF KILL supported in EEPROM.\n");
  457. set_bit(STATUS_INIT, &priv->status);
  458. return 0;
  459. }
  460. int iwl4965_hw_nic_stop_master(struct iwl4965_priv *priv)
  461. {
  462. int rc = 0;
  463. u32 reg_val;
  464. unsigned long flags;
  465. spin_lock_irqsave(&priv->lock, flags);
  466. /* set stop master bit */
  467. iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  468. reg_val = iwl4965_read32(priv, CSR_GP_CNTRL);
  469. if (CSR_GP_CNTRL_REG_FLAG_MAC_POWER_SAVE ==
  470. (reg_val & CSR_GP_CNTRL_REG_MSK_POWER_SAVE_TYPE))
  471. IWL_DEBUG_INFO("Card in power save, master is already "
  472. "stopped\n");
  473. else {
  474. rc = iwl4965_poll_bit(priv, CSR_RESET,
  475. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  476. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  477. if (rc < 0) {
  478. spin_unlock_irqrestore(&priv->lock, flags);
  479. return rc;
  480. }
  481. }
  482. spin_unlock_irqrestore(&priv->lock, flags);
  483. IWL_DEBUG_INFO("stop master\n");
  484. return rc;
  485. }
  486. void iwl4965_hw_txq_ctx_stop(struct iwl4965_priv *priv)
  487. {
  488. int txq_id;
  489. unsigned long flags;
  490. /* reset TFD queues */
  491. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++) {
  492. spin_lock_irqsave(&priv->lock, flags);
  493. if (iwl4965_grab_nic_access(priv)) {
  494. spin_unlock_irqrestore(&priv->lock, flags);
  495. continue;
  496. }
  497. iwl4965_write_direct32(priv,
  498. IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  499. 0x0);
  500. iwl4965_poll_direct_bit(priv, IWL_FH_TSSR_TX_STATUS_REG,
  501. IWL_FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
  502. (txq_id), 200);
  503. iwl4965_release_nic_access(priv);
  504. spin_unlock_irqrestore(&priv->lock, flags);
  505. }
  506. iwl4965_hw_txq_ctx_free(priv);
  507. }
  508. int iwl4965_hw_nic_reset(struct iwl4965_priv *priv)
  509. {
  510. int rc = 0;
  511. unsigned long flags;
  512. iwl4965_hw_nic_stop_master(priv);
  513. spin_lock_irqsave(&priv->lock, flags);
  514. iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  515. udelay(10);
  516. iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  517. rc = iwl4965_poll_bit(priv, CSR_RESET,
  518. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  519. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25);
  520. udelay(10);
  521. rc = iwl4965_grab_nic_access(priv);
  522. if (!rc) {
  523. iwl4965_write_prph(priv, APMG_CLK_EN_REG,
  524. APMG_CLK_VAL_DMA_CLK_RQT |
  525. APMG_CLK_VAL_BSM_CLK_RQT);
  526. udelay(10);
  527. iwl4965_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  528. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  529. iwl4965_release_nic_access(priv);
  530. }
  531. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  532. wake_up_interruptible(&priv->wait_command_queue);
  533. spin_unlock_irqrestore(&priv->lock, flags);
  534. return rc;
  535. }
  536. #define REG_RECALIB_PERIOD (60)
  537. /**
  538. * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
  539. *
  540. * This callback is provided in order to queue the statistics_work
  541. * in work_queue context (v. softirq)
  542. *
  543. * This timer function is continually reset to execute within
  544. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  545. * was received. We need to ensure we receive the statistics in order
  546. * to update the temperature used for calibrating the TXPOWER. However,
  547. * we can't send the statistics command from softirq context (which
  548. * is the context which timers run at) so we have to queue off the
  549. * statistics_work to actually send the command to the hardware.
  550. */
  551. static void iwl4965_bg_statistics_periodic(unsigned long data)
  552. {
  553. struct iwl4965_priv *priv = (struct iwl4965_priv *)data;
  554. queue_work(priv->workqueue, &priv->statistics_work);
  555. }
  556. /**
  557. * iwl4965_bg_statistics_work - Send the statistics request to the hardware.
  558. *
  559. * This is queued by iwl4965_bg_statistics_periodic.
  560. */
  561. static void iwl4965_bg_statistics_work(struct work_struct *work)
  562. {
  563. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  564. statistics_work);
  565. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  566. return;
  567. mutex_lock(&priv->mutex);
  568. iwl4965_send_statistics_request(priv);
  569. mutex_unlock(&priv->mutex);
  570. }
  571. #define CT_LIMIT_CONST 259
  572. #define TM_CT_KILL_THRESHOLD 110
  573. void iwl4965_rf_kill_ct_config(struct iwl4965_priv *priv)
  574. {
  575. struct iwl4965_ct_kill_config cmd;
  576. u32 R1, R2, R3;
  577. u32 temp_th;
  578. u32 crit_temperature;
  579. unsigned long flags;
  580. int rc = 0;
  581. spin_lock_irqsave(&priv->lock, flags);
  582. iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  583. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  584. spin_unlock_irqrestore(&priv->lock, flags);
  585. if (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK) {
  586. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  587. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  588. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  589. } else {
  590. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  591. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  592. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  593. }
  594. temp_th = CELSIUS_TO_KELVIN(TM_CT_KILL_THRESHOLD);
  595. crit_temperature = ((temp_th * (R3-R1))/CT_LIMIT_CONST) + R2;
  596. cmd.critical_temperature_R = cpu_to_le32(crit_temperature);
  597. rc = iwl4965_send_cmd_pdu(priv,
  598. REPLY_CT_KILL_CONFIG_CMD, sizeof(cmd), &cmd);
  599. if (rc)
  600. IWL_ERROR("REPLY_CT_KILL_CONFIG_CMD failed\n");
  601. else
  602. IWL_DEBUG_INFO("REPLY_CT_KILL_CONFIG_CMD succeeded\n");
  603. }
  604. #ifdef CONFIG_IWL4965_SENSITIVITY
  605. /* "false alarms" are signals that our DSP tries to lock onto,
  606. * but then determines that they are either noise, or transmissions
  607. * from a distant wireless network (also "noise", really) that get
  608. * "stepped on" by stronger transmissions within our own network.
  609. * This algorithm attempts to set a sensitivity level that is high
  610. * enough to receive all of our own network traffic, but not so
  611. * high that our DSP gets too busy trying to lock onto non-network
  612. * activity/noise. */
  613. static int iwl4965_sens_energy_cck(struct iwl4965_priv *priv,
  614. u32 norm_fa,
  615. u32 rx_enable_time,
  616. struct statistics_general_data *rx_info)
  617. {
  618. u32 max_nrg_cck = 0;
  619. int i = 0;
  620. u8 max_silence_rssi = 0;
  621. u32 silence_ref = 0;
  622. u8 silence_rssi_a = 0;
  623. u8 silence_rssi_b = 0;
  624. u8 silence_rssi_c = 0;
  625. u32 val;
  626. /* "false_alarms" values below are cross-multiplications to assess the
  627. * numbers of false alarms within the measured period of actual Rx
  628. * (Rx is off when we're txing), vs the min/max expected false alarms
  629. * (some should be expected if rx is sensitive enough) in a
  630. * hypothetical listening period of 200 time units (TU), 204.8 msec:
  631. *
  632. * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time
  633. *
  634. * */
  635. u32 false_alarms = norm_fa * 200 * 1024;
  636. u32 max_false_alarms = MAX_FA_CCK * rx_enable_time;
  637. u32 min_false_alarms = MIN_FA_CCK * rx_enable_time;
  638. struct iwl4965_sensitivity_data *data = NULL;
  639. data = &(priv->sensitivity_data);
  640. data->nrg_auto_corr_silence_diff = 0;
  641. /* Find max silence rssi among all 3 receivers.
  642. * This is background noise, which may include transmissions from other
  643. * networks, measured during silence before our network's beacon */
  644. silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a &
  645. ALL_BAND_FILTER)>>8);
  646. silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b &
  647. ALL_BAND_FILTER)>>8);
  648. silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c &
  649. ALL_BAND_FILTER)>>8);
  650. val = max(silence_rssi_b, silence_rssi_c);
  651. max_silence_rssi = max(silence_rssi_a, (u8) val);
  652. /* Store silence rssi in 20-beacon history table */
  653. data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi;
  654. data->nrg_silence_idx++;
  655. if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L)
  656. data->nrg_silence_idx = 0;
  657. /* Find max silence rssi across 20 beacon history */
  658. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) {
  659. val = data->nrg_silence_rssi[i];
  660. silence_ref = max(silence_ref, val);
  661. }
  662. IWL_DEBUG_CALIB("silence a %u, b %u, c %u, 20-bcn max %u\n",
  663. silence_rssi_a, silence_rssi_b, silence_rssi_c,
  664. silence_ref);
  665. /* Find max rx energy (min value!) among all 3 receivers,
  666. * measured during beacon frame.
  667. * Save it in 10-beacon history table. */
  668. i = data->nrg_energy_idx;
  669. val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c);
  670. data->nrg_value[i] = min(rx_info->beacon_energy_a, val);
  671. data->nrg_energy_idx++;
  672. if (data->nrg_energy_idx >= 10)
  673. data->nrg_energy_idx = 0;
  674. /* Find min rx energy (max value) across 10 beacon history.
  675. * This is the minimum signal level that we want to receive well.
  676. * Add backoff (margin so we don't miss slightly lower energy frames).
  677. * This establishes an upper bound (min value) for energy threshold. */
  678. max_nrg_cck = data->nrg_value[0];
  679. for (i = 1; i < 10; i++)
  680. max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i]));
  681. max_nrg_cck += 6;
  682. IWL_DEBUG_CALIB("rx energy a %u, b %u, c %u, 10-bcn max/min %u\n",
  683. rx_info->beacon_energy_a, rx_info->beacon_energy_b,
  684. rx_info->beacon_energy_c, max_nrg_cck - 6);
  685. /* Count number of consecutive beacons with fewer-than-desired
  686. * false alarms. */
  687. if (false_alarms < min_false_alarms)
  688. data->num_in_cck_no_fa++;
  689. else
  690. data->num_in_cck_no_fa = 0;
  691. IWL_DEBUG_CALIB("consecutive bcns with few false alarms = %u\n",
  692. data->num_in_cck_no_fa);
  693. /* If we got too many false alarms this time, reduce sensitivity */
  694. if (false_alarms > max_false_alarms) {
  695. IWL_DEBUG_CALIB("norm FA %u > max FA %u\n",
  696. false_alarms, max_false_alarms);
  697. IWL_DEBUG_CALIB("... reducing sensitivity\n");
  698. data->nrg_curr_state = IWL_FA_TOO_MANY;
  699. if (data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK) {
  700. /* Store for "fewer than desired" on later beacon */
  701. data->nrg_silence_ref = silence_ref;
  702. /* increase energy threshold (reduce nrg value)
  703. * to decrease sensitivity */
  704. if (data->nrg_th_cck > (NRG_MAX_CCK + NRG_STEP_CCK))
  705. data->nrg_th_cck = data->nrg_th_cck
  706. - NRG_STEP_CCK;
  707. }
  708. /* increase auto_corr values to decrease sensitivity */
  709. if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK)
  710. data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1;
  711. else {
  712. val = data->auto_corr_cck + AUTO_CORR_STEP_CCK;
  713. data->auto_corr_cck = min((u32)AUTO_CORR_MAX_CCK, val);
  714. }
  715. val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK;
  716. data->auto_corr_cck_mrc = min((u32)AUTO_CORR_MAX_CCK_MRC, val);
  717. /* Else if we got fewer than desired, increase sensitivity */
  718. } else if (false_alarms < min_false_alarms) {
  719. data->nrg_curr_state = IWL_FA_TOO_FEW;
  720. /* Compare silence level with silence level for most recent
  721. * healthy number or too many false alarms */
  722. data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref -
  723. (s32)silence_ref;
  724. IWL_DEBUG_CALIB("norm FA %u < min FA %u, silence diff %d\n",
  725. false_alarms, min_false_alarms,
  726. data->nrg_auto_corr_silence_diff);
  727. /* Increase value to increase sensitivity, but only if:
  728. * 1a) previous beacon did *not* have *too many* false alarms
  729. * 1b) AND there's a significant difference in Rx levels
  730. * from a previous beacon with too many, or healthy # FAs
  731. * OR 2) We've seen a lot of beacons (100) with too few
  732. * false alarms */
  733. if ((data->nrg_prev_state != IWL_FA_TOO_MANY) &&
  734. ((data->nrg_auto_corr_silence_diff > NRG_DIFF) ||
  735. (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) {
  736. IWL_DEBUG_CALIB("... increasing sensitivity\n");
  737. /* Increase nrg value to increase sensitivity */
  738. val = data->nrg_th_cck + NRG_STEP_CCK;
  739. data->nrg_th_cck = min((u32)NRG_MIN_CCK, val);
  740. /* Decrease auto_corr values to increase sensitivity */
  741. val = data->auto_corr_cck - AUTO_CORR_STEP_CCK;
  742. data->auto_corr_cck = max((u32)AUTO_CORR_MIN_CCK, val);
  743. val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK;
  744. data->auto_corr_cck_mrc =
  745. max((u32)AUTO_CORR_MIN_CCK_MRC, val);
  746. } else
  747. IWL_DEBUG_CALIB("... but not changing sensitivity\n");
  748. /* Else we got a healthy number of false alarms, keep status quo */
  749. } else {
  750. IWL_DEBUG_CALIB(" FA in safe zone\n");
  751. data->nrg_curr_state = IWL_FA_GOOD_RANGE;
  752. /* Store for use in "fewer than desired" with later beacon */
  753. data->nrg_silence_ref = silence_ref;
  754. /* If previous beacon had too many false alarms,
  755. * give it some extra margin by reducing sensitivity again
  756. * (but don't go below measured energy of desired Rx) */
  757. if (IWL_FA_TOO_MANY == data->nrg_prev_state) {
  758. IWL_DEBUG_CALIB("... increasing margin\n");
  759. data->nrg_th_cck -= NRG_MARGIN;
  760. }
  761. }
  762. /* Make sure the energy threshold does not go above the measured
  763. * energy of the desired Rx signals (reduced by backoff margin),
  764. * or else we might start missing Rx frames.
  765. * Lower value is higher energy, so we use max()!
  766. */
  767. data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck);
  768. IWL_DEBUG_CALIB("new nrg_th_cck %u\n", data->nrg_th_cck);
  769. data->nrg_prev_state = data->nrg_curr_state;
  770. return 0;
  771. }
  772. static int iwl4965_sens_auto_corr_ofdm(struct iwl4965_priv *priv,
  773. u32 norm_fa,
  774. u32 rx_enable_time)
  775. {
  776. u32 val;
  777. u32 false_alarms = norm_fa * 200 * 1024;
  778. u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time;
  779. u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time;
  780. struct iwl4965_sensitivity_data *data = NULL;
  781. data = &(priv->sensitivity_data);
  782. /* If we got too many false alarms this time, reduce sensitivity */
  783. if (false_alarms > max_false_alarms) {
  784. IWL_DEBUG_CALIB("norm FA %u > max FA %u)\n",
  785. false_alarms, max_false_alarms);
  786. val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM;
  787. data->auto_corr_ofdm =
  788. min((u32)AUTO_CORR_MAX_OFDM, val);
  789. val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM;
  790. data->auto_corr_ofdm_mrc =
  791. min((u32)AUTO_CORR_MAX_OFDM_MRC, val);
  792. val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM;
  793. data->auto_corr_ofdm_x1 =
  794. min((u32)AUTO_CORR_MAX_OFDM_X1, val);
  795. val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM;
  796. data->auto_corr_ofdm_mrc_x1 =
  797. min((u32)AUTO_CORR_MAX_OFDM_MRC_X1, val);
  798. }
  799. /* Else if we got fewer than desired, increase sensitivity */
  800. else if (false_alarms < min_false_alarms) {
  801. IWL_DEBUG_CALIB("norm FA %u < min FA %u\n",
  802. false_alarms, min_false_alarms);
  803. val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM;
  804. data->auto_corr_ofdm =
  805. max((u32)AUTO_CORR_MIN_OFDM, val);
  806. val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM;
  807. data->auto_corr_ofdm_mrc =
  808. max((u32)AUTO_CORR_MIN_OFDM_MRC, val);
  809. val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM;
  810. data->auto_corr_ofdm_x1 =
  811. max((u32)AUTO_CORR_MIN_OFDM_X1, val);
  812. val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM;
  813. data->auto_corr_ofdm_mrc_x1 =
  814. max((u32)AUTO_CORR_MIN_OFDM_MRC_X1, val);
  815. }
  816. else
  817. IWL_DEBUG_CALIB("min FA %u < norm FA %u < max FA %u OK\n",
  818. min_false_alarms, false_alarms, max_false_alarms);
  819. return 0;
  820. }
  821. static int iwl4965_sensitivity_callback(struct iwl4965_priv *priv,
  822. struct iwl4965_cmd *cmd, struct sk_buff *skb)
  823. {
  824. /* We didn't cache the SKB; let the caller free it */
  825. return 1;
  826. }
  827. /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */
  828. static int iwl4965_sensitivity_write(struct iwl4965_priv *priv, u8 flags)
  829. {
  830. int rc = 0;
  831. struct iwl4965_sensitivity_cmd cmd ;
  832. struct iwl4965_sensitivity_data *data = NULL;
  833. struct iwl4965_host_cmd cmd_out = {
  834. .id = SENSITIVITY_CMD,
  835. .len = sizeof(struct iwl4965_sensitivity_cmd),
  836. .meta.flags = flags,
  837. .data = &cmd,
  838. };
  839. data = &(priv->sensitivity_data);
  840. memset(&cmd, 0, sizeof(cmd));
  841. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] =
  842. cpu_to_le16((u16)data->auto_corr_ofdm);
  843. cmd.table[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] =
  844. cpu_to_le16((u16)data->auto_corr_ofdm_mrc);
  845. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] =
  846. cpu_to_le16((u16)data->auto_corr_ofdm_x1);
  847. cmd.table[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] =
  848. cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1);
  849. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] =
  850. cpu_to_le16((u16)data->auto_corr_cck);
  851. cmd.table[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] =
  852. cpu_to_le16((u16)data->auto_corr_cck_mrc);
  853. cmd.table[HD_MIN_ENERGY_CCK_DET_INDEX] =
  854. cpu_to_le16((u16)data->nrg_th_cck);
  855. cmd.table[HD_MIN_ENERGY_OFDM_DET_INDEX] =
  856. cpu_to_le16((u16)data->nrg_th_ofdm);
  857. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_INDEX] =
  858. __constant_cpu_to_le16(190);
  859. cmd.table[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] =
  860. __constant_cpu_to_le16(390);
  861. cmd.table[HD_OFDM_ENERGY_TH_IN_INDEX] =
  862. __constant_cpu_to_le16(62);
  863. IWL_DEBUG_CALIB("ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n",
  864. data->auto_corr_ofdm, data->auto_corr_ofdm_mrc,
  865. data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1,
  866. data->nrg_th_ofdm);
  867. IWL_DEBUG_CALIB("cck: ac %u mrc %u thresh %u\n",
  868. data->auto_corr_cck, data->auto_corr_cck_mrc,
  869. data->nrg_th_cck);
  870. /* Update uCode's "work" table, and copy it to DSP */
  871. cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE;
  872. if (flags & CMD_ASYNC)
  873. cmd_out.meta.u.callback = iwl4965_sensitivity_callback;
  874. /* Don't send command to uCode if nothing has changed */
  875. if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]),
  876. sizeof(u16)*HD_TABLE_SIZE)) {
  877. IWL_DEBUG_CALIB("No change in SENSITIVITY_CMD\n");
  878. return 0;
  879. }
  880. /* Copy table for comparison next time */
  881. memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]),
  882. sizeof(u16)*HD_TABLE_SIZE);
  883. rc = iwl4965_send_cmd(priv, &cmd_out);
  884. if (!rc) {
  885. IWL_DEBUG_CALIB("SENSITIVITY_CMD succeeded\n");
  886. return rc;
  887. }
  888. return 0;
  889. }
  890. void iwl4965_init_sensitivity(struct iwl4965_priv *priv, u8 flags, u8 force)
  891. {
  892. int rc = 0;
  893. int i;
  894. struct iwl4965_sensitivity_data *data = NULL;
  895. IWL_DEBUG_CALIB("Start iwl4965_init_sensitivity\n");
  896. if (force)
  897. memset(&(priv->sensitivity_tbl[0]), 0,
  898. sizeof(u16)*HD_TABLE_SIZE);
  899. /* Clear driver's sensitivity algo data */
  900. data = &(priv->sensitivity_data);
  901. memset(data, 0, sizeof(struct iwl4965_sensitivity_data));
  902. data->num_in_cck_no_fa = 0;
  903. data->nrg_curr_state = IWL_FA_TOO_MANY;
  904. data->nrg_prev_state = IWL_FA_TOO_MANY;
  905. data->nrg_silence_ref = 0;
  906. data->nrg_silence_idx = 0;
  907. data->nrg_energy_idx = 0;
  908. for (i = 0; i < 10; i++)
  909. data->nrg_value[i] = 0;
  910. for (i = 0; i < NRG_NUM_PREV_STAT_L; i++)
  911. data->nrg_silence_rssi[i] = 0;
  912. data->auto_corr_ofdm = 90;
  913. data->auto_corr_ofdm_mrc = 170;
  914. data->auto_corr_ofdm_x1 = 105;
  915. data->auto_corr_ofdm_mrc_x1 = 220;
  916. data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF;
  917. data->auto_corr_cck_mrc = 200;
  918. data->nrg_th_cck = 100;
  919. data->nrg_th_ofdm = 100;
  920. data->last_bad_plcp_cnt_ofdm = 0;
  921. data->last_fa_cnt_ofdm = 0;
  922. data->last_bad_plcp_cnt_cck = 0;
  923. data->last_fa_cnt_cck = 0;
  924. /* Clear prior Sensitivity command data to force send to uCode */
  925. if (force)
  926. memset(&(priv->sensitivity_tbl[0]), 0,
  927. sizeof(u16)*HD_TABLE_SIZE);
  928. rc |= iwl4965_sensitivity_write(priv, flags);
  929. IWL_DEBUG_CALIB("<<return 0x%X\n", rc);
  930. return;
  931. }
  932. /* Reset differential Rx gains in NIC to prepare for chain noise calibration.
  933. * Called after every association, but this runs only once!
  934. * ... once chain noise is calibrated the first time, it's good forever. */
  935. void iwl4965_chain_noise_reset(struct iwl4965_priv *priv)
  936. {
  937. struct iwl4965_chain_noise_data *data = NULL;
  938. int rc = 0;
  939. data = &(priv->chain_noise_data);
  940. if ((data->state == IWL_CHAIN_NOISE_ALIVE) && iwl4965_is_associated(priv)) {
  941. struct iwl4965_calibration_cmd cmd;
  942. memset(&cmd, 0, sizeof(cmd));
  943. cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
  944. cmd.diff_gain_a = 0;
  945. cmd.diff_gain_b = 0;
  946. cmd.diff_gain_c = 0;
  947. rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  948. sizeof(cmd), &cmd);
  949. msleep(4);
  950. data->state = IWL_CHAIN_NOISE_ACCUMULATE;
  951. IWL_DEBUG_CALIB("Run chain_noise_calibrate\n");
  952. }
  953. return;
  954. }
  955. /*
  956. * Accumulate 20 beacons of signal and noise statistics for each of
  957. * 3 receivers/antennas/rx-chains, then figure out:
  958. * 1) Which antennas are connected.
  959. * 2) Differential rx gain settings to balance the 3 receivers.
  960. */
  961. static void iwl4965_noise_calibration(struct iwl4965_priv *priv,
  962. struct iwl4965_notif_statistics *stat_resp)
  963. {
  964. struct iwl4965_chain_noise_data *data = NULL;
  965. int rc = 0;
  966. u32 chain_noise_a;
  967. u32 chain_noise_b;
  968. u32 chain_noise_c;
  969. u32 chain_sig_a;
  970. u32 chain_sig_b;
  971. u32 chain_sig_c;
  972. u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  973. u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE};
  974. u32 max_average_sig;
  975. u16 max_average_sig_antenna_i;
  976. u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE;
  977. u16 min_average_noise_antenna_i = INITIALIZATION_VALUE;
  978. u16 i = 0;
  979. u16 chan_num = INITIALIZATION_VALUE;
  980. u32 band = INITIALIZATION_VALUE;
  981. u32 active_chains = 0;
  982. unsigned long flags;
  983. struct statistics_rx_non_phy *rx_info = &(stat_resp->rx.general);
  984. data = &(priv->chain_noise_data);
  985. /* Accumulate just the first 20 beacons after the first association,
  986. * then we're done forever. */
  987. if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) {
  988. if (data->state == IWL_CHAIN_NOISE_ALIVE)
  989. IWL_DEBUG_CALIB("Wait for noise calib reset\n");
  990. return;
  991. }
  992. spin_lock_irqsave(&priv->lock, flags);
  993. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  994. IWL_DEBUG_CALIB(" << Interference data unavailable\n");
  995. spin_unlock_irqrestore(&priv->lock, flags);
  996. return;
  997. }
  998. band = (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) ? 0 : 1;
  999. chan_num = le16_to_cpu(priv->staging_rxon.channel);
  1000. /* Make sure we accumulate data for just the associated channel
  1001. * (even if scanning). */
  1002. if ((chan_num != (le32_to_cpu(stat_resp->flag) >> 16)) ||
  1003. ((STATISTICS_REPLY_FLG_BAND_24G_MSK ==
  1004. (stat_resp->flag & STATISTICS_REPLY_FLG_BAND_24G_MSK)) && band)) {
  1005. IWL_DEBUG_CALIB("Stats not from chan=%d, band=%d\n",
  1006. chan_num, band);
  1007. spin_unlock_irqrestore(&priv->lock, flags);
  1008. return;
  1009. }
  1010. /* Accumulate beacon statistics values across 20 beacons */
  1011. chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) &
  1012. IN_BAND_FILTER;
  1013. chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) &
  1014. IN_BAND_FILTER;
  1015. chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) &
  1016. IN_BAND_FILTER;
  1017. chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER;
  1018. chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER;
  1019. chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER;
  1020. spin_unlock_irqrestore(&priv->lock, flags);
  1021. data->beacon_count++;
  1022. data->chain_noise_a = (chain_noise_a + data->chain_noise_a);
  1023. data->chain_noise_b = (chain_noise_b + data->chain_noise_b);
  1024. data->chain_noise_c = (chain_noise_c + data->chain_noise_c);
  1025. data->chain_signal_a = (chain_sig_a + data->chain_signal_a);
  1026. data->chain_signal_b = (chain_sig_b + data->chain_signal_b);
  1027. data->chain_signal_c = (chain_sig_c + data->chain_signal_c);
  1028. IWL_DEBUG_CALIB("chan=%d, band=%d, beacon=%d\n", chan_num, band,
  1029. data->beacon_count);
  1030. IWL_DEBUG_CALIB("chain_sig: a %d b %d c %d\n",
  1031. chain_sig_a, chain_sig_b, chain_sig_c);
  1032. IWL_DEBUG_CALIB("chain_noise: a %d b %d c %d\n",
  1033. chain_noise_a, chain_noise_b, chain_noise_c);
  1034. /* If this is the 20th beacon, determine:
  1035. * 1) Disconnected antennas (using signal strengths)
  1036. * 2) Differential gain (using silence noise) to balance receivers */
  1037. if (data->beacon_count == CAL_NUM_OF_BEACONS) {
  1038. /* Analyze signal for disconnected antenna */
  1039. average_sig[0] = (data->chain_signal_a) / CAL_NUM_OF_BEACONS;
  1040. average_sig[1] = (data->chain_signal_b) / CAL_NUM_OF_BEACONS;
  1041. average_sig[2] = (data->chain_signal_c) / CAL_NUM_OF_BEACONS;
  1042. if (average_sig[0] >= average_sig[1]) {
  1043. max_average_sig = average_sig[0];
  1044. max_average_sig_antenna_i = 0;
  1045. active_chains = (1 << max_average_sig_antenna_i);
  1046. } else {
  1047. max_average_sig = average_sig[1];
  1048. max_average_sig_antenna_i = 1;
  1049. active_chains = (1 << max_average_sig_antenna_i);
  1050. }
  1051. if (average_sig[2] >= max_average_sig) {
  1052. max_average_sig = average_sig[2];
  1053. max_average_sig_antenna_i = 2;
  1054. active_chains = (1 << max_average_sig_antenna_i);
  1055. }
  1056. IWL_DEBUG_CALIB("average_sig: a %d b %d c %d\n",
  1057. average_sig[0], average_sig[1], average_sig[2]);
  1058. IWL_DEBUG_CALIB("max_average_sig = %d, antenna %d\n",
  1059. max_average_sig, max_average_sig_antenna_i);
  1060. /* Compare signal strengths for all 3 receivers. */
  1061. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1062. if (i != max_average_sig_antenna_i) {
  1063. s32 rssi_delta = (max_average_sig -
  1064. average_sig[i]);
  1065. /* If signal is very weak, compared with
  1066. * strongest, mark it as disconnected. */
  1067. if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS)
  1068. data->disconn_array[i] = 1;
  1069. else
  1070. active_chains |= (1 << i);
  1071. IWL_DEBUG_CALIB("i = %d rssiDelta = %d "
  1072. "disconn_array[i] = %d\n",
  1073. i, rssi_delta, data->disconn_array[i]);
  1074. }
  1075. }
  1076. /*If both chains A & B are disconnected -
  1077. * connect B and leave A as is */
  1078. if (data->disconn_array[CHAIN_A] &&
  1079. data->disconn_array[CHAIN_B]) {
  1080. data->disconn_array[CHAIN_B] = 0;
  1081. active_chains |= (1 << CHAIN_B);
  1082. IWL_DEBUG_CALIB("both A & B chains are disconnected! "
  1083. "W/A - declare B as connected\n");
  1084. }
  1085. IWL_DEBUG_CALIB("active_chains (bitwise) = 0x%x\n",
  1086. active_chains);
  1087. /* Save for use within RXON, TX, SCAN commands, etc. */
  1088. priv->valid_antenna = active_chains;
  1089. /* Analyze noise for rx balance */
  1090. average_noise[0] = ((data->chain_noise_a)/CAL_NUM_OF_BEACONS);
  1091. average_noise[1] = ((data->chain_noise_b)/CAL_NUM_OF_BEACONS);
  1092. average_noise[2] = ((data->chain_noise_c)/CAL_NUM_OF_BEACONS);
  1093. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1094. if (!(data->disconn_array[i]) &&
  1095. (average_noise[i] <= min_average_noise)) {
  1096. /* This means that chain i is active and has
  1097. * lower noise values so far: */
  1098. min_average_noise = average_noise[i];
  1099. min_average_noise_antenna_i = i;
  1100. }
  1101. }
  1102. data->delta_gain_code[min_average_noise_antenna_i] = 0;
  1103. IWL_DEBUG_CALIB("average_noise: a %d b %d c %d\n",
  1104. average_noise[0], average_noise[1],
  1105. average_noise[2]);
  1106. IWL_DEBUG_CALIB("min_average_noise = %d, antenna %d\n",
  1107. min_average_noise, min_average_noise_antenna_i);
  1108. for (i = 0; i < NUM_RX_CHAINS; i++) {
  1109. s32 delta_g = 0;
  1110. if (!(data->disconn_array[i]) &&
  1111. (data->delta_gain_code[i] ==
  1112. CHAIN_NOISE_DELTA_GAIN_INIT_VAL)) {
  1113. delta_g = average_noise[i] - min_average_noise;
  1114. data->delta_gain_code[i] = (u8)((delta_g *
  1115. 10) / 15);
  1116. if (CHAIN_NOISE_MAX_DELTA_GAIN_CODE <
  1117. data->delta_gain_code[i])
  1118. data->delta_gain_code[i] =
  1119. CHAIN_NOISE_MAX_DELTA_GAIN_CODE;
  1120. data->delta_gain_code[i] =
  1121. (data->delta_gain_code[i] | (1 << 2));
  1122. } else
  1123. data->delta_gain_code[i] = 0;
  1124. }
  1125. IWL_DEBUG_CALIB("delta_gain_codes: a %d b %d c %d\n",
  1126. data->delta_gain_code[0],
  1127. data->delta_gain_code[1],
  1128. data->delta_gain_code[2]);
  1129. /* Differential gain gets sent to uCode only once */
  1130. if (!data->radio_write) {
  1131. struct iwl4965_calibration_cmd cmd;
  1132. data->radio_write = 1;
  1133. memset(&cmd, 0, sizeof(cmd));
  1134. cmd.opCode = PHY_CALIBRATE_DIFF_GAIN_CMD;
  1135. cmd.diff_gain_a = data->delta_gain_code[0];
  1136. cmd.diff_gain_b = data->delta_gain_code[1];
  1137. cmd.diff_gain_c = data->delta_gain_code[2];
  1138. rc = iwl4965_send_cmd_pdu(priv, REPLY_PHY_CALIBRATION_CMD,
  1139. sizeof(cmd), &cmd);
  1140. if (rc)
  1141. IWL_DEBUG_CALIB("fail sending cmd "
  1142. "REPLY_PHY_CALIBRATION_CMD \n");
  1143. /* TODO we might want recalculate
  1144. * rx_chain in rxon cmd */
  1145. /* Mark so we run this algo only once! */
  1146. data->state = IWL_CHAIN_NOISE_CALIBRATED;
  1147. }
  1148. data->chain_noise_a = 0;
  1149. data->chain_noise_b = 0;
  1150. data->chain_noise_c = 0;
  1151. data->chain_signal_a = 0;
  1152. data->chain_signal_b = 0;
  1153. data->chain_signal_c = 0;
  1154. data->beacon_count = 0;
  1155. }
  1156. return;
  1157. }
  1158. static void iwl4965_sensitivity_calibration(struct iwl4965_priv *priv,
  1159. struct iwl4965_notif_statistics *resp)
  1160. {
  1161. int rc = 0;
  1162. u32 rx_enable_time;
  1163. u32 fa_cck;
  1164. u32 fa_ofdm;
  1165. u32 bad_plcp_cck;
  1166. u32 bad_plcp_ofdm;
  1167. u32 norm_fa_ofdm;
  1168. u32 norm_fa_cck;
  1169. struct iwl4965_sensitivity_data *data = NULL;
  1170. struct statistics_rx_non_phy *rx_info = &(resp->rx.general);
  1171. struct statistics_rx *statistics = &(resp->rx);
  1172. unsigned long flags;
  1173. struct statistics_general_data statis;
  1174. data = &(priv->sensitivity_data);
  1175. if (!iwl4965_is_associated(priv)) {
  1176. IWL_DEBUG_CALIB("<< - not associated\n");
  1177. return;
  1178. }
  1179. spin_lock_irqsave(&priv->lock, flags);
  1180. if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) {
  1181. IWL_DEBUG_CALIB("<< invalid data.\n");
  1182. spin_unlock_irqrestore(&priv->lock, flags);
  1183. return;
  1184. }
  1185. /* Extract Statistics: */
  1186. rx_enable_time = le32_to_cpu(rx_info->channel_load);
  1187. fa_cck = le32_to_cpu(statistics->cck.false_alarm_cnt);
  1188. fa_ofdm = le32_to_cpu(statistics->ofdm.false_alarm_cnt);
  1189. bad_plcp_cck = le32_to_cpu(statistics->cck.plcp_err);
  1190. bad_plcp_ofdm = le32_to_cpu(statistics->ofdm.plcp_err);
  1191. statis.beacon_silence_rssi_a =
  1192. le32_to_cpu(statistics->general.beacon_silence_rssi_a);
  1193. statis.beacon_silence_rssi_b =
  1194. le32_to_cpu(statistics->general.beacon_silence_rssi_b);
  1195. statis.beacon_silence_rssi_c =
  1196. le32_to_cpu(statistics->general.beacon_silence_rssi_c);
  1197. statis.beacon_energy_a =
  1198. le32_to_cpu(statistics->general.beacon_energy_a);
  1199. statis.beacon_energy_b =
  1200. le32_to_cpu(statistics->general.beacon_energy_b);
  1201. statis.beacon_energy_c =
  1202. le32_to_cpu(statistics->general.beacon_energy_c);
  1203. spin_unlock_irqrestore(&priv->lock, flags);
  1204. IWL_DEBUG_CALIB("rx_enable_time = %u usecs\n", rx_enable_time);
  1205. if (!rx_enable_time) {
  1206. IWL_DEBUG_CALIB("<< RX Enable Time == 0! \n");
  1207. return;
  1208. }
  1209. /* These statistics increase monotonically, and do not reset
  1210. * at each beacon. Calculate difference from last value, or just
  1211. * use the new statistics value if it has reset or wrapped around. */
  1212. if (data->last_bad_plcp_cnt_cck > bad_plcp_cck)
  1213. data->last_bad_plcp_cnt_cck = bad_plcp_cck;
  1214. else {
  1215. bad_plcp_cck -= data->last_bad_plcp_cnt_cck;
  1216. data->last_bad_plcp_cnt_cck += bad_plcp_cck;
  1217. }
  1218. if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm)
  1219. data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm;
  1220. else {
  1221. bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm;
  1222. data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm;
  1223. }
  1224. if (data->last_fa_cnt_ofdm > fa_ofdm)
  1225. data->last_fa_cnt_ofdm = fa_ofdm;
  1226. else {
  1227. fa_ofdm -= data->last_fa_cnt_ofdm;
  1228. data->last_fa_cnt_ofdm += fa_ofdm;
  1229. }
  1230. if (data->last_fa_cnt_cck > fa_cck)
  1231. data->last_fa_cnt_cck = fa_cck;
  1232. else {
  1233. fa_cck -= data->last_fa_cnt_cck;
  1234. data->last_fa_cnt_cck += fa_cck;
  1235. }
  1236. /* Total aborted signal locks */
  1237. norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm;
  1238. norm_fa_cck = fa_cck + bad_plcp_cck;
  1239. IWL_DEBUG_CALIB("cck: fa %u badp %u ofdm: fa %u badp %u\n", fa_cck,
  1240. bad_plcp_cck, fa_ofdm, bad_plcp_ofdm);
  1241. iwl4965_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time);
  1242. iwl4965_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis);
  1243. rc |= iwl4965_sensitivity_write(priv, CMD_ASYNC);
  1244. return;
  1245. }
  1246. static void iwl4965_bg_sensitivity_work(struct work_struct *work)
  1247. {
  1248. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  1249. sensitivity_work);
  1250. mutex_lock(&priv->mutex);
  1251. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  1252. test_bit(STATUS_SCANNING, &priv->status)) {
  1253. mutex_unlock(&priv->mutex);
  1254. return;
  1255. }
  1256. if (priv->start_calib) {
  1257. iwl4965_noise_calibration(priv, &priv->statistics);
  1258. if (priv->sensitivity_data.state ==
  1259. IWL_SENS_CALIB_NEED_REINIT) {
  1260. iwl4965_init_sensitivity(priv, CMD_ASYNC, 0);
  1261. priv->sensitivity_data.state = IWL_SENS_CALIB_ALLOWED;
  1262. } else
  1263. iwl4965_sensitivity_calibration(priv,
  1264. &priv->statistics);
  1265. }
  1266. mutex_unlock(&priv->mutex);
  1267. return;
  1268. }
  1269. #endif /*CONFIG_IWL4965_SENSITIVITY*/
  1270. static void iwl4965_bg_txpower_work(struct work_struct *work)
  1271. {
  1272. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  1273. txpower_work);
  1274. /* If a scan happened to start before we got here
  1275. * then just return; the statistics notification will
  1276. * kick off another scheduled work to compensate for
  1277. * any temperature delta we missed here. */
  1278. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  1279. test_bit(STATUS_SCANNING, &priv->status))
  1280. return;
  1281. mutex_lock(&priv->mutex);
  1282. /* Regardless of if we are assocaited, we must reconfigure the
  1283. * TX power since frames can be sent on non-radar channels while
  1284. * not associated */
  1285. iwl4965_hw_reg_send_txpower(priv);
  1286. /* Update last_temperature to keep is_calib_needed from running
  1287. * when it isn't needed... */
  1288. priv->last_temperature = priv->temperature;
  1289. mutex_unlock(&priv->mutex);
  1290. }
  1291. /*
  1292. * Acquire priv->lock before calling this function !
  1293. */
  1294. static void iwl4965_set_wr_ptrs(struct iwl4965_priv *priv, int txq_id, u32 index)
  1295. {
  1296. iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
  1297. (index & 0xff) | (txq_id << 8));
  1298. iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(txq_id), index);
  1299. }
  1300. /*
  1301. * Acquire priv->lock before calling this function !
  1302. */
  1303. static void iwl4965_tx_queue_set_status(struct iwl4965_priv *priv,
  1304. struct iwl4965_tx_queue *txq,
  1305. int tx_fifo_id, int scd_retry)
  1306. {
  1307. int txq_id = txq->q.id;
  1308. int active = test_bit(txq_id, &priv->txq_ctx_active_msk)?1:0;
  1309. iwl4965_write_prph(priv, KDR_SCD_QUEUE_STATUS_BITS(txq_id),
  1310. (active << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1311. (tx_fifo_id << SCD_QUEUE_STTS_REG_POS_TXF) |
  1312. (scd_retry << SCD_QUEUE_STTS_REG_POS_WSL) |
  1313. (scd_retry << SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  1314. SCD_QUEUE_STTS_REG_MSK);
  1315. txq->sched_retry = scd_retry;
  1316. IWL_DEBUG_INFO("%s %s Queue %d on AC %d\n",
  1317. active ? "Activete" : "Deactivate",
  1318. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  1319. }
  1320. static const u16 default_queue_to_tx_fifo[] = {
  1321. IWL_TX_FIFO_AC3,
  1322. IWL_TX_FIFO_AC2,
  1323. IWL_TX_FIFO_AC1,
  1324. IWL_TX_FIFO_AC0,
  1325. IWL_CMD_FIFO_NUM,
  1326. IWL_TX_FIFO_HCCA_1,
  1327. IWL_TX_FIFO_HCCA_2
  1328. };
  1329. static inline void iwl4965_txq_ctx_activate(struct iwl4965_priv *priv, int txq_id)
  1330. {
  1331. set_bit(txq_id, &priv->txq_ctx_active_msk);
  1332. }
  1333. static inline void iwl4965_txq_ctx_deactivate(struct iwl4965_priv *priv, int txq_id)
  1334. {
  1335. clear_bit(txq_id, &priv->txq_ctx_active_msk);
  1336. }
  1337. int iwl4965_alive_notify(struct iwl4965_priv *priv)
  1338. {
  1339. u32 a;
  1340. int i = 0;
  1341. unsigned long flags;
  1342. int rc;
  1343. spin_lock_irqsave(&priv->lock, flags);
  1344. #ifdef CONFIG_IWL4965_SENSITIVITY
  1345. memset(&(priv->sensitivity_data), 0,
  1346. sizeof(struct iwl4965_sensitivity_data));
  1347. memset(&(priv->chain_noise_data), 0,
  1348. sizeof(struct iwl4965_chain_noise_data));
  1349. for (i = 0; i < NUM_RX_CHAINS; i++)
  1350. priv->chain_noise_data.delta_gain_code[i] =
  1351. CHAIN_NOISE_DELTA_GAIN_INIT_VAL;
  1352. #endif /* CONFIG_IWL4965_SENSITIVITY*/
  1353. rc = iwl4965_grab_nic_access(priv);
  1354. if (rc) {
  1355. spin_unlock_irqrestore(&priv->lock, flags);
  1356. return rc;
  1357. }
  1358. priv->scd_base_addr = iwl4965_read_prph(priv, KDR_SCD_SRAM_BASE_ADDR);
  1359. a = priv->scd_base_addr + SCD_CONTEXT_DATA_OFFSET;
  1360. for (; a < priv->scd_base_addr + SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  1361. iwl4965_write_targ_mem(priv, a, 0);
  1362. for (; a < priv->scd_base_addr + SCD_TRANSLATE_TBL_OFFSET; a += 4)
  1363. iwl4965_write_targ_mem(priv, a, 0);
  1364. for (; a < sizeof(u16) * priv->hw_setting.max_txq_num; a += 4)
  1365. iwl4965_write_targ_mem(priv, a, 0);
  1366. iwl4965_write_prph(priv, KDR_SCD_DRAM_BASE_ADDR,
  1367. (priv->hw_setting.shared_phys +
  1368. offsetof(struct iwl4965_shared, queues_byte_cnt_tbls)) >> 10);
  1369. iwl4965_write_prph(priv, KDR_SCD_QUEUECHAIN_SEL, 0);
  1370. /* initiate the queues */
  1371. for (i = 0; i < priv->hw_setting.max_txq_num; i++) {
  1372. iwl4965_write_prph(priv, KDR_SCD_QUEUE_RDPTR(i), 0);
  1373. iwl4965_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  1374. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  1375. SCD_CONTEXT_QUEUE_OFFSET(i),
  1376. (SCD_WIN_SIZE <<
  1377. SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  1378. SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1379. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  1380. SCD_CONTEXT_QUEUE_OFFSET(i) +
  1381. sizeof(u32),
  1382. (SCD_FRAME_LIMIT <<
  1383. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1384. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1385. }
  1386. iwl4965_write_prph(priv, KDR_SCD_INTERRUPT_MASK,
  1387. (1 << priv->hw_setting.max_txq_num) - 1);
  1388. iwl4965_write_prph(priv, KDR_SCD_TXFACT,
  1389. SCD_TXFACT_REG_TXFIFO_MASK(0, 7));
  1390. iwl4965_set_wr_ptrs(priv, IWL_CMD_QUEUE_NUM, 0);
  1391. /* map qos queues to fifos one-to-one */
  1392. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  1393. int ac = default_queue_to_tx_fifo[i];
  1394. iwl4965_txq_ctx_activate(priv, i);
  1395. iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
  1396. }
  1397. iwl4965_release_nic_access(priv);
  1398. spin_unlock_irqrestore(&priv->lock, flags);
  1399. return 0;
  1400. }
  1401. int iwl4965_hw_set_hw_setting(struct iwl4965_priv *priv)
  1402. {
  1403. priv->hw_setting.shared_virt =
  1404. pci_alloc_consistent(priv->pci_dev,
  1405. sizeof(struct iwl4965_shared),
  1406. &priv->hw_setting.shared_phys);
  1407. if (!priv->hw_setting.shared_virt)
  1408. return -1;
  1409. memset(priv->hw_setting.shared_virt, 0, sizeof(struct iwl4965_shared));
  1410. priv->hw_setting.max_txq_num = iwl4965_param_queues_num;
  1411. priv->hw_setting.ac_queue_count = AC_NUM;
  1412. priv->hw_setting.tx_cmd_len = sizeof(struct iwl4965_tx_cmd);
  1413. priv->hw_setting.max_rxq_size = RX_QUEUE_SIZE;
  1414. priv->hw_setting.max_rxq_log = RX_QUEUE_SIZE_LOG;
  1415. priv->hw_setting.max_stations = IWL4965_STATION_COUNT;
  1416. priv->hw_setting.bcast_sta_id = IWL4965_BROADCAST_ID;
  1417. return 0;
  1418. }
  1419. /**
  1420. * iwl4965_hw_txq_ctx_free - Free TXQ Context
  1421. *
  1422. * Destroy all TX DMA queues and structures
  1423. */
  1424. void iwl4965_hw_txq_ctx_free(struct iwl4965_priv *priv)
  1425. {
  1426. int txq_id;
  1427. /* Tx queues */
  1428. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
  1429. iwl4965_tx_queue_free(priv, &priv->txq[txq_id]);
  1430. iwl4965_kw_free(priv);
  1431. }
  1432. /**
  1433. * iwl4965_hw_txq_free_tfd - Free one TFD, those at index [txq->q.read_ptr]
  1434. *
  1435. * Does NOT advance any indexes
  1436. */
  1437. int iwl4965_hw_txq_free_tfd(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
  1438. {
  1439. struct iwl4965_tfd_frame *bd_tmp = (struct iwl4965_tfd_frame *)&txq->bd[0];
  1440. struct iwl4965_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  1441. struct pci_dev *dev = priv->pci_dev;
  1442. int i;
  1443. int counter = 0;
  1444. int index, is_odd;
  1445. /* classify bd */
  1446. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  1447. /* nothing to cleanup after for host commands */
  1448. return 0;
  1449. /* sanity check */
  1450. counter = IWL_GET_BITS(*bd, num_tbs);
  1451. if (counter > MAX_NUM_OF_TBS) {
  1452. IWL_ERROR("Too many chunks: %i\n", counter);
  1453. /* @todo issue fatal error, it is quite serious situation */
  1454. return 0;
  1455. }
  1456. /* unmap chunks if any */
  1457. for (i = 0; i < counter; i++) {
  1458. index = i / 2;
  1459. is_odd = i & 0x1;
  1460. if (is_odd)
  1461. pci_unmap_single(
  1462. dev,
  1463. IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  1464. (IWL_GET_BITS(bd->pa[index],
  1465. tb2_addr_hi20) << 16),
  1466. IWL_GET_BITS(bd->pa[index], tb2_len),
  1467. PCI_DMA_TODEVICE);
  1468. else if (i > 0)
  1469. pci_unmap_single(dev,
  1470. le32_to_cpu(bd->pa[index].tb1_addr),
  1471. IWL_GET_BITS(bd->pa[index], tb1_len),
  1472. PCI_DMA_TODEVICE);
  1473. if (txq->txb[txq->q.read_ptr].skb[i]) {
  1474. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
  1475. dev_kfree_skb(skb);
  1476. txq->txb[txq->q.read_ptr].skb[i] = NULL;
  1477. }
  1478. }
  1479. return 0;
  1480. }
  1481. int iwl4965_hw_reg_set_txpower(struct iwl4965_priv *priv, s8 power)
  1482. {
  1483. IWL_ERROR("TODO: Implement iwl4965_hw_reg_set_txpower!\n");
  1484. return -EINVAL;
  1485. }
  1486. static s32 iwl4965_math_div_round(s32 num, s32 denom, s32 *res)
  1487. {
  1488. s32 sign = 1;
  1489. if (num < 0) {
  1490. sign = -sign;
  1491. num = -num;
  1492. }
  1493. if (denom < 0) {
  1494. sign = -sign;
  1495. denom = -denom;
  1496. }
  1497. *res = 1;
  1498. *res = ((num * 2 + denom) / (denom * 2)) * sign;
  1499. return 1;
  1500. }
  1501. static s32 iwl4965_get_voltage_compensation(s32 eeprom_voltage,
  1502. s32 current_voltage)
  1503. {
  1504. s32 comp = 0;
  1505. if ((TX_POWER_IWL_ILLEGAL_VOLTAGE == eeprom_voltage) ||
  1506. (TX_POWER_IWL_ILLEGAL_VOLTAGE == current_voltage))
  1507. return 0;
  1508. iwl4965_math_div_round(current_voltage - eeprom_voltage,
  1509. TX_POWER_IWL_VOLTAGE_CODES_PER_03V, &comp);
  1510. if (current_voltage > eeprom_voltage)
  1511. comp *= 2;
  1512. if ((comp < -2) || (comp > 2))
  1513. comp = 0;
  1514. return comp;
  1515. }
  1516. static const struct iwl4965_channel_info *
  1517. iwl4965_get_channel_txpower_info(struct iwl4965_priv *priv, u8 phymode, u16 channel)
  1518. {
  1519. const struct iwl4965_channel_info *ch_info;
  1520. ch_info = iwl4965_get_channel_info(priv, phymode, channel);
  1521. if (!is_channel_valid(ch_info))
  1522. return NULL;
  1523. return ch_info;
  1524. }
  1525. static s32 iwl4965_get_tx_atten_grp(u16 channel)
  1526. {
  1527. if (channel >= CALIB_IWL_TX_ATTEN_GR5_FCH &&
  1528. channel <= CALIB_IWL_TX_ATTEN_GR5_LCH)
  1529. return CALIB_CH_GROUP_5;
  1530. if (channel >= CALIB_IWL_TX_ATTEN_GR1_FCH &&
  1531. channel <= CALIB_IWL_TX_ATTEN_GR1_LCH)
  1532. return CALIB_CH_GROUP_1;
  1533. if (channel >= CALIB_IWL_TX_ATTEN_GR2_FCH &&
  1534. channel <= CALIB_IWL_TX_ATTEN_GR2_LCH)
  1535. return CALIB_CH_GROUP_2;
  1536. if (channel >= CALIB_IWL_TX_ATTEN_GR3_FCH &&
  1537. channel <= CALIB_IWL_TX_ATTEN_GR3_LCH)
  1538. return CALIB_CH_GROUP_3;
  1539. if (channel >= CALIB_IWL_TX_ATTEN_GR4_FCH &&
  1540. channel <= CALIB_IWL_TX_ATTEN_GR4_LCH)
  1541. return CALIB_CH_GROUP_4;
  1542. IWL_ERROR("Can't find txatten group for channel %d.\n", channel);
  1543. return -1;
  1544. }
  1545. static u32 iwl4965_get_sub_band(const struct iwl4965_priv *priv, u32 channel)
  1546. {
  1547. s32 b = -1;
  1548. for (b = 0; b < EEPROM_TX_POWER_BANDS; b++) {
  1549. if (priv->eeprom.calib_info.band_info[b].ch_from == 0)
  1550. continue;
  1551. if ((channel >= priv->eeprom.calib_info.band_info[b].ch_from)
  1552. && (channel <= priv->eeprom.calib_info.band_info[b].ch_to))
  1553. break;
  1554. }
  1555. return b;
  1556. }
  1557. static s32 iwl4965_interpolate_value(s32 x, s32 x1, s32 y1, s32 x2, s32 y2)
  1558. {
  1559. s32 val;
  1560. if (x2 == x1)
  1561. return y1;
  1562. else {
  1563. iwl4965_math_div_round((x2 - x) * (y1 - y2), (x2 - x1), &val);
  1564. return val + y2;
  1565. }
  1566. }
  1567. static int iwl4965_interpolate_chan(struct iwl4965_priv *priv, u32 channel,
  1568. struct iwl4965_eeprom_calib_ch_info *chan_info)
  1569. {
  1570. s32 s = -1;
  1571. u32 c;
  1572. u32 m;
  1573. const struct iwl4965_eeprom_calib_measure *m1;
  1574. const struct iwl4965_eeprom_calib_measure *m2;
  1575. struct iwl4965_eeprom_calib_measure *omeas;
  1576. u32 ch_i1;
  1577. u32 ch_i2;
  1578. s = iwl4965_get_sub_band(priv, channel);
  1579. if (s >= EEPROM_TX_POWER_BANDS) {
  1580. IWL_ERROR("Tx Power can not find channel %d ", channel);
  1581. return -1;
  1582. }
  1583. ch_i1 = priv->eeprom.calib_info.band_info[s].ch1.ch_num;
  1584. ch_i2 = priv->eeprom.calib_info.band_info[s].ch2.ch_num;
  1585. chan_info->ch_num = (u8) channel;
  1586. IWL_DEBUG_TXPOWER("channel %d subband %d factory cal ch %d & %d\n",
  1587. channel, s, ch_i1, ch_i2);
  1588. for (c = 0; c < EEPROM_TX_POWER_TX_CHAINS; c++) {
  1589. for (m = 0; m < EEPROM_TX_POWER_MEASUREMENTS; m++) {
  1590. m1 = &(priv->eeprom.calib_info.band_info[s].ch1.
  1591. measurements[c][m]);
  1592. m2 = &(priv->eeprom.calib_info.band_info[s].ch2.
  1593. measurements[c][m]);
  1594. omeas = &(chan_info->measurements[c][m]);
  1595. omeas->actual_pow =
  1596. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1597. m1->actual_pow,
  1598. ch_i2,
  1599. m2->actual_pow);
  1600. omeas->gain_idx =
  1601. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1602. m1->gain_idx, ch_i2,
  1603. m2->gain_idx);
  1604. omeas->temperature =
  1605. (u8) iwl4965_interpolate_value(channel, ch_i1,
  1606. m1->temperature,
  1607. ch_i2,
  1608. m2->temperature);
  1609. omeas->pa_det =
  1610. (s8) iwl4965_interpolate_value(channel, ch_i1,
  1611. m1->pa_det, ch_i2,
  1612. m2->pa_det);
  1613. IWL_DEBUG_TXPOWER
  1614. ("chain %d meas %d AP1=%d AP2=%d AP=%d\n", c, m,
  1615. m1->actual_pow, m2->actual_pow, omeas->actual_pow);
  1616. IWL_DEBUG_TXPOWER
  1617. ("chain %d meas %d NI1=%d NI2=%d NI=%d\n", c, m,
  1618. m1->gain_idx, m2->gain_idx, omeas->gain_idx);
  1619. IWL_DEBUG_TXPOWER
  1620. ("chain %d meas %d PA1=%d PA2=%d PA=%d\n", c, m,
  1621. m1->pa_det, m2->pa_det, omeas->pa_det);
  1622. IWL_DEBUG_TXPOWER
  1623. ("chain %d meas %d T1=%d T2=%d T=%d\n", c, m,
  1624. m1->temperature, m2->temperature,
  1625. omeas->temperature);
  1626. }
  1627. }
  1628. return 0;
  1629. }
  1630. /* bit-rate-dependent table to prevent Tx distortion, in half-dB units,
  1631. * for OFDM 6, 12, 18, 24, 36, 48, 54, 60 MBit, and CCK all rates. */
  1632. static s32 back_off_table[] = {
  1633. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 20 MHz */
  1634. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 20 MHz */
  1635. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM SISO 40 MHz */
  1636. 10, 10, 10, 10, 10, 15, 17, 20, /* OFDM MIMO 40 MHz */
  1637. 10 /* CCK */
  1638. };
  1639. /* Thermal compensation values for txpower for various frequency ranges ...
  1640. * ratios from 3:1 to 4.5:1 of degrees (Celsius) per half-dB gain adjust */
  1641. static struct iwl4965_txpower_comp_entry {
  1642. s32 degrees_per_05db_a;
  1643. s32 degrees_per_05db_a_denom;
  1644. } tx_power_cmp_tble[CALIB_CH_GROUP_MAX] = {
  1645. {9, 2}, /* group 0 5.2, ch 34-43 */
  1646. {4, 1}, /* group 1 5.2, ch 44-70 */
  1647. {4, 1}, /* group 2 5.2, ch 71-124 */
  1648. {4, 1}, /* group 3 5.2, ch 125-200 */
  1649. {3, 1} /* group 4 2.4, ch all */
  1650. };
  1651. static s32 get_min_power_index(s32 rate_power_index, u32 band)
  1652. {
  1653. if (!band) {
  1654. if ((rate_power_index & 7) <= 4)
  1655. return MIN_TX_GAIN_INDEX_52GHZ_EXT;
  1656. }
  1657. return MIN_TX_GAIN_INDEX;
  1658. }
  1659. struct gain_entry {
  1660. u8 dsp;
  1661. u8 radio;
  1662. };
  1663. static const struct gain_entry gain_table[2][108] = {
  1664. /* 5.2GHz power gain index table */
  1665. {
  1666. {123, 0x3F}, /* highest txpower */
  1667. {117, 0x3F},
  1668. {110, 0x3F},
  1669. {104, 0x3F},
  1670. {98, 0x3F},
  1671. {110, 0x3E},
  1672. {104, 0x3E},
  1673. {98, 0x3E},
  1674. {110, 0x3D},
  1675. {104, 0x3D},
  1676. {98, 0x3D},
  1677. {110, 0x3C},
  1678. {104, 0x3C},
  1679. {98, 0x3C},
  1680. {110, 0x3B},
  1681. {104, 0x3B},
  1682. {98, 0x3B},
  1683. {110, 0x3A},
  1684. {104, 0x3A},
  1685. {98, 0x3A},
  1686. {110, 0x39},
  1687. {104, 0x39},
  1688. {98, 0x39},
  1689. {110, 0x38},
  1690. {104, 0x38},
  1691. {98, 0x38},
  1692. {110, 0x37},
  1693. {104, 0x37},
  1694. {98, 0x37},
  1695. {110, 0x36},
  1696. {104, 0x36},
  1697. {98, 0x36},
  1698. {110, 0x35},
  1699. {104, 0x35},
  1700. {98, 0x35},
  1701. {110, 0x34},
  1702. {104, 0x34},
  1703. {98, 0x34},
  1704. {110, 0x33},
  1705. {104, 0x33},
  1706. {98, 0x33},
  1707. {110, 0x32},
  1708. {104, 0x32},
  1709. {98, 0x32},
  1710. {110, 0x31},
  1711. {104, 0x31},
  1712. {98, 0x31},
  1713. {110, 0x30},
  1714. {104, 0x30},
  1715. {98, 0x30},
  1716. {110, 0x25},
  1717. {104, 0x25},
  1718. {98, 0x25},
  1719. {110, 0x24},
  1720. {104, 0x24},
  1721. {98, 0x24},
  1722. {110, 0x23},
  1723. {104, 0x23},
  1724. {98, 0x23},
  1725. {110, 0x22},
  1726. {104, 0x18},
  1727. {98, 0x18},
  1728. {110, 0x17},
  1729. {104, 0x17},
  1730. {98, 0x17},
  1731. {110, 0x16},
  1732. {104, 0x16},
  1733. {98, 0x16},
  1734. {110, 0x15},
  1735. {104, 0x15},
  1736. {98, 0x15},
  1737. {110, 0x14},
  1738. {104, 0x14},
  1739. {98, 0x14},
  1740. {110, 0x13},
  1741. {104, 0x13},
  1742. {98, 0x13},
  1743. {110, 0x12},
  1744. {104, 0x08},
  1745. {98, 0x08},
  1746. {110, 0x07},
  1747. {104, 0x07},
  1748. {98, 0x07},
  1749. {110, 0x06},
  1750. {104, 0x06},
  1751. {98, 0x06},
  1752. {110, 0x05},
  1753. {104, 0x05},
  1754. {98, 0x05},
  1755. {110, 0x04},
  1756. {104, 0x04},
  1757. {98, 0x04},
  1758. {110, 0x03},
  1759. {104, 0x03},
  1760. {98, 0x03},
  1761. {110, 0x02},
  1762. {104, 0x02},
  1763. {98, 0x02},
  1764. {110, 0x01},
  1765. {104, 0x01},
  1766. {98, 0x01},
  1767. {110, 0x00},
  1768. {104, 0x00},
  1769. {98, 0x00},
  1770. {93, 0x00},
  1771. {88, 0x00},
  1772. {83, 0x00},
  1773. {78, 0x00},
  1774. },
  1775. /* 2.4GHz power gain index table */
  1776. {
  1777. {110, 0x3f}, /* highest txpower */
  1778. {104, 0x3f},
  1779. {98, 0x3f},
  1780. {110, 0x3e},
  1781. {104, 0x3e},
  1782. {98, 0x3e},
  1783. {110, 0x3d},
  1784. {104, 0x3d},
  1785. {98, 0x3d},
  1786. {110, 0x3c},
  1787. {104, 0x3c},
  1788. {98, 0x3c},
  1789. {110, 0x3b},
  1790. {104, 0x3b},
  1791. {98, 0x3b},
  1792. {110, 0x3a},
  1793. {104, 0x3a},
  1794. {98, 0x3a},
  1795. {110, 0x39},
  1796. {104, 0x39},
  1797. {98, 0x39},
  1798. {110, 0x38},
  1799. {104, 0x38},
  1800. {98, 0x38},
  1801. {110, 0x37},
  1802. {104, 0x37},
  1803. {98, 0x37},
  1804. {110, 0x36},
  1805. {104, 0x36},
  1806. {98, 0x36},
  1807. {110, 0x35},
  1808. {104, 0x35},
  1809. {98, 0x35},
  1810. {110, 0x34},
  1811. {104, 0x34},
  1812. {98, 0x34},
  1813. {110, 0x33},
  1814. {104, 0x33},
  1815. {98, 0x33},
  1816. {110, 0x32},
  1817. {104, 0x32},
  1818. {98, 0x32},
  1819. {110, 0x31},
  1820. {104, 0x31},
  1821. {98, 0x31},
  1822. {110, 0x30},
  1823. {104, 0x30},
  1824. {98, 0x30},
  1825. {110, 0x6},
  1826. {104, 0x6},
  1827. {98, 0x6},
  1828. {110, 0x5},
  1829. {104, 0x5},
  1830. {98, 0x5},
  1831. {110, 0x4},
  1832. {104, 0x4},
  1833. {98, 0x4},
  1834. {110, 0x3},
  1835. {104, 0x3},
  1836. {98, 0x3},
  1837. {110, 0x2},
  1838. {104, 0x2},
  1839. {98, 0x2},
  1840. {110, 0x1},
  1841. {104, 0x1},
  1842. {98, 0x1},
  1843. {110, 0x0},
  1844. {104, 0x0},
  1845. {98, 0x0},
  1846. {97, 0},
  1847. {96, 0},
  1848. {95, 0},
  1849. {94, 0},
  1850. {93, 0},
  1851. {92, 0},
  1852. {91, 0},
  1853. {90, 0},
  1854. {89, 0},
  1855. {88, 0},
  1856. {87, 0},
  1857. {86, 0},
  1858. {85, 0},
  1859. {84, 0},
  1860. {83, 0},
  1861. {82, 0},
  1862. {81, 0},
  1863. {80, 0},
  1864. {79, 0},
  1865. {78, 0},
  1866. {77, 0},
  1867. {76, 0},
  1868. {75, 0},
  1869. {74, 0},
  1870. {73, 0},
  1871. {72, 0},
  1872. {71, 0},
  1873. {70, 0},
  1874. {69, 0},
  1875. {68, 0},
  1876. {67, 0},
  1877. {66, 0},
  1878. {65, 0},
  1879. {64, 0},
  1880. {63, 0},
  1881. {62, 0},
  1882. {61, 0},
  1883. {60, 0},
  1884. {59, 0},
  1885. }
  1886. };
  1887. static int iwl4965_fill_txpower_tbl(struct iwl4965_priv *priv, u8 band, u16 channel,
  1888. u8 is_fat, u8 ctrl_chan_high,
  1889. struct iwl4965_tx_power_db *tx_power_tbl)
  1890. {
  1891. u8 saturation_power;
  1892. s32 target_power;
  1893. s32 user_target_power;
  1894. s32 power_limit;
  1895. s32 current_temp;
  1896. s32 reg_limit;
  1897. s32 current_regulatory;
  1898. s32 txatten_grp = CALIB_CH_GROUP_MAX;
  1899. int i;
  1900. int c;
  1901. const struct iwl4965_channel_info *ch_info = NULL;
  1902. struct iwl4965_eeprom_calib_ch_info ch_eeprom_info;
  1903. const struct iwl4965_eeprom_calib_measure *measurement;
  1904. s16 voltage;
  1905. s32 init_voltage;
  1906. s32 voltage_compensation;
  1907. s32 degrees_per_05db_num;
  1908. s32 degrees_per_05db_denom;
  1909. s32 factory_temp;
  1910. s32 temperature_comp[2];
  1911. s32 factory_gain_index[2];
  1912. s32 factory_actual_pwr[2];
  1913. s32 power_index;
  1914. /* Sanity check requested level (dBm) */
  1915. if (priv->user_txpower_limit < IWL_TX_POWER_TARGET_POWER_MIN) {
  1916. IWL_WARNING("Requested user TXPOWER %d below limit.\n",
  1917. priv->user_txpower_limit);
  1918. return -EINVAL;
  1919. }
  1920. if (priv->user_txpower_limit > IWL_TX_POWER_TARGET_POWER_MAX) {
  1921. IWL_WARNING("Requested user TXPOWER %d above limit.\n",
  1922. priv->user_txpower_limit);
  1923. return -EINVAL;
  1924. }
  1925. /* user_txpower_limit is in dBm, convert to half-dBm (half-dB units
  1926. * are used for indexing into txpower table) */
  1927. user_target_power = 2 * priv->user_txpower_limit;
  1928. /* Get current (RXON) channel, band, width */
  1929. ch_info =
  1930. iwl4965_get_channel_txpower_info(priv, priv->phymode, channel);
  1931. IWL_DEBUG_TXPOWER("chan %d band %d is_fat %d\n", channel, band,
  1932. is_fat);
  1933. if (!ch_info)
  1934. return -EINVAL;
  1935. /* get txatten group, used to select 1) thermal txpower adjustment
  1936. * and 2) mimo txpower balance between Tx chains. */
  1937. txatten_grp = iwl4965_get_tx_atten_grp(channel);
  1938. if (txatten_grp < 0)
  1939. return -EINVAL;
  1940. IWL_DEBUG_TXPOWER("channel %d belongs to txatten group %d\n",
  1941. channel, txatten_grp);
  1942. if (is_fat) {
  1943. if (ctrl_chan_high)
  1944. channel -= 2;
  1945. else
  1946. channel += 2;
  1947. }
  1948. /* hardware txpower limits ...
  1949. * saturation (clipping distortion) txpowers are in half-dBm */
  1950. if (band)
  1951. saturation_power = priv->eeprom.calib_info.saturation_power24;
  1952. else
  1953. saturation_power = priv->eeprom.calib_info.saturation_power52;
  1954. if (saturation_power < IWL_TX_POWER_SATURATION_MIN ||
  1955. saturation_power > IWL_TX_POWER_SATURATION_MAX) {
  1956. if (band)
  1957. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_24;
  1958. else
  1959. saturation_power = IWL_TX_POWER_DEFAULT_SATURATION_52;
  1960. }
  1961. /* regulatory txpower limits ... reg_limit values are in half-dBm,
  1962. * max_power_avg values are in dBm, convert * 2 */
  1963. if (is_fat)
  1964. reg_limit = ch_info->fat_max_power_avg * 2;
  1965. else
  1966. reg_limit = ch_info->max_power_avg * 2;
  1967. if ((reg_limit < IWL_TX_POWER_REGULATORY_MIN) ||
  1968. (reg_limit > IWL_TX_POWER_REGULATORY_MAX)) {
  1969. if (band)
  1970. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_24;
  1971. else
  1972. reg_limit = IWL_TX_POWER_DEFAULT_REGULATORY_52;
  1973. }
  1974. /* Interpolate txpower calibration values for this channel,
  1975. * based on factory calibration tests on spaced channels. */
  1976. iwl4965_interpolate_chan(priv, channel, &ch_eeprom_info);
  1977. /* calculate tx gain adjustment based on power supply voltage */
  1978. voltage = priv->eeprom.calib_info.voltage;
  1979. init_voltage = (s32)le32_to_cpu(priv->card_alive_init.voltage);
  1980. voltage_compensation =
  1981. iwl4965_get_voltage_compensation(voltage, init_voltage);
  1982. IWL_DEBUG_TXPOWER("curr volt %d eeprom volt %d volt comp %d\n",
  1983. init_voltage,
  1984. voltage, voltage_compensation);
  1985. /* get current temperature (Celsius) */
  1986. current_temp = max(priv->temperature, IWL_TX_POWER_TEMPERATURE_MIN);
  1987. current_temp = min(priv->temperature, IWL_TX_POWER_TEMPERATURE_MAX);
  1988. current_temp = KELVIN_TO_CELSIUS(current_temp);
  1989. /* select thermal txpower adjustment params, based on channel group
  1990. * (same frequency group used for mimo txatten adjustment) */
  1991. degrees_per_05db_num =
  1992. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a;
  1993. degrees_per_05db_denom =
  1994. tx_power_cmp_tble[txatten_grp].degrees_per_05db_a_denom;
  1995. /* get per-chain txpower values from factory measurements */
  1996. for (c = 0; c < 2; c++) {
  1997. measurement = &ch_eeprom_info.measurements[c][1];
  1998. /* txgain adjustment (in half-dB steps) based on difference
  1999. * between factory and current temperature */
  2000. factory_temp = measurement->temperature;
  2001. iwl4965_math_div_round((current_temp - factory_temp) *
  2002. degrees_per_05db_denom,
  2003. degrees_per_05db_num,
  2004. &temperature_comp[c]);
  2005. factory_gain_index[c] = measurement->gain_idx;
  2006. factory_actual_pwr[c] = measurement->actual_pow;
  2007. IWL_DEBUG_TXPOWER("chain = %d\n", c);
  2008. IWL_DEBUG_TXPOWER("fctry tmp %d, "
  2009. "curr tmp %d, comp %d steps\n",
  2010. factory_temp, current_temp,
  2011. temperature_comp[c]);
  2012. IWL_DEBUG_TXPOWER("fctry idx %d, fctry pwr %d\n",
  2013. factory_gain_index[c],
  2014. factory_actual_pwr[c]);
  2015. }
  2016. /* for each of 33 bit-rates (including 1 for CCK) */
  2017. for (i = 0; i < POWER_TABLE_NUM_ENTRIES; i++) {
  2018. u8 is_mimo_rate;
  2019. union iwl4965_tx_power_dual_stream tx_power;
  2020. /* for mimo, reduce each chain's txpower by half
  2021. * (3dB, 6 steps), so total output power is regulatory
  2022. * compliant. */
  2023. if (i & 0x8) {
  2024. current_regulatory = reg_limit -
  2025. IWL_TX_POWER_MIMO_REGULATORY_COMPENSATION;
  2026. is_mimo_rate = 1;
  2027. } else {
  2028. current_regulatory = reg_limit;
  2029. is_mimo_rate = 0;
  2030. }
  2031. /* find txpower limit, either hardware or regulatory */
  2032. power_limit = saturation_power - back_off_table[i];
  2033. if (power_limit > current_regulatory)
  2034. power_limit = current_regulatory;
  2035. /* reduce user's txpower request if necessary
  2036. * for this rate on this channel */
  2037. target_power = user_target_power;
  2038. if (target_power > power_limit)
  2039. target_power = power_limit;
  2040. IWL_DEBUG_TXPOWER("rate %d sat %d reg %d usr %d tgt %d\n",
  2041. i, saturation_power - back_off_table[i],
  2042. current_regulatory, user_target_power,
  2043. target_power);
  2044. /* for each of 2 Tx chains (radio transmitters) */
  2045. for (c = 0; c < 2; c++) {
  2046. s32 atten_value;
  2047. if (is_mimo_rate)
  2048. atten_value =
  2049. (s32)le32_to_cpu(priv->card_alive_init.
  2050. tx_atten[txatten_grp][c]);
  2051. else
  2052. atten_value = 0;
  2053. /* calculate index; higher index means lower txpower */
  2054. power_index = (u8) (factory_gain_index[c] -
  2055. (target_power -
  2056. factory_actual_pwr[c]) -
  2057. temperature_comp[c] -
  2058. voltage_compensation +
  2059. atten_value);
  2060. /* IWL_DEBUG_TXPOWER("calculated txpower index %d\n",
  2061. power_index); */
  2062. if (power_index < get_min_power_index(i, band))
  2063. power_index = get_min_power_index(i, band);
  2064. /* adjust 5 GHz index to support negative indexes */
  2065. if (!band)
  2066. power_index += 9;
  2067. /* CCK, rate 32, reduce txpower for CCK */
  2068. if (i == POWER_TABLE_CCK_ENTRY)
  2069. power_index +=
  2070. IWL_TX_POWER_CCK_COMPENSATION_C_STEP;
  2071. /* stay within the table! */
  2072. if (power_index > 107) {
  2073. IWL_WARNING("txpower index %d > 107\n",
  2074. power_index);
  2075. power_index = 107;
  2076. }
  2077. if (power_index < 0) {
  2078. IWL_WARNING("txpower index %d < 0\n",
  2079. power_index);
  2080. power_index = 0;
  2081. }
  2082. /* fill txpower command for this rate/chain */
  2083. tx_power.s.radio_tx_gain[c] =
  2084. gain_table[band][power_index].radio;
  2085. tx_power.s.dsp_predis_atten[c] =
  2086. gain_table[band][power_index].dsp;
  2087. IWL_DEBUG_TXPOWER("chain %d mimo %d index %d "
  2088. "gain 0x%02x dsp %d\n",
  2089. c, atten_value, power_index,
  2090. tx_power.s.radio_tx_gain[c],
  2091. tx_power.s.dsp_predis_atten[c]);
  2092. }/* for each chain */
  2093. tx_power_tbl->power_tbl[i].dw = cpu_to_le32(tx_power.dw);
  2094. }/* for each rate */
  2095. return 0;
  2096. }
  2097. /**
  2098. * iwl4965_hw_reg_send_txpower - Configure the TXPOWER level user limit
  2099. *
  2100. * Uses the active RXON for channel, band, and characteristics (fat, high)
  2101. * The power limit is taken from priv->user_txpower_limit.
  2102. */
  2103. int iwl4965_hw_reg_send_txpower(struct iwl4965_priv *priv)
  2104. {
  2105. struct iwl4965_txpowertable_cmd cmd = { 0 };
  2106. int rc = 0;
  2107. u8 band = 0;
  2108. u8 is_fat = 0;
  2109. u8 ctrl_chan_high = 0;
  2110. if (test_bit(STATUS_SCANNING, &priv->status)) {
  2111. /* If this gets hit a lot, switch it to a BUG() and catch
  2112. * the stack trace to find out who is calling this during
  2113. * a scan. */
  2114. IWL_WARNING("TX Power requested while scanning!\n");
  2115. return -EAGAIN;
  2116. }
  2117. band = ((priv->phymode == MODE_IEEE80211B) ||
  2118. (priv->phymode == MODE_IEEE80211G));
  2119. is_fat = is_fat_channel(priv->active_rxon.flags);
  2120. if (is_fat &&
  2121. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  2122. ctrl_chan_high = 1;
  2123. cmd.band = band;
  2124. cmd.channel = priv->active_rxon.channel;
  2125. rc = iwl4965_fill_txpower_tbl(priv, band,
  2126. le16_to_cpu(priv->active_rxon.channel),
  2127. is_fat, ctrl_chan_high, &cmd.tx_power);
  2128. if (rc)
  2129. return rc;
  2130. rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_PWR_TABLE_CMD, sizeof(cmd), &cmd);
  2131. return rc;
  2132. }
  2133. int iwl4965_hw_channel_switch(struct iwl4965_priv *priv, u16 channel)
  2134. {
  2135. int rc;
  2136. u8 band = 0;
  2137. u8 is_fat = 0;
  2138. u8 ctrl_chan_high = 0;
  2139. struct iwl4965_channel_switch_cmd cmd = { 0 };
  2140. const struct iwl4965_channel_info *ch_info;
  2141. band = ((priv->phymode == MODE_IEEE80211B) ||
  2142. (priv->phymode == MODE_IEEE80211G));
  2143. ch_info = iwl4965_get_channel_info(priv, priv->phymode, channel);
  2144. is_fat = is_fat_channel(priv->staging_rxon.flags);
  2145. if (is_fat &&
  2146. (priv->active_rxon.flags & RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK))
  2147. ctrl_chan_high = 1;
  2148. cmd.band = band;
  2149. cmd.expect_beacon = 0;
  2150. cmd.channel = cpu_to_le16(channel);
  2151. cmd.rxon_flags = priv->active_rxon.flags;
  2152. cmd.rxon_filter_flags = priv->active_rxon.filter_flags;
  2153. cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
  2154. if (ch_info)
  2155. cmd.expect_beacon = is_channel_radar(ch_info);
  2156. else
  2157. cmd.expect_beacon = 1;
  2158. rc = iwl4965_fill_txpower_tbl(priv, band, channel, is_fat,
  2159. ctrl_chan_high, &cmd.tx_power);
  2160. if (rc) {
  2161. IWL_DEBUG_11H("error:%d fill txpower_tbl\n", rc);
  2162. return rc;
  2163. }
  2164. rc = iwl4965_send_cmd_pdu(priv, REPLY_CHANNEL_SWITCH, sizeof(cmd), &cmd);
  2165. return rc;
  2166. }
  2167. #define RTS_HCCA_RETRY_LIMIT 3
  2168. #define RTS_DFAULT_RETRY_LIMIT 60
  2169. void iwl4965_hw_build_tx_cmd_rate(struct iwl4965_priv *priv,
  2170. struct iwl4965_cmd *cmd,
  2171. struct ieee80211_tx_control *ctrl,
  2172. struct ieee80211_hdr *hdr, int sta_id,
  2173. int is_hcca)
  2174. {
  2175. u8 rate;
  2176. u8 rts_retry_limit = 0;
  2177. u8 data_retry_limit = 0;
  2178. __le32 tx_flags;
  2179. u16 fc = le16_to_cpu(hdr->frame_control);
  2180. tx_flags = cmd->cmd.tx.tx_flags;
  2181. rate = iwl4965_rates[ctrl->tx_rate].plcp;
  2182. rts_retry_limit = (is_hcca) ?
  2183. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  2184. if (ieee80211_is_probe_response(fc)) {
  2185. data_retry_limit = 3;
  2186. if (data_retry_limit < rts_retry_limit)
  2187. rts_retry_limit = data_retry_limit;
  2188. } else
  2189. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  2190. if (priv->data_retry_limit != -1)
  2191. data_retry_limit = priv->data_retry_limit;
  2192. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  2193. switch (fc & IEEE80211_FCTL_STYPE) {
  2194. case IEEE80211_STYPE_AUTH:
  2195. case IEEE80211_STYPE_DEAUTH:
  2196. case IEEE80211_STYPE_ASSOC_REQ:
  2197. case IEEE80211_STYPE_REASSOC_REQ:
  2198. if (tx_flags & TX_CMD_FLG_RTS_MSK) {
  2199. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  2200. tx_flags |= TX_CMD_FLG_CTS_MSK;
  2201. }
  2202. break;
  2203. default:
  2204. break;
  2205. }
  2206. }
  2207. cmd->cmd.tx.rts_retry_limit = rts_retry_limit;
  2208. cmd->cmd.tx.data_retry_limit = data_retry_limit;
  2209. cmd->cmd.tx.rate_n_flags = iwl4965_hw_set_rate_n_flags(rate, 0);
  2210. cmd->cmd.tx.tx_flags = tx_flags;
  2211. }
  2212. int iwl4965_hw_get_rx_read(struct iwl4965_priv *priv)
  2213. {
  2214. struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
  2215. return IWL_GET_BITS(*shared_data, rb_closed_stts_rb_num);
  2216. }
  2217. int iwl4965_hw_get_temperature(struct iwl4965_priv *priv)
  2218. {
  2219. return priv->temperature;
  2220. }
  2221. unsigned int iwl4965_hw_get_beacon_cmd(struct iwl4965_priv *priv,
  2222. struct iwl4965_frame *frame, u8 rate)
  2223. {
  2224. struct iwl4965_tx_beacon_cmd *tx_beacon_cmd;
  2225. unsigned int frame_size;
  2226. tx_beacon_cmd = &frame->u.beacon;
  2227. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  2228. tx_beacon_cmd->tx.sta_id = IWL4965_BROADCAST_ID;
  2229. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2230. frame_size = iwl4965_fill_beacon_frame(priv,
  2231. tx_beacon_cmd->frame,
  2232. iwl4965_broadcast_addr,
  2233. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  2234. BUG_ON(frame_size > MAX_MPDU_SIZE);
  2235. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  2236. if ((rate == IWL_RATE_1M_PLCP) || (rate >= IWL_RATE_2M_PLCP))
  2237. tx_beacon_cmd->tx.rate_n_flags =
  2238. iwl4965_hw_set_rate_n_flags(rate, RATE_MCS_CCK_MSK);
  2239. else
  2240. tx_beacon_cmd->tx.rate_n_flags =
  2241. iwl4965_hw_set_rate_n_flags(rate, 0);
  2242. tx_beacon_cmd->tx.tx_flags = (TX_CMD_FLG_SEQ_CTL_MSK |
  2243. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK);
  2244. return (sizeof(*tx_beacon_cmd) + frame_size);
  2245. }
  2246. int iwl4965_hw_tx_queue_init(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
  2247. {
  2248. int rc;
  2249. unsigned long flags;
  2250. int txq_id = txq->q.id;
  2251. spin_lock_irqsave(&priv->lock, flags);
  2252. rc = iwl4965_grab_nic_access(priv);
  2253. if (rc) {
  2254. spin_unlock_irqrestore(&priv->lock, flags);
  2255. return rc;
  2256. }
  2257. iwl4965_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  2258. txq->q.dma_addr >> 8);
  2259. iwl4965_write_direct32(
  2260. priv, IWL_FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  2261. IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  2262. IWL_FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  2263. iwl4965_release_nic_access(priv);
  2264. spin_unlock_irqrestore(&priv->lock, flags);
  2265. return 0;
  2266. }
  2267. static inline u8 iwl4965_get_dma_hi_address(dma_addr_t addr)
  2268. {
  2269. return sizeof(addr) > sizeof(u32) ? (addr >> 16) >> 16 : 0;
  2270. }
  2271. int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl4965_priv *priv, void *ptr,
  2272. dma_addr_t addr, u16 len)
  2273. {
  2274. int index, is_odd;
  2275. struct iwl4965_tfd_frame *tfd = ptr;
  2276. u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
  2277. if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
  2278. IWL_ERROR("Error can not send more than %d chunks\n",
  2279. MAX_NUM_OF_TBS);
  2280. return -EINVAL;
  2281. }
  2282. index = num_tbs / 2;
  2283. is_odd = num_tbs & 0x1;
  2284. if (!is_odd) {
  2285. tfd->pa[index].tb1_addr = cpu_to_le32(addr);
  2286. IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
  2287. iwl4965_get_dma_hi_address(addr));
  2288. IWL_SET_BITS(tfd->pa[index], tb1_len, len);
  2289. } else {
  2290. IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
  2291. (u32) (addr & 0xffff));
  2292. IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
  2293. IWL_SET_BITS(tfd->pa[index], tb2_len, len);
  2294. }
  2295. IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
  2296. return 0;
  2297. }
  2298. static void iwl4965_hw_card_show_info(struct iwl4965_priv *priv)
  2299. {
  2300. u16 hw_version = priv->eeprom.board_revision_4965;
  2301. IWL_DEBUG_INFO("4965ABGN HW Version %u.%u.%u\n",
  2302. ((hw_version >> 8) & 0x0F),
  2303. ((hw_version >> 8) >> 4), (hw_version & 0x00FF));
  2304. IWL_DEBUG_INFO("4965ABGN PBA Number %.16s\n",
  2305. priv->eeprom.board_pba_number_4965);
  2306. }
  2307. #define IWL_TX_CRC_SIZE 4
  2308. #define IWL_TX_DELIMITER_SIZE 4
  2309. int iwl4965_tx_queue_update_wr_ptr(struct iwl4965_priv *priv,
  2310. struct iwl4965_tx_queue *txq, u16 byte_cnt)
  2311. {
  2312. int len;
  2313. int txq_id = txq->q.id;
  2314. struct iwl4965_shared *shared_data = priv->hw_setting.shared_virt;
  2315. if (txq->need_update == 0)
  2316. return 0;
  2317. len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  2318. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  2319. tfd_offset[txq->q.write_ptr], byte_cnt, len);
  2320. if (txq->q.write_ptr < IWL4965_MAX_WIN_SIZE)
  2321. IWL_SET_BITS16(shared_data->queues_byte_cnt_tbls[txq_id].
  2322. tfd_offset[IWL4965_QUEUE_SIZE + txq->q.write_ptr],
  2323. byte_cnt, len);
  2324. return 0;
  2325. }
  2326. /* Set up Rx receiver/antenna/chain usage in "staging" RXON image.
  2327. * This should not be used for scan command ... it puts data in wrong place. */
  2328. void iwl4965_set_rxon_chain(struct iwl4965_priv *priv)
  2329. {
  2330. u8 is_single = is_single_stream(priv);
  2331. u8 idle_state, rx_state;
  2332. priv->staging_rxon.rx_chain = 0;
  2333. rx_state = idle_state = 3;
  2334. /* Tell uCode which antennas are actually connected.
  2335. * Before first association, we assume all antennas are connected.
  2336. * Just after first association, iwl4965_noise_calibration()
  2337. * checks which antennas actually *are* connected. */
  2338. priv->staging_rxon.rx_chain |=
  2339. cpu_to_le16(priv->valid_antenna << RXON_RX_CHAIN_VALID_POS);
  2340. /* How many receivers should we use? */
  2341. iwl4965_get_rx_chain_counter(priv, &idle_state, &rx_state);
  2342. priv->staging_rxon.rx_chain |=
  2343. cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
  2344. priv->staging_rxon.rx_chain |=
  2345. cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
  2346. if (!is_single && (rx_state >= 2) &&
  2347. !test_bit(STATUS_POWER_PMI, &priv->status))
  2348. priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  2349. else
  2350. priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  2351. IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
  2352. }
  2353. #ifdef CONFIG_IWL4965_HT
  2354. #ifdef CONFIG_IWL4965_HT_AGG
  2355. /*
  2356. get the traffic load value for tid
  2357. */
  2358. static u32 iwl4965_tl_get_load(struct iwl4965_priv *priv, u8 tid)
  2359. {
  2360. u32 load = 0;
  2361. u32 current_time = jiffies_to_msecs(jiffies);
  2362. u32 time_diff;
  2363. s32 index;
  2364. unsigned long flags;
  2365. struct iwl4965_traffic_load *tid_ptr = NULL;
  2366. if (tid >= TID_MAX_LOAD_COUNT)
  2367. return 0;
  2368. tid_ptr = &(priv->lq_mngr.agg_ctrl.traffic_load[tid]);
  2369. current_time -= current_time % TID_ROUND_VALUE;
  2370. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2371. if (!(tid_ptr->queue_count))
  2372. goto out;
  2373. time_diff = TIME_WRAP_AROUND(tid_ptr->time_stamp, current_time);
  2374. index = time_diff / TID_QUEUE_CELL_SPACING;
  2375. if (index >= TID_QUEUE_MAX_SIZE) {
  2376. u32 oldest_time = current_time - TID_MAX_TIME_DIFF;
  2377. while (tid_ptr->queue_count &&
  2378. (tid_ptr->time_stamp < oldest_time)) {
  2379. tid_ptr->total -= tid_ptr->packet_count[tid_ptr->head];
  2380. tid_ptr->packet_count[tid_ptr->head] = 0;
  2381. tid_ptr->time_stamp += TID_QUEUE_CELL_SPACING;
  2382. tid_ptr->queue_count--;
  2383. tid_ptr->head++;
  2384. if (tid_ptr->head >= TID_QUEUE_MAX_SIZE)
  2385. tid_ptr->head = 0;
  2386. }
  2387. }
  2388. load = tid_ptr->total;
  2389. out:
  2390. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2391. return load;
  2392. }
  2393. /*
  2394. increment traffic load value for tid and also remove
  2395. any old values if passed the certian time period
  2396. */
  2397. static void iwl4965_tl_add_packet(struct iwl4965_priv *priv, u8 tid)
  2398. {
  2399. u32 current_time = jiffies_to_msecs(jiffies);
  2400. u32 time_diff;
  2401. s32 index;
  2402. unsigned long flags;
  2403. struct iwl4965_traffic_load *tid_ptr = NULL;
  2404. if (tid >= TID_MAX_LOAD_COUNT)
  2405. return;
  2406. tid_ptr = &(priv->lq_mngr.agg_ctrl.traffic_load[tid]);
  2407. current_time -= current_time % TID_ROUND_VALUE;
  2408. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2409. if (!(tid_ptr->queue_count)) {
  2410. tid_ptr->total = 1;
  2411. tid_ptr->time_stamp = current_time;
  2412. tid_ptr->queue_count = 1;
  2413. tid_ptr->head = 0;
  2414. tid_ptr->packet_count[0] = 1;
  2415. goto out;
  2416. }
  2417. time_diff = TIME_WRAP_AROUND(tid_ptr->time_stamp, current_time);
  2418. index = time_diff / TID_QUEUE_CELL_SPACING;
  2419. if (index >= TID_QUEUE_MAX_SIZE) {
  2420. u32 oldest_time = current_time - TID_MAX_TIME_DIFF;
  2421. while (tid_ptr->queue_count &&
  2422. (tid_ptr->time_stamp < oldest_time)) {
  2423. tid_ptr->total -= tid_ptr->packet_count[tid_ptr->head];
  2424. tid_ptr->packet_count[tid_ptr->head] = 0;
  2425. tid_ptr->time_stamp += TID_QUEUE_CELL_SPACING;
  2426. tid_ptr->queue_count--;
  2427. tid_ptr->head++;
  2428. if (tid_ptr->head >= TID_QUEUE_MAX_SIZE)
  2429. tid_ptr->head = 0;
  2430. }
  2431. }
  2432. index = (tid_ptr->head + index) % TID_QUEUE_MAX_SIZE;
  2433. tid_ptr->packet_count[index] = tid_ptr->packet_count[index] + 1;
  2434. tid_ptr->total = tid_ptr->total + 1;
  2435. if ((index + 1) > tid_ptr->queue_count)
  2436. tid_ptr->queue_count = index + 1;
  2437. out:
  2438. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2439. }
  2440. #define MMAC_SCHED_MAX_NUMBER_OF_HT_BACK_FLOWS 7
  2441. enum HT_STATUS {
  2442. BA_STATUS_FAILURE = 0,
  2443. BA_STATUS_INITIATOR_DELBA,
  2444. BA_STATUS_RECIPIENT_DELBA,
  2445. BA_STATUS_RENEW_ADDBA_REQUEST,
  2446. BA_STATUS_ACTIVE,
  2447. };
  2448. static u8 iwl4964_tl_ba_avail(struct iwl4965_priv *priv)
  2449. {
  2450. int i;
  2451. struct iwl4965_lq_mngr *lq;
  2452. u8 count = 0;
  2453. u16 msk;
  2454. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2455. for (i = 0; i < TID_MAX_LOAD_COUNT ; i++) {
  2456. msk = 1 << i;
  2457. if ((lq->agg_ctrl.granted_ba & msk) ||
  2458. (lq->agg_ctrl.wait_for_agg_status & msk))
  2459. count++;
  2460. }
  2461. if (count < MMAC_SCHED_MAX_NUMBER_OF_HT_BACK_FLOWS)
  2462. return 1;
  2463. return 0;
  2464. }
  2465. static void iwl4965_ba_status(struct iwl4965_priv *priv,
  2466. u8 tid, enum HT_STATUS status);
  2467. static int iwl4965_perform_addba(struct iwl4965_priv *priv, u8 tid, u32 length,
  2468. u32 ba_timeout)
  2469. {
  2470. int rc;
  2471. rc = ieee80211_start_BA_session(priv->hw, priv->bssid, tid);
  2472. if (rc)
  2473. iwl4965_ba_status(priv, tid, BA_STATUS_FAILURE);
  2474. return rc;
  2475. }
  2476. static int iwl4965_perform_delba(struct iwl4965_priv *priv, u8 tid)
  2477. {
  2478. int rc;
  2479. rc = ieee80211_stop_BA_session(priv->hw, priv->bssid, tid);
  2480. if (rc)
  2481. iwl4965_ba_status(priv, tid, BA_STATUS_FAILURE);
  2482. return rc;
  2483. }
  2484. static void iwl4965_turn_on_agg_for_tid(struct iwl4965_priv *priv,
  2485. struct iwl4965_lq_mngr *lq,
  2486. u8 auto_agg, u8 tid)
  2487. {
  2488. u32 tid_msk = (1 << tid);
  2489. unsigned long flags;
  2490. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2491. /*
  2492. if ((auto_agg) && (!lq->enable_counter)){
  2493. lq->agg_ctrl.next_retry = 0;
  2494. lq->agg_ctrl.tid_retry = 0;
  2495. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2496. return;
  2497. }
  2498. */
  2499. if (!(lq->agg_ctrl.granted_ba & tid_msk) &&
  2500. (lq->agg_ctrl.requested_ba & tid_msk)) {
  2501. u8 available_queues;
  2502. u32 load;
  2503. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2504. available_queues = iwl4964_tl_ba_avail(priv);
  2505. load = iwl4965_tl_get_load(priv, tid);
  2506. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2507. if (!available_queues) {
  2508. if (auto_agg)
  2509. lq->agg_ctrl.tid_retry |= tid_msk;
  2510. else {
  2511. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2512. lq->agg_ctrl.wait_for_agg_status &= ~tid_msk;
  2513. }
  2514. } else if ((auto_agg) &&
  2515. ((load <= lq->agg_ctrl.tid_traffic_load_threshold) ||
  2516. ((lq->agg_ctrl.wait_for_agg_status & tid_msk))))
  2517. lq->agg_ctrl.tid_retry |= tid_msk;
  2518. else {
  2519. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2520. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2521. iwl4965_perform_addba(priv, tid, 0x40,
  2522. lq->agg_ctrl.ba_timeout);
  2523. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2524. }
  2525. }
  2526. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2527. }
  2528. static void iwl4965_turn_on_agg(struct iwl4965_priv *priv, u8 tid)
  2529. {
  2530. struct iwl4965_lq_mngr *lq;
  2531. unsigned long flags;
  2532. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2533. if ((tid < TID_MAX_LOAD_COUNT))
  2534. iwl4965_turn_on_agg_for_tid(priv, lq, lq->agg_ctrl.auto_agg,
  2535. tid);
  2536. else if (tid == TID_ALL_SPECIFIED) {
  2537. if (lq->agg_ctrl.requested_ba) {
  2538. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
  2539. iwl4965_turn_on_agg_for_tid(priv, lq,
  2540. lq->agg_ctrl.auto_agg, tid);
  2541. } else {
  2542. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2543. lq->agg_ctrl.tid_retry = 0;
  2544. lq->agg_ctrl.next_retry = 0;
  2545. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2546. }
  2547. }
  2548. }
  2549. void iwl4965_turn_off_agg(struct iwl4965_priv *priv, u8 tid)
  2550. {
  2551. u32 tid_msk;
  2552. struct iwl4965_lq_mngr *lq;
  2553. unsigned long flags;
  2554. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2555. if ((tid < TID_MAX_LOAD_COUNT)) {
  2556. tid_msk = 1 << tid;
  2557. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2558. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2559. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2560. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2561. iwl4965_perform_delba(priv, tid);
  2562. } else if (tid == TID_ALL_SPECIFIED) {
  2563. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2564. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++) {
  2565. tid_msk = 1 << tid;
  2566. lq->agg_ctrl.wait_for_agg_status |= tid_msk;
  2567. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2568. iwl4965_perform_delba(priv, tid);
  2569. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2570. }
  2571. lq->agg_ctrl.requested_ba = 0;
  2572. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2573. }
  2574. }
  2575. static void iwl4965_ba_status(struct iwl4965_priv *priv,
  2576. u8 tid, enum HT_STATUS status)
  2577. {
  2578. struct iwl4965_lq_mngr *lq;
  2579. u32 tid_msk = (1 << tid);
  2580. unsigned long flags;
  2581. lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2582. if ((tid >= TID_MAX_LOAD_COUNT))
  2583. goto out;
  2584. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2585. switch (status) {
  2586. case BA_STATUS_ACTIVE:
  2587. if (!(lq->agg_ctrl.granted_ba & tid_msk))
  2588. lq->agg_ctrl.granted_ba |= tid_msk;
  2589. break;
  2590. default:
  2591. if ((lq->agg_ctrl.granted_ba & tid_msk))
  2592. lq->agg_ctrl.granted_ba &= ~tid_msk;
  2593. break;
  2594. }
  2595. lq->agg_ctrl.wait_for_agg_status &= ~tid_msk;
  2596. if (status != BA_STATUS_ACTIVE) {
  2597. if (lq->agg_ctrl.auto_agg) {
  2598. lq->agg_ctrl.tid_retry |= tid_msk;
  2599. lq->agg_ctrl.next_retry =
  2600. jiffies + msecs_to_jiffies(500);
  2601. } else
  2602. lq->agg_ctrl.requested_ba &= ~tid_msk;
  2603. }
  2604. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2605. out:
  2606. return;
  2607. }
  2608. static void iwl4965_bg_agg_work(struct work_struct *work)
  2609. {
  2610. struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv,
  2611. agg_work);
  2612. u32 tid;
  2613. u32 retry_tid;
  2614. u32 tid_msk;
  2615. unsigned long flags;
  2616. struct iwl4965_lq_mngr *lq = (struct iwl4965_lq_mngr *)&(priv->lq_mngr);
  2617. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2618. retry_tid = lq->agg_ctrl.tid_retry;
  2619. lq->agg_ctrl.tid_retry = 0;
  2620. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2621. if (retry_tid == TID_ALL_SPECIFIED)
  2622. iwl4965_turn_on_agg(priv, TID_ALL_SPECIFIED);
  2623. else {
  2624. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++) {
  2625. tid_msk = (1 << tid);
  2626. if (retry_tid & tid_msk)
  2627. iwl4965_turn_on_agg(priv, tid);
  2628. }
  2629. }
  2630. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2631. if (lq->agg_ctrl.tid_retry)
  2632. lq->agg_ctrl.next_retry = jiffies + msecs_to_jiffies(500);
  2633. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2634. return;
  2635. }
  2636. #endif /*CONFIG_IWL4965_HT_AGG */
  2637. #endif /* CONFIG_IWL4965_HT */
  2638. int iwl4965_tx_cmd(struct iwl4965_priv *priv, struct iwl4965_cmd *out_cmd,
  2639. u8 sta_id, dma_addr_t txcmd_phys,
  2640. struct ieee80211_hdr *hdr, u8 hdr_len,
  2641. struct ieee80211_tx_control *ctrl, void *sta_in)
  2642. {
  2643. struct iwl4965_tx_cmd cmd;
  2644. struct iwl4965_tx_cmd *tx = (struct iwl4965_tx_cmd *)&out_cmd->cmd.payload[0];
  2645. dma_addr_t scratch_phys;
  2646. u8 unicast = 0;
  2647. u8 is_data = 1;
  2648. u16 fc;
  2649. u16 rate_flags;
  2650. int rate_index = min(ctrl->tx_rate & 0xffff, IWL_RATE_COUNT - 1);
  2651. #ifdef CONFIG_IWL4965_HT
  2652. #ifdef CONFIG_IWL4965_HT_AGG
  2653. __le16 *qc;
  2654. #endif /*CONFIG_IWL4965_HT_AGG */
  2655. #endif /* CONFIG_IWL4965_HT */
  2656. unicast = !is_multicast_ether_addr(hdr->addr1);
  2657. fc = le16_to_cpu(hdr->frame_control);
  2658. if ((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA)
  2659. is_data = 0;
  2660. memcpy(&cmd, &(out_cmd->cmd.tx), sizeof(struct iwl4965_tx_cmd));
  2661. memset(tx, 0, sizeof(struct iwl4965_tx_cmd));
  2662. memcpy(tx->hdr, hdr, hdr_len);
  2663. tx->len = cmd.len;
  2664. tx->driver_txop = cmd.driver_txop;
  2665. tx->stop_time.life_time = cmd.stop_time.life_time;
  2666. tx->tx_flags = cmd.tx_flags;
  2667. tx->sta_id = cmd.sta_id;
  2668. tx->tid_tspec = cmd.tid_tspec;
  2669. tx->timeout.pm_frame_timeout = cmd.timeout.pm_frame_timeout;
  2670. tx->next_frame_len = cmd.next_frame_len;
  2671. tx->sec_ctl = cmd.sec_ctl;
  2672. memcpy(&(tx->key[0]), &(cmd.key[0]), 16);
  2673. tx->tx_flags = cmd.tx_flags;
  2674. tx->rts_retry_limit = cmd.rts_retry_limit;
  2675. tx->data_retry_limit = cmd.data_retry_limit;
  2676. scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
  2677. offsetof(struct iwl4965_tx_cmd, scratch);
  2678. tx->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  2679. tx->dram_msb_ptr = iwl4965_get_dma_hi_address(scratch_phys);
  2680. /* Hard coded to start at the highest retry fallback position
  2681. * until the 4965 specific rate control algorithm is tied in */
  2682. tx->initial_rate_index = LINK_QUAL_MAX_RETRY_NUM - 1;
  2683. /* Alternate between antenna A and B for successive frames */
  2684. if (priv->use_ant_b_for_management_frame) {
  2685. priv->use_ant_b_for_management_frame = 0;
  2686. rate_flags = RATE_MCS_ANT_B_MSK;
  2687. } else {
  2688. priv->use_ant_b_for_management_frame = 1;
  2689. rate_flags = RATE_MCS_ANT_A_MSK;
  2690. }
  2691. if (!unicast || !is_data) {
  2692. if ((rate_index >= IWL_FIRST_CCK_RATE) &&
  2693. (rate_index <= IWL_LAST_CCK_RATE))
  2694. rate_flags |= RATE_MCS_CCK_MSK;
  2695. } else {
  2696. tx->initial_rate_index = 0;
  2697. tx->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  2698. }
  2699. tx->rate_n_flags = iwl4965_hw_set_rate_n_flags(iwl4965_rates[rate_index].plcp,
  2700. rate_flags);
  2701. if (ieee80211_is_back_request(fc))
  2702. tx->tx_flags |= TX_CMD_FLG_ACK_MSK |
  2703. TX_CMD_FLG_IMM_BA_RSP_MASK;
  2704. #ifdef CONFIG_IWL4965_HT
  2705. #ifdef CONFIG_IWL4965_HT_AGG
  2706. qc = ieee80211_get_qos_ctrl(hdr);
  2707. if (qc &&
  2708. (priv->iw_mode != IEEE80211_IF_TYPE_IBSS)) {
  2709. u8 tid = 0;
  2710. tid = (u8) (le16_to_cpu(*qc) & 0xF);
  2711. if (tid < TID_MAX_LOAD_COUNT)
  2712. iwl4965_tl_add_packet(priv, tid);
  2713. }
  2714. if (priv->lq_mngr.agg_ctrl.next_retry &&
  2715. (time_after(priv->lq_mngr.agg_ctrl.next_retry, jiffies))) {
  2716. unsigned long flags;
  2717. spin_lock_irqsave(&priv->lq_mngr.lock, flags);
  2718. priv->lq_mngr.agg_ctrl.next_retry = 0;
  2719. spin_unlock_irqrestore(&priv->lq_mngr.lock, flags);
  2720. schedule_work(&priv->agg_work);
  2721. }
  2722. #endif
  2723. #endif
  2724. return 0;
  2725. }
  2726. /**
  2727. * sign_extend - Sign extend a value using specified bit as sign-bit
  2728. *
  2729. * Example: sign_extend(9, 3) would return -7 as bit3 of 1001b is 1
  2730. * and bit0..2 is 001b which when sign extended to 1111111111111001b is -7.
  2731. *
  2732. * @param oper value to sign extend
  2733. * @param index 0 based bit index (0<=index<32) to sign bit
  2734. */
  2735. static s32 sign_extend(u32 oper, int index)
  2736. {
  2737. u8 shift = 31 - index;
  2738. return (s32)(oper << shift) >> shift;
  2739. }
  2740. /**
  2741. * iwl4965_get_temperature - return the calibrated temperature (in Kelvin)
  2742. * @statistics: Provides the temperature reading from the uCode
  2743. *
  2744. * A return of <0 indicates bogus data in the statistics
  2745. */
  2746. int iwl4965_get_temperature(const struct iwl4965_priv *priv)
  2747. {
  2748. s32 temperature;
  2749. s32 vt;
  2750. s32 R1, R2, R3;
  2751. u32 R4;
  2752. if (test_bit(STATUS_TEMPERATURE, &priv->status) &&
  2753. (priv->statistics.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)) {
  2754. IWL_DEBUG_TEMP("Running FAT temperature calibration\n");
  2755. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[1]);
  2756. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[1]);
  2757. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[1]);
  2758. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[1]);
  2759. } else {
  2760. IWL_DEBUG_TEMP("Running temperature calibration\n");
  2761. R1 = (s32)le32_to_cpu(priv->card_alive_init.therm_r1[0]);
  2762. R2 = (s32)le32_to_cpu(priv->card_alive_init.therm_r2[0]);
  2763. R3 = (s32)le32_to_cpu(priv->card_alive_init.therm_r3[0]);
  2764. R4 = le32_to_cpu(priv->card_alive_init.therm_r4[0]);
  2765. }
  2766. /*
  2767. * Temperature is only 23 bits so sign extend out to 32
  2768. *
  2769. * NOTE If we haven't received a statistics notification yet
  2770. * with an updated temperature, use R4 provided to us in the
  2771. * ALIVE response. */
  2772. if (!test_bit(STATUS_TEMPERATURE, &priv->status))
  2773. vt = sign_extend(R4, 23);
  2774. else
  2775. vt = sign_extend(
  2776. le32_to_cpu(priv->statistics.general.temperature), 23);
  2777. IWL_DEBUG_TEMP("Calib values R[1-3]: %d %d %d R4: %d\n",
  2778. R1, R2, R3, vt);
  2779. if (R3 == R1) {
  2780. IWL_ERROR("Calibration conflict R1 == R3\n");
  2781. return -1;
  2782. }
  2783. /* Calculate temperature in degrees Kelvin, adjust by 97%.
  2784. * Add offset to center the adjustment around 0 degrees Centigrade. */
  2785. temperature = TEMPERATURE_CALIB_A_VAL * (vt - R2);
  2786. temperature /= (R3 - R1);
  2787. temperature = (temperature * 97) / 100 +
  2788. TEMPERATURE_CALIB_KELVIN_OFFSET;
  2789. IWL_DEBUG_TEMP("Calibrated temperature: %dK, %dC\n", temperature,
  2790. KELVIN_TO_CELSIUS(temperature));
  2791. return temperature;
  2792. }
  2793. /* Adjust Txpower only if temperature variance is greater than threshold. */
  2794. #define IWL_TEMPERATURE_THRESHOLD 3
  2795. /**
  2796. * iwl4965_is_temp_calib_needed - determines if new calibration is needed
  2797. *
  2798. * If the temperature changed has changed sufficiently, then a recalibration
  2799. * is needed.
  2800. *
  2801. * Assumes caller will replace priv->last_temperature once calibration
  2802. * executed.
  2803. */
  2804. static int iwl4965_is_temp_calib_needed(struct iwl4965_priv *priv)
  2805. {
  2806. int temp_diff;
  2807. if (!test_bit(STATUS_STATISTICS, &priv->status)) {
  2808. IWL_DEBUG_TEMP("Temperature not updated -- no statistics.\n");
  2809. return 0;
  2810. }
  2811. temp_diff = priv->temperature - priv->last_temperature;
  2812. /* get absolute value */
  2813. if (temp_diff < 0) {
  2814. IWL_DEBUG_POWER("Getting cooler, delta %d, \n", temp_diff);
  2815. temp_diff = -temp_diff;
  2816. } else if (temp_diff == 0)
  2817. IWL_DEBUG_POWER("Same temp, \n");
  2818. else
  2819. IWL_DEBUG_POWER("Getting warmer, delta %d, \n", temp_diff);
  2820. if (temp_diff < IWL_TEMPERATURE_THRESHOLD) {
  2821. IWL_DEBUG_POWER("Thermal txpower calib not needed\n");
  2822. return 0;
  2823. }
  2824. IWL_DEBUG_POWER("Thermal txpower calib needed\n");
  2825. return 1;
  2826. }
  2827. /* Calculate noise level, based on measurements during network silence just
  2828. * before arriving beacon. This measurement can be done only if we know
  2829. * exactly when to expect beacons, therefore only when we're associated. */
  2830. static void iwl4965_rx_calc_noise(struct iwl4965_priv *priv)
  2831. {
  2832. struct statistics_rx_non_phy *rx_info
  2833. = &(priv->statistics.rx.general);
  2834. int num_active_rx = 0;
  2835. int total_silence = 0;
  2836. int bcn_silence_a =
  2837. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  2838. int bcn_silence_b =
  2839. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  2840. int bcn_silence_c =
  2841. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  2842. if (bcn_silence_a) {
  2843. total_silence += bcn_silence_a;
  2844. num_active_rx++;
  2845. }
  2846. if (bcn_silence_b) {
  2847. total_silence += bcn_silence_b;
  2848. num_active_rx++;
  2849. }
  2850. if (bcn_silence_c) {
  2851. total_silence += bcn_silence_c;
  2852. num_active_rx++;
  2853. }
  2854. /* Average among active antennas */
  2855. if (num_active_rx)
  2856. priv->last_rx_noise = (total_silence / num_active_rx) - 107;
  2857. else
  2858. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  2859. IWL_DEBUG_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  2860. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  2861. priv->last_rx_noise);
  2862. }
  2863. void iwl4965_hw_rx_statistics(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
  2864. {
  2865. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  2866. int change;
  2867. s32 temp;
  2868. IWL_DEBUG_RX("Statistics notification received (%d vs %d).\n",
  2869. (int)sizeof(priv->statistics), pkt->len);
  2870. change = ((priv->statistics.general.temperature !=
  2871. pkt->u.stats.general.temperature) ||
  2872. ((priv->statistics.flag &
  2873. STATISTICS_REPLY_FLG_FAT_MODE_MSK) !=
  2874. (pkt->u.stats.flag & STATISTICS_REPLY_FLG_FAT_MODE_MSK)));
  2875. memcpy(&priv->statistics, &pkt->u.stats, sizeof(priv->statistics));
  2876. set_bit(STATUS_STATISTICS, &priv->status);
  2877. /* Reschedule the statistics timer to occur in
  2878. * REG_RECALIB_PERIOD seconds to ensure we get a
  2879. * thermal update even if the uCode doesn't give
  2880. * us one */
  2881. mod_timer(&priv->statistics_periodic, jiffies +
  2882. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  2883. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  2884. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  2885. iwl4965_rx_calc_noise(priv);
  2886. #ifdef CONFIG_IWL4965_SENSITIVITY
  2887. queue_work(priv->workqueue, &priv->sensitivity_work);
  2888. #endif
  2889. }
  2890. /* If the hardware hasn't reported a change in
  2891. * temperature then don't bother computing a
  2892. * calibrated temperature value */
  2893. if (!change)
  2894. return;
  2895. temp = iwl4965_get_temperature(priv);
  2896. if (temp < 0)
  2897. return;
  2898. if (priv->temperature != temp) {
  2899. if (priv->temperature)
  2900. IWL_DEBUG_TEMP("Temperature changed "
  2901. "from %dC to %dC\n",
  2902. KELVIN_TO_CELSIUS(priv->temperature),
  2903. KELVIN_TO_CELSIUS(temp));
  2904. else
  2905. IWL_DEBUG_TEMP("Temperature "
  2906. "initialized to %dC\n",
  2907. KELVIN_TO_CELSIUS(temp));
  2908. }
  2909. priv->temperature = temp;
  2910. set_bit(STATUS_TEMPERATURE, &priv->status);
  2911. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  2912. iwl4965_is_temp_calib_needed(priv))
  2913. queue_work(priv->workqueue, &priv->txpower_work);
  2914. }
  2915. static void iwl4965_handle_data_packet(struct iwl4965_priv *priv, int is_data,
  2916. int include_phy,
  2917. struct iwl4965_rx_mem_buffer *rxb,
  2918. struct ieee80211_rx_status *stats)
  2919. {
  2920. struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
  2921. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  2922. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) : NULL;
  2923. struct ieee80211_hdr *hdr;
  2924. u16 len;
  2925. __le32 *rx_end;
  2926. unsigned int skblen;
  2927. u32 ampdu_status;
  2928. if (!include_phy && priv->last_phy_res[0])
  2929. rx_start = (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  2930. if (!rx_start) {
  2931. IWL_ERROR("MPDU frame without a PHY data\n");
  2932. return;
  2933. }
  2934. if (include_phy) {
  2935. hdr = (struct ieee80211_hdr *)((u8 *) & rx_start[1] +
  2936. rx_start->cfg_phy_cnt);
  2937. len = le16_to_cpu(rx_start->byte_count);
  2938. rx_end = (__le32 *) ((u8 *) & pkt->u.raw[0] +
  2939. sizeof(struct iwl4965_rx_phy_res) +
  2940. rx_start->cfg_phy_cnt + len);
  2941. } else {
  2942. struct iwl4965_rx_mpdu_res_start *amsdu =
  2943. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  2944. hdr = (struct ieee80211_hdr *)(pkt->u.raw +
  2945. sizeof(struct iwl4965_rx_mpdu_res_start));
  2946. len = le16_to_cpu(amsdu->byte_count);
  2947. rx_start->byte_count = amsdu->byte_count;
  2948. rx_end = (__le32 *) (((u8 *) hdr) + len);
  2949. }
  2950. if (len > IWL_RX_BUF_SIZE || len < 16) {
  2951. IWL_WARNING("byte count out of range [16,4K]"
  2952. " : %d\n", len);
  2953. return;
  2954. }
  2955. ampdu_status = le32_to_cpu(*rx_end);
  2956. skblen = ((u8 *) rx_end - (u8 *) & pkt->u.raw[0]) + sizeof(u32);
  2957. /* start from MAC */
  2958. skb_reserve(rxb->skb, (void *)hdr - (void *)pkt);
  2959. skb_put(rxb->skb, len); /* end where data ends */
  2960. /* We only process data packets if the interface is open */
  2961. if (unlikely(!priv->is_open)) {
  2962. IWL_DEBUG_DROP_LIMIT
  2963. ("Dropping packet while interface is not open.\n");
  2964. return;
  2965. }
  2966. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  2967. if (iwl4965_param_hwcrypto)
  2968. iwl4965_set_decrypted_flag(priv, rxb->skb,
  2969. ampdu_status, stats);
  2970. iwl4965_handle_data_packet_monitor(priv, rxb, hdr, len, stats, 0);
  2971. return;
  2972. }
  2973. stats->flag = 0;
  2974. hdr = (struct ieee80211_hdr *)rxb->skb->data;
  2975. if (iwl4965_param_hwcrypto)
  2976. iwl4965_set_decrypted_flag(priv, rxb->skb, ampdu_status, stats);
  2977. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  2978. priv->alloc_rxb_skb--;
  2979. rxb->skb = NULL;
  2980. #ifdef LED
  2981. priv->led_packets += len;
  2982. iwl4965_setup_activity_timer(priv);
  2983. #endif
  2984. }
  2985. /* Calc max signal level (dBm) among 3 possible receivers */
  2986. static int iwl4965_calc_rssi(struct iwl4965_rx_phy_res *rx_resp)
  2987. {
  2988. /* data from PHY/DSP regarding signal strength, etc.,
  2989. * contents are always there, not configurable by host. */
  2990. struct iwl4965_rx_non_cfg_phy *ncphy =
  2991. (struct iwl4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy;
  2992. u32 agc = (le16_to_cpu(ncphy->agc_info) & IWL_AGC_DB_MASK)
  2993. >> IWL_AGC_DB_POS;
  2994. u32 valid_antennae =
  2995. (le16_to_cpu(rx_resp->phy_flags) & RX_PHY_FLAGS_ANTENNAE_MASK)
  2996. >> RX_PHY_FLAGS_ANTENNAE_OFFSET;
  2997. u8 max_rssi = 0;
  2998. u32 i;
  2999. /* Find max rssi among 3 possible receivers.
  3000. * These values are measured by the digital signal processor (DSP).
  3001. * They should stay fairly constant even as the signal strength varies,
  3002. * if the radio's automatic gain control (AGC) is working right.
  3003. * AGC value (see below) will provide the "interesting" info. */
  3004. for (i = 0; i < 3; i++)
  3005. if (valid_antennae & (1 << i))
  3006. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  3007. IWL_DEBUG_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  3008. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  3009. max_rssi, agc);
  3010. /* dBm = max_rssi dB - agc dB - constant.
  3011. * Higher AGC (higher radio gain) means lower signal. */
  3012. return (max_rssi - agc - IWL_RSSI_OFFSET);
  3013. }
  3014. #ifdef CONFIG_IWL4965_HT
  3015. /* Parsed Information Elements */
  3016. struct ieee802_11_elems {
  3017. u8 *ds_params;
  3018. u8 ds_params_len;
  3019. u8 *tim;
  3020. u8 tim_len;
  3021. u8 *ibss_params;
  3022. u8 ibss_params_len;
  3023. u8 *erp_info;
  3024. u8 erp_info_len;
  3025. u8 *ht_cap_param;
  3026. u8 ht_cap_param_len;
  3027. u8 *ht_extra_param;
  3028. u8 ht_extra_param_len;
  3029. };
  3030. static int parse_elems(u8 *start, size_t len, struct ieee802_11_elems *elems)
  3031. {
  3032. size_t left = len;
  3033. u8 *pos = start;
  3034. int unknown = 0;
  3035. memset(elems, 0, sizeof(*elems));
  3036. while (left >= 2) {
  3037. u8 id, elen;
  3038. id = *pos++;
  3039. elen = *pos++;
  3040. left -= 2;
  3041. if (elen > left)
  3042. return -1;
  3043. switch (id) {
  3044. case WLAN_EID_DS_PARAMS:
  3045. elems->ds_params = pos;
  3046. elems->ds_params_len = elen;
  3047. break;
  3048. case WLAN_EID_TIM:
  3049. elems->tim = pos;
  3050. elems->tim_len = elen;
  3051. break;
  3052. case WLAN_EID_IBSS_PARAMS:
  3053. elems->ibss_params = pos;
  3054. elems->ibss_params_len = elen;
  3055. break;
  3056. case WLAN_EID_ERP_INFO:
  3057. elems->erp_info = pos;
  3058. elems->erp_info_len = elen;
  3059. break;
  3060. case WLAN_EID_HT_CAPABILITY:
  3061. elems->ht_cap_param = pos;
  3062. elems->ht_cap_param_len = elen;
  3063. break;
  3064. case WLAN_EID_HT_EXTRA_INFO:
  3065. elems->ht_extra_param = pos;
  3066. elems->ht_extra_param_len = elen;
  3067. break;
  3068. default:
  3069. unknown++;
  3070. break;
  3071. }
  3072. left -= elen;
  3073. pos += elen;
  3074. }
  3075. return 0;
  3076. }
  3077. #endif /* CONFIG_IWL4965_HT */
  3078. static void iwl4965_sta_modify_ps_wake(struct iwl4965_priv *priv, int sta_id)
  3079. {
  3080. unsigned long flags;
  3081. spin_lock_irqsave(&priv->sta_lock, flags);
  3082. priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
  3083. priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  3084. priv->stations[sta_id].sta.sta.modify_mask = 0;
  3085. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3086. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3087. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3088. }
  3089. static void iwl4965_update_ps_mode(struct iwl4965_priv *priv, u16 ps_bit, u8 *addr)
  3090. {
  3091. /* FIXME: need locking over ps_status ??? */
  3092. u8 sta_id = iwl4965_hw_find_station(priv, addr);
  3093. if (sta_id != IWL_INVALID_STATION) {
  3094. u8 sta_awake = priv->stations[sta_id].
  3095. ps_status == STA_PS_STATUS_WAKE;
  3096. if (sta_awake && ps_bit)
  3097. priv->stations[sta_id].ps_status = STA_PS_STATUS_SLEEP;
  3098. else if (!sta_awake && !ps_bit) {
  3099. iwl4965_sta_modify_ps_wake(priv, sta_id);
  3100. priv->stations[sta_id].ps_status = STA_PS_STATUS_WAKE;
  3101. }
  3102. }
  3103. }
  3104. /* Called for REPLY_4965_RX (legacy ABG frames), or
  3105. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  3106. static void iwl4965_rx_reply_rx(struct iwl4965_priv *priv,
  3107. struct iwl4965_rx_mem_buffer *rxb)
  3108. {
  3109. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3110. /* Use phy data (Rx signal strength, etc.) contained within
  3111. * this rx packet for legacy frames,
  3112. * or phy data cached from REPLY_RX_PHY_CMD for HT frames. */
  3113. int include_phy = (pkt->hdr.cmd == REPLY_4965_RX);
  3114. struct iwl4965_rx_phy_res *rx_start = (include_phy) ?
  3115. (struct iwl4965_rx_phy_res *)&(pkt->u.raw[0]) :
  3116. (struct iwl4965_rx_phy_res *)&priv->last_phy_res[1];
  3117. __le32 *rx_end;
  3118. unsigned int len = 0;
  3119. struct ieee80211_hdr *header;
  3120. u16 fc;
  3121. struct ieee80211_rx_status stats = {
  3122. .mactime = le64_to_cpu(rx_start->timestamp),
  3123. .channel = le16_to_cpu(rx_start->channel),
  3124. .phymode =
  3125. (rx_start->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  3126. MODE_IEEE80211G : MODE_IEEE80211A,
  3127. .antenna = 0,
  3128. .rate = iwl4965_hw_get_rate(rx_start->rate_n_flags),
  3129. .flag = 0,
  3130. #ifdef CONFIG_IWL4965_HT_AGG
  3131. .ordered = 0
  3132. #endif /* CONFIG_IWL4965_HT_AGG */
  3133. };
  3134. u8 network_packet;
  3135. if ((unlikely(rx_start->cfg_phy_cnt > 20))) {
  3136. IWL_DEBUG_DROP
  3137. ("dsp size out of range [0,20]: "
  3138. "%d/n", rx_start->cfg_phy_cnt);
  3139. return;
  3140. }
  3141. if (!include_phy) {
  3142. if (priv->last_phy_res[0])
  3143. rx_start = (struct iwl4965_rx_phy_res *)
  3144. &priv->last_phy_res[1];
  3145. else
  3146. rx_start = NULL;
  3147. }
  3148. if (!rx_start) {
  3149. IWL_ERROR("MPDU frame without a PHY data\n");
  3150. return;
  3151. }
  3152. if (include_phy) {
  3153. header = (struct ieee80211_hdr *)((u8 *) & rx_start[1]
  3154. + rx_start->cfg_phy_cnt);
  3155. len = le16_to_cpu(rx_start->byte_count);
  3156. rx_end = (__le32 *) (pkt->u.raw + rx_start->cfg_phy_cnt +
  3157. sizeof(struct iwl4965_rx_phy_res) + len);
  3158. } else {
  3159. struct iwl4965_rx_mpdu_res_start *amsdu =
  3160. (struct iwl4965_rx_mpdu_res_start *)pkt->u.raw;
  3161. header = (void *)(pkt->u.raw +
  3162. sizeof(struct iwl4965_rx_mpdu_res_start));
  3163. len = le16_to_cpu(amsdu->byte_count);
  3164. rx_end = (__le32 *) (pkt->u.raw +
  3165. sizeof(struct iwl4965_rx_mpdu_res_start) + len);
  3166. }
  3167. if (!(*rx_end & RX_RES_STATUS_NO_CRC32_ERROR) ||
  3168. !(*rx_end & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  3169. IWL_DEBUG_RX("Bad CRC or FIFO: 0x%08X.\n",
  3170. le32_to_cpu(*rx_end));
  3171. return;
  3172. }
  3173. priv->ucode_beacon_time = le32_to_cpu(rx_start->beacon_time_stamp);
  3174. stats.freq = ieee80211chan2mhz(stats.channel);
  3175. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  3176. stats.ssi = iwl4965_calc_rssi(rx_start);
  3177. /* Meaningful noise values are available only from beacon statistics,
  3178. * which are gathered only when associated, and indicate noise
  3179. * only for the associated network channel ...
  3180. * Ignore these noise values while scanning (other channels) */
  3181. if (iwl4965_is_associated(priv) &&
  3182. !test_bit(STATUS_SCANNING, &priv->status)) {
  3183. stats.noise = priv->last_rx_noise;
  3184. stats.signal = iwl4965_calc_sig_qual(stats.ssi, stats.noise);
  3185. } else {
  3186. stats.noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  3187. stats.signal = iwl4965_calc_sig_qual(stats.ssi, 0);
  3188. }
  3189. /* Reset beacon noise level if not associated. */
  3190. if (!iwl4965_is_associated(priv))
  3191. priv->last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  3192. #ifdef CONFIG_IWL4965_DEBUG
  3193. /* TODO: Parts of iwl4965_report_frame are broken for 4965 */
  3194. if (iwl4965_debug_level & (IWL_DL_RX))
  3195. /* Set "1" to report good data frames in groups of 100 */
  3196. iwl4965_report_frame(priv, pkt, header, 1);
  3197. if (iwl4965_debug_level & (IWL_DL_RX | IWL_DL_STATS))
  3198. IWL_DEBUG_RX("Rssi %d, noise %d, qual %d, TSF %lu\n",
  3199. stats.ssi, stats.noise, stats.signal,
  3200. (long unsigned int)le64_to_cpu(rx_start->timestamp));
  3201. #endif
  3202. network_packet = iwl4965_is_network_packet(priv, header);
  3203. if (network_packet) {
  3204. priv->last_rx_rssi = stats.ssi;
  3205. priv->last_beacon_time = priv->ucode_beacon_time;
  3206. priv->last_tsf = le64_to_cpu(rx_start->timestamp);
  3207. }
  3208. fc = le16_to_cpu(header->frame_control);
  3209. switch (fc & IEEE80211_FCTL_FTYPE) {
  3210. case IEEE80211_FTYPE_MGMT:
  3211. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  3212. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  3213. header->addr2);
  3214. switch (fc & IEEE80211_FCTL_STYPE) {
  3215. case IEEE80211_STYPE_PROBE_RESP:
  3216. case IEEE80211_STYPE_BEACON:
  3217. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA &&
  3218. !compare_ether_addr(header->addr2, priv->bssid)) ||
  3219. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
  3220. !compare_ether_addr(header->addr3, priv->bssid))) {
  3221. struct ieee80211_mgmt *mgmt =
  3222. (struct ieee80211_mgmt *)header;
  3223. u64 timestamp =
  3224. le64_to_cpu(mgmt->u.beacon.timestamp);
  3225. priv->timestamp0 = timestamp & 0xFFFFFFFF;
  3226. priv->timestamp1 =
  3227. (timestamp >> 32) & 0xFFFFFFFF;
  3228. priv->beacon_int = le16_to_cpu(
  3229. mgmt->u.beacon.beacon_int);
  3230. if (priv->call_post_assoc_from_beacon &&
  3231. (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
  3232. priv->call_post_assoc_from_beacon = 0;
  3233. queue_work(priv->workqueue,
  3234. &priv->post_associate.work);
  3235. }
  3236. }
  3237. break;
  3238. case IEEE80211_STYPE_ACTION:
  3239. break;
  3240. /*
  3241. * TODO: There is no callback function from upper
  3242. * stack to inform us when associated status. this
  3243. * work around to sniff assoc_resp management frame
  3244. * and finish the association process.
  3245. */
  3246. case IEEE80211_STYPE_ASSOC_RESP:
  3247. case IEEE80211_STYPE_REASSOC_RESP:
  3248. if (network_packet) {
  3249. #ifdef CONFIG_IWL4965_HT
  3250. u8 *pos = NULL;
  3251. struct ieee802_11_elems elems;
  3252. #endif /*CONFIG_IWL4965_HT */
  3253. struct ieee80211_mgmt *mgnt =
  3254. (struct ieee80211_mgmt *)header;
  3255. priv->assoc_id = (~((1 << 15) | (1 << 14))
  3256. & le16_to_cpu(mgnt->u.assoc_resp.aid));
  3257. priv->assoc_capability =
  3258. le16_to_cpu(
  3259. mgnt->u.assoc_resp.capab_info);
  3260. #ifdef CONFIG_IWL4965_HT
  3261. pos = mgnt->u.assoc_resp.variable;
  3262. if (!parse_elems(pos,
  3263. len - (pos - (u8 *) mgnt),
  3264. &elems)) {
  3265. if (elems.ht_extra_param &&
  3266. elems.ht_cap_param)
  3267. break;
  3268. }
  3269. #endif /*CONFIG_IWL4965_HT */
  3270. /* assoc_id is 0 no association */
  3271. if (!priv->assoc_id)
  3272. break;
  3273. if (priv->beacon_int)
  3274. queue_work(priv->workqueue,
  3275. &priv->post_associate.work);
  3276. else
  3277. priv->call_post_assoc_from_beacon = 1;
  3278. }
  3279. break;
  3280. case IEEE80211_STYPE_PROBE_REQ:
  3281. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  3282. !iwl4965_is_associated(priv)) {
  3283. DECLARE_MAC_BUF(mac1);
  3284. DECLARE_MAC_BUF(mac2);
  3285. DECLARE_MAC_BUF(mac3);
  3286. IWL_DEBUG_DROP("Dropping (non network): "
  3287. "%s, %s, %s\n",
  3288. print_mac(mac1, header->addr1),
  3289. print_mac(mac2, header->addr2),
  3290. print_mac(mac3, header->addr3));
  3291. return;
  3292. }
  3293. }
  3294. iwl4965_handle_data_packet(priv, 0, include_phy, rxb, &stats);
  3295. break;
  3296. case IEEE80211_FTYPE_CTL:
  3297. #ifdef CONFIG_IWL4965_HT_AGG
  3298. switch (fc & IEEE80211_FCTL_STYPE) {
  3299. case IEEE80211_STYPE_BACK_REQ:
  3300. IWL_DEBUG_HT("IEEE80211_STYPE_BACK_REQ arrived\n");
  3301. iwl4965_handle_data_packet(priv, 0, include_phy,
  3302. rxb, &stats);
  3303. break;
  3304. default:
  3305. break;
  3306. }
  3307. #endif
  3308. break;
  3309. case IEEE80211_FTYPE_DATA: {
  3310. DECLARE_MAC_BUF(mac1);
  3311. DECLARE_MAC_BUF(mac2);
  3312. DECLARE_MAC_BUF(mac3);
  3313. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  3314. iwl4965_update_ps_mode(priv, fc & IEEE80211_FCTL_PM,
  3315. header->addr2);
  3316. if (unlikely(!network_packet))
  3317. IWL_DEBUG_DROP("Dropping (non network): "
  3318. "%s, %s, %s\n",
  3319. print_mac(mac1, header->addr1),
  3320. print_mac(mac2, header->addr2),
  3321. print_mac(mac3, header->addr3));
  3322. else if (unlikely(iwl4965_is_duplicate_packet(priv, header)))
  3323. IWL_DEBUG_DROP("Dropping (dup): %s, %s, %s\n",
  3324. print_mac(mac1, header->addr1),
  3325. print_mac(mac2, header->addr2),
  3326. print_mac(mac3, header->addr3));
  3327. else
  3328. iwl4965_handle_data_packet(priv, 1, include_phy, rxb,
  3329. &stats);
  3330. break;
  3331. }
  3332. default:
  3333. break;
  3334. }
  3335. }
  3336. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  3337. * This will be used later in iwl4965_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  3338. static void iwl4965_rx_reply_rx_phy(struct iwl4965_priv *priv,
  3339. struct iwl4965_rx_mem_buffer *rxb)
  3340. {
  3341. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3342. priv->last_phy_res[0] = 1;
  3343. memcpy(&priv->last_phy_res[1], &(pkt->u.raw[0]),
  3344. sizeof(struct iwl4965_rx_phy_res));
  3345. }
  3346. static void iwl4965_rx_missed_beacon_notif(struct iwl4965_priv *priv,
  3347. struct iwl4965_rx_mem_buffer *rxb)
  3348. {
  3349. #ifdef CONFIG_IWL4965_SENSITIVITY
  3350. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3351. struct iwl4965_missed_beacon_notif *missed_beacon;
  3352. missed_beacon = &pkt->u.missed_beacon;
  3353. if (le32_to_cpu(missed_beacon->consequtive_missed_beacons) > 5) {
  3354. IWL_DEBUG_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  3355. le32_to_cpu(missed_beacon->consequtive_missed_beacons),
  3356. le32_to_cpu(missed_beacon->total_missed_becons),
  3357. le32_to_cpu(missed_beacon->num_recvd_beacons),
  3358. le32_to_cpu(missed_beacon->num_expected_beacons));
  3359. priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
  3360. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)))
  3361. queue_work(priv->workqueue, &priv->sensitivity_work);
  3362. }
  3363. #endif /*CONFIG_IWL4965_SENSITIVITY*/
  3364. }
  3365. #ifdef CONFIG_IWL4965_HT
  3366. #ifdef CONFIG_IWL4965_HT_AGG
  3367. static void iwl4965_set_tx_status(struct iwl4965_priv *priv, int txq_id, int idx,
  3368. u32 status, u32 retry_count, u32 rate)
  3369. {
  3370. struct ieee80211_tx_status *tx_status =
  3371. &(priv->txq[txq_id].txb[idx].status);
  3372. tx_status->flags = status ? IEEE80211_TX_STATUS_ACK : 0;
  3373. tx_status->retry_count += retry_count;
  3374. tx_status->control.tx_rate = rate;
  3375. }
  3376. static void iwl4965_sta_modify_enable_tid_tx(struct iwl4965_priv *priv,
  3377. int sta_id, int tid)
  3378. {
  3379. unsigned long flags;
  3380. spin_lock_irqsave(&priv->sta_lock, flags);
  3381. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  3382. priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  3383. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3384. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3385. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3386. }
  3387. static int iwl4965_tx_status_reply_compressed_ba(struct iwl4965_priv *priv,
  3388. struct iwl4965_ht_agg *agg,
  3389. struct iwl4965_compressed_ba_resp*
  3390. ba_resp)
  3391. {
  3392. int i, sh, ack;
  3393. u16 ba_seq_ctl = le16_to_cpu(ba_resp->ba_seq_ctl);
  3394. u32 bitmap0, bitmap1;
  3395. u32 resp_bitmap0 = le32_to_cpu(ba_resp->ba_bitmap0);
  3396. u32 resp_bitmap1 = le32_to_cpu(ba_resp->ba_bitmap1);
  3397. if (unlikely(!agg->wait_for_ba)) {
  3398. IWL_ERROR("Received BA when not expected\n");
  3399. return -EINVAL;
  3400. }
  3401. agg->wait_for_ba = 0;
  3402. IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->ba_seq_ctl);
  3403. sh = agg->start_idx - SEQ_TO_INDEX(ba_seq_ctl>>4);
  3404. if (sh < 0) /* tbw something is wrong with indices */
  3405. sh += 0x100;
  3406. /* don't use 64 bits for now */
  3407. bitmap0 = resp_bitmap0 >> sh;
  3408. bitmap1 = resp_bitmap1 >> sh;
  3409. bitmap0 |= (resp_bitmap1 & ((1<<sh)|((1<<sh)-1))) << (32 - sh);
  3410. if (agg->frame_count > (64 - sh)) {
  3411. IWL_DEBUG_TX_REPLY("more frames than bitmap size");
  3412. return -1;
  3413. }
  3414. /* check for success or failure according to the
  3415. * transmitted bitmap and back bitmap */
  3416. bitmap0 &= agg->bitmap0;
  3417. bitmap1 &= agg->bitmap1;
  3418. for (i = 0; i < agg->frame_count ; i++) {
  3419. int idx = (agg->start_idx + i) & 0xff;
  3420. ack = bitmap0 & (1 << i);
  3421. IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  3422. ack? "ACK":"NACK", i, idx, agg->start_idx + i);
  3423. iwl4965_set_tx_status(priv, agg->txq_id, idx, ack, 0,
  3424. agg->rate_n_flags);
  3425. }
  3426. IWL_DEBUG_TX_REPLY("Bitmap %x%x\n", bitmap0, bitmap1);
  3427. return 0;
  3428. }
  3429. static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
  3430. {
  3431. return (index == 0) ? n_bd - 1 : index - 1;
  3432. }
  3433. static void iwl4965_rx_reply_compressed_ba(struct iwl4965_priv *priv,
  3434. struct iwl4965_rx_mem_buffer *rxb)
  3435. {
  3436. struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
  3437. struct iwl4965_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  3438. int index;
  3439. struct iwl4965_tx_queue *txq = NULL;
  3440. struct iwl4965_ht_agg *agg;
  3441. u16 ba_resp_scd_flow = le16_to_cpu(ba_resp->scd_flow);
  3442. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  3443. if (ba_resp_scd_flow >= ARRAY_SIZE(priv->txq)) {
  3444. IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
  3445. return;
  3446. }
  3447. txq = &priv->txq[ba_resp_scd_flow];
  3448. agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
  3449. index = iwl4965_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  3450. /* TODO: Need to get this copy more safely - now good for debug */
  3451. /*
  3452. {
  3453. DECLARE_MAC_BUF(mac);
  3454. IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
  3455. "sta_id = %d\n",
  3456. agg->wait_for_ba,
  3457. print_mac(mac, (u8*) &ba_resp->sta_addr_lo32),
  3458. ba_resp->sta_id);
  3459. IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%X%X, scd_flow = "
  3460. "%d, scd_ssn = %d\n",
  3461. ba_resp->tid,
  3462. ba_resp->ba_seq_ctl,
  3463. ba_resp->ba_bitmap1,
  3464. ba_resp->ba_bitmap0,
  3465. ba_resp->scd_flow,
  3466. ba_resp->scd_ssn);
  3467. IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%X%X \n",
  3468. agg->start_idx,
  3469. agg->bitmap1,
  3470. agg->bitmap0);
  3471. }
  3472. */
  3473. iwl4965_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  3474. /* releases all the TFDs until the SSN */
  3475. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff))
  3476. iwl4965_tx_queue_reclaim(priv, ba_resp_scd_flow, index);
  3477. }
  3478. static void iwl4965_tx_queue_stop_scheduler(struct iwl4965_priv *priv, u16 txq_id)
  3479. {
  3480. iwl4965_write_prph(priv,
  3481. KDR_SCD_QUEUE_STATUS_BITS(txq_id),
  3482. (0 << SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  3483. (1 << SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  3484. }
  3485. static int iwl4965_tx_queue_set_q2ratid(struct iwl4965_priv *priv, u16 ra_tid,
  3486. u16 txq_id)
  3487. {
  3488. u32 tbl_dw_addr;
  3489. u32 tbl_dw;
  3490. u16 scd_q2ratid;
  3491. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  3492. tbl_dw_addr = priv->scd_base_addr +
  3493. SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  3494. tbl_dw = iwl4965_read_targ_mem(priv, tbl_dw_addr);
  3495. if (txq_id & 0x1)
  3496. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  3497. else
  3498. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  3499. iwl4965_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  3500. return 0;
  3501. }
  3502. /**
  3503. * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
  3504. */
  3505. static int iwl4965_tx_queue_agg_enable(struct iwl4965_priv *priv, int txq_id,
  3506. int tx_fifo, int sta_id, int tid,
  3507. u16 ssn_idx)
  3508. {
  3509. unsigned long flags;
  3510. int rc;
  3511. u16 ra_tid;
  3512. if (IWL_BACK_QUEUE_FIRST_ID > txq_id)
  3513. IWL_WARNING("queue number too small: %d, must be > %d\n",
  3514. txq_id, IWL_BACK_QUEUE_FIRST_ID);
  3515. ra_tid = BUILD_RAxTID(sta_id, tid);
  3516. iwl4965_sta_modify_enable_tid_tx(priv, sta_id, tid);
  3517. spin_lock_irqsave(&priv->lock, flags);
  3518. rc = iwl4965_grab_nic_access(priv);
  3519. if (rc) {
  3520. spin_unlock_irqrestore(&priv->lock, flags);
  3521. return rc;
  3522. }
  3523. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  3524. iwl4965_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  3525. iwl4965_set_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1<<txq_id));
  3526. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  3527. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  3528. /* supposes that ssn_idx is valid (!= 0xFFF) */
  3529. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  3530. iwl4965_write_targ_mem(priv,
  3531. priv->scd_base_addr + SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  3532. (SCD_WIN_SIZE << SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  3533. SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  3534. iwl4965_write_targ_mem(priv, priv->scd_base_addr +
  3535. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  3536. (SCD_FRAME_LIMIT << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  3537. & SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  3538. iwl4965_set_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
  3539. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  3540. iwl4965_release_nic_access(priv);
  3541. spin_unlock_irqrestore(&priv->lock, flags);
  3542. return 0;
  3543. }
  3544. /**
  3545. * txq_id must be greater than IWL_BACK_QUEUE_FIRST_ID
  3546. */
  3547. static int iwl4965_tx_queue_agg_disable(struct iwl4965_priv *priv, u16 txq_id,
  3548. u16 ssn_idx, u8 tx_fifo)
  3549. {
  3550. unsigned long flags;
  3551. int rc;
  3552. if (IWL_BACK_QUEUE_FIRST_ID > txq_id) {
  3553. IWL_WARNING("queue number too small: %d, must be > %d\n",
  3554. txq_id, IWL_BACK_QUEUE_FIRST_ID);
  3555. return -EINVAL;
  3556. }
  3557. spin_lock_irqsave(&priv->lock, flags);
  3558. rc = iwl4965_grab_nic_access(priv);
  3559. if (rc) {
  3560. spin_unlock_irqrestore(&priv->lock, flags);
  3561. return rc;
  3562. }
  3563. iwl4965_tx_queue_stop_scheduler(priv, txq_id);
  3564. iwl4965_clear_bits_prph(priv, KDR_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  3565. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  3566. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  3567. /* supposes that ssn_idx is valid (!= 0xFFF) */
  3568. iwl4965_set_wr_ptrs(priv, txq_id, ssn_idx);
  3569. iwl4965_clear_bits_prph(priv, KDR_SCD_INTERRUPT_MASK, (1 << txq_id));
  3570. iwl4965_txq_ctx_deactivate(priv, txq_id);
  3571. iwl4965_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  3572. iwl4965_release_nic_access(priv);
  3573. spin_unlock_irqrestore(&priv->lock, flags);
  3574. return 0;
  3575. }
  3576. #endif/* CONFIG_IWL4965_HT_AGG */
  3577. #endif /* CONFIG_IWL4965_HT */
  3578. /**
  3579. * iwl4965_add_station - Initialize a station's hardware rate table
  3580. *
  3581. * The uCode contains a table of fallback rates and retries per rate
  3582. * for automatic fallback during transmission.
  3583. *
  3584. * NOTE: This initializes the table for a single retry per data rate
  3585. * which is not optimal. Setting up an intelligent retry per rate
  3586. * requires feedback from transmission, which isn't exposed through
  3587. * rc80211_simple which is what this driver is currently using.
  3588. *
  3589. */
  3590. void iwl4965_add_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
  3591. {
  3592. int i, r;
  3593. struct iwl4965_link_quality_cmd link_cmd = {
  3594. .reserved1 = 0,
  3595. };
  3596. u16 rate_flags;
  3597. /* Set up the rate scaling to start at 54M and fallback
  3598. * all the way to 1M in IEEE order and then spin on IEEE */
  3599. if (is_ap)
  3600. r = IWL_RATE_54M_INDEX;
  3601. else if (priv->phymode == MODE_IEEE80211A)
  3602. r = IWL_RATE_6M_INDEX;
  3603. else
  3604. r = IWL_RATE_1M_INDEX;
  3605. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  3606. rate_flags = 0;
  3607. if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
  3608. rate_flags |= RATE_MCS_CCK_MSK;
  3609. rate_flags |= RATE_MCS_ANT_B_MSK;
  3610. rate_flags &= ~RATE_MCS_ANT_A_MSK;
  3611. link_cmd.rs_table[i].rate_n_flags =
  3612. iwl4965_hw_set_rate_n_flags(iwl4965_rates[r].plcp, rate_flags);
  3613. r = iwl4965_get_prev_ieee_rate(r);
  3614. }
  3615. link_cmd.general_params.single_stream_ant_msk = 2;
  3616. link_cmd.general_params.dual_stream_ant_msk = 3;
  3617. link_cmd.agg_params.agg_dis_start_th = 3;
  3618. link_cmd.agg_params.agg_time_limit = cpu_to_le16(4000);
  3619. /* Update the rate scaling for control frame Tx to AP */
  3620. link_cmd.sta_id = is_ap ? IWL_AP_ID : IWL4965_BROADCAST_ID;
  3621. iwl4965_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD, sizeof(link_cmd),
  3622. &link_cmd);
  3623. }
  3624. #ifdef CONFIG_IWL4965_HT
  3625. static u8 iwl4965_is_channel_extension(struct iwl4965_priv *priv, int phymode,
  3626. u16 channel, u8 extension_chan_offset)
  3627. {
  3628. const struct iwl4965_channel_info *ch_info;
  3629. ch_info = iwl4965_get_channel_info(priv, phymode, channel);
  3630. if (!is_channel_valid(ch_info))
  3631. return 0;
  3632. if (extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_AUTO)
  3633. return 0;
  3634. if ((ch_info->fat_extension_channel == extension_chan_offset) ||
  3635. (ch_info->fat_extension_channel == HT_IE_EXT_CHANNEL_MAX))
  3636. return 1;
  3637. return 0;
  3638. }
  3639. static u8 iwl4965_is_fat_tx_allowed(struct iwl4965_priv *priv,
  3640. const struct sta_ht_info *ht_info)
  3641. {
  3642. if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
  3643. return 0;
  3644. if (ht_info->supported_chan_width != IWL_CHANNEL_WIDTH_40MHZ)
  3645. return 0;
  3646. if (ht_info->extension_chan_offset == IWL_EXT_CHANNEL_OFFSET_AUTO)
  3647. return 0;
  3648. /* no fat tx allowed on 2.4GHZ */
  3649. if (priv->phymode != MODE_IEEE80211A)
  3650. return 0;
  3651. return (iwl4965_is_channel_extension(priv, priv->phymode,
  3652. ht_info->control_channel,
  3653. ht_info->extension_chan_offset));
  3654. }
  3655. void iwl4965_set_rxon_ht(struct iwl4965_priv *priv, struct sta_ht_info *ht_info)
  3656. {
  3657. struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
  3658. u32 val;
  3659. if (!ht_info->is_ht)
  3660. return;
  3661. if (iwl4965_is_fat_tx_allowed(priv, ht_info))
  3662. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3663. else
  3664. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
  3665. RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
  3666. if (le16_to_cpu(rxon->channel) != ht_info->control_channel) {
  3667. IWL_DEBUG_ASSOC("control diff than current %d %d\n",
  3668. le16_to_cpu(rxon->channel),
  3669. ht_info->control_channel);
  3670. rxon->channel = cpu_to_le16(ht_info->control_channel);
  3671. return;
  3672. }
  3673. /* Note: control channel is oposit to extension channel */
  3674. switch (ht_info->extension_chan_offset) {
  3675. case IWL_EXT_CHANNEL_OFFSET_ABOVE:
  3676. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  3677. break;
  3678. case IWL_EXT_CHANNEL_OFFSET_BELOW:
  3679. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  3680. break;
  3681. case IWL_EXT_CHANNEL_OFFSET_AUTO:
  3682. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3683. break;
  3684. default:
  3685. rxon->flags &= ~RXON_FLG_CHANNEL_MODE_MIXED_MSK;
  3686. break;
  3687. }
  3688. val = ht_info->operating_mode;
  3689. rxon->flags |= cpu_to_le32(val << RXON_FLG_HT_OPERATING_MODE_POS);
  3690. priv->active_rate_ht[0] = ht_info->supp_rates[0];
  3691. priv->active_rate_ht[1] = ht_info->supp_rates[1];
  3692. iwl4965_set_rxon_chain(priv);
  3693. IWL_DEBUG_ASSOC("supported HT rate 0x%X %X "
  3694. "rxon flags 0x%X operation mode :0x%X "
  3695. "extension channel offset 0x%x "
  3696. "control chan %d\n",
  3697. priv->active_rate_ht[0], priv->active_rate_ht[1],
  3698. le32_to_cpu(rxon->flags), ht_info->operating_mode,
  3699. ht_info->extension_chan_offset,
  3700. ht_info->control_channel);
  3701. return;
  3702. }
  3703. void iwl4965_set_ht_add_station(struct iwl4965_priv *priv, u8 index)
  3704. {
  3705. __le32 sta_flags;
  3706. struct sta_ht_info *ht_info = &priv->current_assoc_ht;
  3707. priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
  3708. if (!ht_info->is_ht)
  3709. goto done;
  3710. sta_flags = priv->stations[index].sta.station_flags;
  3711. if (ht_info->tx_mimo_ps_mode == IWL_MIMO_PS_DYNAMIC)
  3712. sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
  3713. else
  3714. sta_flags &= ~STA_FLG_RTS_MIMO_PROT_MSK;
  3715. sta_flags |= cpu_to_le32(
  3716. (u32)ht_info->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
  3717. sta_flags |= cpu_to_le32(
  3718. (u32)ht_info->mpdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
  3719. sta_flags &= (~STA_FLG_FAT_EN_MSK);
  3720. ht_info->tx_chan_width = IWL_CHANNEL_WIDTH_20MHZ;
  3721. ht_info->chan_width_cap = IWL_CHANNEL_WIDTH_20MHZ;
  3722. if (iwl4965_is_fat_tx_allowed(priv, ht_info)) {
  3723. sta_flags |= STA_FLG_FAT_EN_MSK;
  3724. ht_info->chan_width_cap = IWL_CHANNEL_WIDTH_40MHZ;
  3725. if (ht_info->supported_chan_width == IWL_CHANNEL_WIDTH_40MHZ)
  3726. ht_info->tx_chan_width = IWL_CHANNEL_WIDTH_40MHZ;
  3727. }
  3728. priv->current_channel_width = ht_info->tx_chan_width;
  3729. priv->stations[index].sta.station_flags = sta_flags;
  3730. done:
  3731. return;
  3732. }
  3733. #ifdef CONFIG_IWL4965_HT_AGG
  3734. static void iwl4965_sta_modify_add_ba_tid(struct iwl4965_priv *priv,
  3735. int sta_id, int tid, u16 ssn)
  3736. {
  3737. unsigned long flags;
  3738. spin_lock_irqsave(&priv->sta_lock, flags);
  3739. priv->stations[sta_id].sta.station_flags_msk = 0;
  3740. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  3741. priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  3742. priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  3743. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3744. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3745. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3746. }
  3747. static void iwl4965_sta_modify_del_ba_tid(struct iwl4965_priv *priv,
  3748. int sta_id, int tid)
  3749. {
  3750. unsigned long flags;
  3751. spin_lock_irqsave(&priv->sta_lock, flags);
  3752. priv->stations[sta_id].sta.station_flags_msk = 0;
  3753. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  3754. priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  3755. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3756. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3757. iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
  3758. }
  3759. static const u16 default_tid_to_tx_fifo[] = {
  3760. IWL_TX_FIFO_AC1,
  3761. IWL_TX_FIFO_AC0,
  3762. IWL_TX_FIFO_AC0,
  3763. IWL_TX_FIFO_AC1,
  3764. IWL_TX_FIFO_AC2,
  3765. IWL_TX_FIFO_AC2,
  3766. IWL_TX_FIFO_AC3,
  3767. IWL_TX_FIFO_AC3,
  3768. IWL_TX_FIFO_NONE,
  3769. IWL_TX_FIFO_NONE,
  3770. IWL_TX_FIFO_NONE,
  3771. IWL_TX_FIFO_NONE,
  3772. IWL_TX_FIFO_NONE,
  3773. IWL_TX_FIFO_NONE,
  3774. IWL_TX_FIFO_NONE,
  3775. IWL_TX_FIFO_NONE,
  3776. IWL_TX_FIFO_AC3
  3777. };
  3778. static int iwl4965_txq_ctx_activate_free(struct iwl4965_priv *priv)
  3779. {
  3780. int txq_id;
  3781. for (txq_id = 0; txq_id < priv->hw_setting.max_txq_num; txq_id++)
  3782. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  3783. return txq_id;
  3784. return -1;
  3785. }
  3786. int iwl4965_mac_ht_tx_agg_start(struct ieee80211_hw *hw, u8 *da, u16 tid,
  3787. u16 *start_seq_num)
  3788. {
  3789. struct iwl4965_priv *priv = hw->priv;
  3790. int sta_id;
  3791. int tx_fifo;
  3792. int txq_id;
  3793. int ssn = -1;
  3794. unsigned long flags;
  3795. struct iwl4965_tid_data *tid_data;
  3796. DECLARE_MAC_BUF(mac);
  3797. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  3798. tx_fifo = default_tid_to_tx_fifo[tid];
  3799. else
  3800. return -EINVAL;
  3801. IWL_WARNING("iwl-AGG iwl4965_mac_ht_tx_agg_start on da=%s"
  3802. " tid=%d\n", print_mac(mac, da), tid);
  3803. sta_id = iwl4965_hw_find_station(priv, da);
  3804. if (sta_id == IWL_INVALID_STATION)
  3805. return -ENXIO;
  3806. txq_id = iwl4965_txq_ctx_activate_free(priv);
  3807. if (txq_id == -1)
  3808. return -ENXIO;
  3809. spin_lock_irqsave(&priv->sta_lock, flags);
  3810. tid_data = &priv->stations[sta_id].tid[tid];
  3811. ssn = SEQ_TO_SN(tid_data->seq_number);
  3812. tid_data->agg.txq_id = txq_id;
  3813. spin_unlock_irqrestore(&priv->sta_lock, flags);
  3814. *start_seq_num = ssn;
  3815. iwl4965_ba_status(priv, tid, BA_STATUS_ACTIVE);
  3816. return iwl4965_tx_queue_agg_enable(priv, txq_id, tx_fifo,
  3817. sta_id, tid, ssn);
  3818. }
  3819. int iwl4965_mac_ht_tx_agg_stop(struct ieee80211_hw *hw, u8 *da, u16 tid,
  3820. int generator)
  3821. {
  3822. struct iwl4965_priv *priv = hw->priv;
  3823. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  3824. struct iwl4965_tid_data *tid_data;
  3825. int rc;
  3826. DECLARE_MAC_BUF(mac);
  3827. if (!da) {
  3828. IWL_ERROR("%s: da = NULL\n", __func__);
  3829. return -EINVAL;
  3830. }
  3831. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  3832. tx_fifo_id = default_tid_to_tx_fifo[tid];
  3833. else
  3834. return -EINVAL;
  3835. sta_id = iwl4965_hw_find_station(priv, da);
  3836. if (sta_id == IWL_INVALID_STATION)
  3837. return -ENXIO;
  3838. tid_data = &priv->stations[sta_id].tid[tid];
  3839. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  3840. txq_id = tid_data->agg.txq_id;
  3841. rc = iwl4965_tx_queue_agg_disable(priv, txq_id, ssn, tx_fifo_id);
  3842. /* FIXME: need more safe way to handle error condition */
  3843. if (rc)
  3844. return rc;
  3845. iwl4965_ba_status(priv, tid, BA_STATUS_INITIATOR_DELBA);
  3846. IWL_DEBUG_INFO("iwl4965_mac_ht_tx_agg_stop on da=%s tid=%d\n",
  3847. print_mac(mac, da), tid);
  3848. return 0;
  3849. }
  3850. int iwl4965_mac_ht_rx_agg_start(struct ieee80211_hw *hw, u8 *da,
  3851. u16 tid, u16 start_seq_num)
  3852. {
  3853. struct iwl4965_priv *priv = hw->priv;
  3854. int sta_id;
  3855. DECLARE_MAC_BUF(mac);
  3856. IWL_WARNING("iwl-AGG iwl4965_mac_ht_rx_agg_start on da=%s"
  3857. " tid=%d\n", print_mac(mac, da), tid);
  3858. sta_id = iwl4965_hw_find_station(priv, da);
  3859. iwl4965_sta_modify_add_ba_tid(priv, sta_id, tid, start_seq_num);
  3860. return 0;
  3861. }
  3862. int iwl4965_mac_ht_rx_agg_stop(struct ieee80211_hw *hw, u8 *da,
  3863. u16 tid, int generator)
  3864. {
  3865. struct iwl4965_priv *priv = hw->priv;
  3866. int sta_id;
  3867. DECLARE_MAC_BUF(mac);
  3868. IWL_WARNING("iwl-AGG iwl4965_mac_ht_rx_agg_stop on da=%s tid=%d\n",
  3869. print_mac(mac, da), tid);
  3870. sta_id = iwl4965_hw_find_station(priv, da);
  3871. iwl4965_sta_modify_del_ba_tid(priv, sta_id, tid);
  3872. return 0;
  3873. }
  3874. #endif /* CONFIG_IWL4965_HT_AGG */
  3875. #endif /* CONFIG_IWL4965_HT */
  3876. /* Set up 4965-specific Rx frame reply handlers */
  3877. void iwl4965_hw_rx_handler_setup(struct iwl4965_priv *priv)
  3878. {
  3879. /* Legacy Rx frames */
  3880. priv->rx_handlers[REPLY_4965_RX] = iwl4965_rx_reply_rx;
  3881. /* High-throughput (HT) Rx frames */
  3882. priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
  3883. priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
  3884. priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
  3885. iwl4965_rx_missed_beacon_notif;
  3886. #ifdef CONFIG_IWL4965_HT
  3887. #ifdef CONFIG_IWL4965_HT_AGG
  3888. priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
  3889. #endif /* CONFIG_IWL4965_HT_AGG */
  3890. #endif /* CONFIG_IWL4965_HT */
  3891. }
  3892. void iwl4965_hw_setup_deferred_work(struct iwl4965_priv *priv)
  3893. {
  3894. INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
  3895. INIT_WORK(&priv->statistics_work, iwl4965_bg_statistics_work);
  3896. #ifdef CONFIG_IWL4965_SENSITIVITY
  3897. INIT_WORK(&priv->sensitivity_work, iwl4965_bg_sensitivity_work);
  3898. #endif
  3899. #ifdef CONFIG_IWL4965_HT
  3900. #ifdef CONFIG_IWL4965_HT_AGG
  3901. INIT_WORK(&priv->agg_work, iwl4965_bg_agg_work);
  3902. #endif /* CONFIG_IWL4965_HT_AGG */
  3903. #endif /* CONFIG_IWL4965_HT */
  3904. init_timer(&priv->statistics_periodic);
  3905. priv->statistics_periodic.data = (unsigned long)priv;
  3906. priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
  3907. }
  3908. void iwl4965_hw_cancel_deferred_work(struct iwl4965_priv *priv)
  3909. {
  3910. del_timer_sync(&priv->statistics_periodic);
  3911. cancel_delayed_work(&priv->init_alive_start);
  3912. }
  3913. struct pci_device_id iwl4965_hw_card_ids[] = {
  3914. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4229)},
  3915. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x4230)},
  3916. {0}
  3917. };
  3918. /*
  3919. * The device's EEPROM semaphore prevents conflicts between driver and uCode
  3920. * when accessing the EEPROM; each access is a series of pulses to/from the
  3921. * EEPROM chip, not a single event, so even reads could conflict if they
  3922. * weren't arbitrated by the semaphore.
  3923. */
  3924. int iwl4965_eeprom_acquire_semaphore(struct iwl4965_priv *priv)
  3925. {
  3926. u16 count;
  3927. int rc;
  3928. for (count = 0; count < EEPROM_SEM_RETRY_LIMIT; count++) {
  3929. /* Request semaphore */
  3930. iwl4965_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  3931. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  3932. /* See if we got it */
  3933. rc = iwl4965_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  3934. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  3935. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM,
  3936. EEPROM_SEM_TIMEOUT);
  3937. if (rc >= 0) {
  3938. IWL_DEBUG_IO("Acquired semaphore after %d tries.\n",
  3939. count+1);
  3940. return rc;
  3941. }
  3942. }
  3943. return rc;
  3944. }
  3945. inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
  3946. {
  3947. iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
  3948. CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
  3949. }
  3950. MODULE_DEVICE_TABLE(pci, iwl4965_hw_card_ids);