iwl-4965.c 142 KB

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