iwl-4965.c 143 KB

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