4965-mac.c 145 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/slab.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/if_arp.h>
  44. #include <net/mac80211.h>
  45. #include <asm/div64.h>
  46. #define DRV_NAME "iwl4965"
  47. #include "iwl-eeprom.h"
  48. #include "iwl-dev.h"
  49. #include "iwl-core.h"
  50. #include "iwl-io.h"
  51. #include "iwl-helpers.h"
  52. #include "iwl-sta.h"
  53. #include "iwl-4965-calib.h"
  54. #include "iwl-4965.h"
  55. /******************************************************************************
  56. *
  57. * module boiler plate
  58. *
  59. ******************************************************************************/
  60. /*
  61. * module name, copyright, version, etc.
  62. */
  63. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
  64. #ifdef CONFIG_IWLEGACY_DEBUG
  65. #define VD "d"
  66. #else
  67. #define VD
  68. #endif
  69. #define DRV_VERSION IWLWIFI_VERSION VD
  70. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  71. MODULE_VERSION(DRV_VERSION);
  72. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  73. MODULE_LICENSE("GPL");
  74. MODULE_ALIAS("iwl4965");
  75. void il4965_rx_missed_beacon_notif(struct il_priv *il,
  76. struct il_rx_buf *rxb)
  77. {
  78. struct il_rx_pkt *pkt = rxb_addr(rxb);
  79. struct il_missed_beacon_notif *missed_beacon;
  80. missed_beacon = &pkt->u.missed_beacon;
  81. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  82. il->missed_beacon_threshold) {
  83. D_CALIB(
  84. "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  85. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  86. le32_to_cpu(missed_beacon->total_missed_becons),
  87. le32_to_cpu(missed_beacon->num_recvd_beacons),
  88. le32_to_cpu(missed_beacon->num_expected_beacons));
  89. if (!test_bit(STATUS_SCANNING, &il->status))
  90. il4965_init_sensitivity(il);
  91. }
  92. }
  93. /* Calculate noise level, based on measurements during network silence just
  94. * before arriving beacon. This measurement can be done only if we know
  95. * exactly when to expect beacons, therefore only when we're associated. */
  96. static void il4965_rx_calc_noise(struct il_priv *il)
  97. {
  98. struct stats_rx_non_phy *rx_info;
  99. int num_active_rx = 0;
  100. int total_silence = 0;
  101. int bcn_silence_a, bcn_silence_b, bcn_silence_c;
  102. int last_rx_noise;
  103. rx_info = &(il->_4965.stats.rx.general);
  104. bcn_silence_a =
  105. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  106. bcn_silence_b =
  107. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  108. bcn_silence_c =
  109. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  110. if (bcn_silence_a) {
  111. total_silence += bcn_silence_a;
  112. num_active_rx++;
  113. }
  114. if (bcn_silence_b) {
  115. total_silence += bcn_silence_b;
  116. num_active_rx++;
  117. }
  118. if (bcn_silence_c) {
  119. total_silence += bcn_silence_c;
  120. num_active_rx++;
  121. }
  122. /* Average among active antennas */
  123. if (num_active_rx)
  124. last_rx_noise = (total_silence / num_active_rx) - 107;
  125. else
  126. last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
  127. D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n",
  128. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  129. last_rx_noise);
  130. }
  131. #ifdef CONFIG_IWLEGACY_DEBUGFS
  132. /*
  133. * based on the assumption of all stats counter are in DWORD
  134. * FIXME: This function is for debugging, do not deal with
  135. * the case of counters roll-over.
  136. */
  137. static void il4965_accumulative_stats(struct il_priv *il,
  138. __le32 *stats)
  139. {
  140. int i, size;
  141. __le32 *prev_stats;
  142. u32 *accum_stats;
  143. u32 *delta, *max_delta;
  144. struct stats_general_common *general, *accum_general;
  145. struct stats_tx *tx, *accum_tx;
  146. prev_stats = (__le32 *)&il->_4965.stats;
  147. accum_stats = (u32 *)&il->_4965.accum_stats;
  148. size = sizeof(struct il_notif_stats);
  149. general = &il->_4965.stats.general.common;
  150. accum_general = &il->_4965.accum_stats.general.common;
  151. tx = &il->_4965.stats.tx;
  152. accum_tx = &il->_4965.accum_stats.tx;
  153. delta = (u32 *)&il->_4965.delta_stats;
  154. max_delta = (u32 *)&il->_4965.max_delta;
  155. for (i = sizeof(__le32); i < size;
  156. i += sizeof(__le32), stats++, prev_stats++, delta++,
  157. max_delta++, accum_stats++) {
  158. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
  159. *delta = (le32_to_cpu(*stats) -
  160. le32_to_cpu(*prev_stats));
  161. *accum_stats += *delta;
  162. if (*delta > *max_delta)
  163. *max_delta = *delta;
  164. }
  165. }
  166. /* reset accumulative stats for "no-counter" type stats */
  167. accum_general->temperature = general->temperature;
  168. accum_general->ttl_timestamp = general->ttl_timestamp;
  169. }
  170. #endif
  171. #define REG_RECALIB_PERIOD (60)
  172. void il4965_rx_stats(struct il_priv *il,
  173. struct il_rx_buf *rxb)
  174. {
  175. int change;
  176. struct il_rx_pkt *pkt = rxb_addr(rxb);
  177. D_RX(
  178. "Statistics notification received (%d vs %d).\n",
  179. (int)sizeof(struct il_notif_stats),
  180. le32_to_cpu(pkt->len_n_flags) &
  181. FH_RSCSR_FRAME_SIZE_MSK);
  182. change = ((il->_4965.stats.general.common.temperature !=
  183. pkt->u.stats.general.common.temperature) ||
  184. ((il->_4965.stats.flag &
  185. STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  186. (pkt->u.stats.flag &
  187. STATISTICS_REPLY_FLG_HT40_MODE_MSK)));
  188. #ifdef CONFIG_IWLEGACY_DEBUGFS
  189. il4965_accumulative_stats(il, (__le32 *)&pkt->u.stats);
  190. #endif
  191. /* TODO: reading some of stats is unneeded */
  192. memcpy(&il->_4965.stats, &pkt->u.stats,
  193. sizeof(il->_4965.stats));
  194. set_bit(STATUS_STATISTICS, &il->status);
  195. /* Reschedule the stats timer to occur in
  196. * REG_RECALIB_PERIOD seconds to ensure we get a
  197. * thermal update even if the uCode doesn't give
  198. * us one */
  199. mod_timer(&il->stats_periodic, jiffies +
  200. msecs_to_jiffies(REG_RECALIB_PERIOD * 1000));
  201. if (unlikely(!test_bit(STATUS_SCANNING, &il->status)) &&
  202. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  203. il4965_rx_calc_noise(il);
  204. queue_work(il->workqueue, &il->run_time_calib_work);
  205. }
  206. if (il->cfg->ops->lib->temp_ops.temperature && change)
  207. il->cfg->ops->lib->temp_ops.temperature(il);
  208. }
  209. void il4965_reply_stats(struct il_priv *il,
  210. struct il_rx_buf *rxb)
  211. {
  212. struct il_rx_pkt *pkt = rxb_addr(rxb);
  213. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
  214. #ifdef CONFIG_IWLEGACY_DEBUGFS
  215. memset(&il->_4965.accum_stats, 0,
  216. sizeof(struct il_notif_stats));
  217. memset(&il->_4965.delta_stats, 0,
  218. sizeof(struct il_notif_stats));
  219. memset(&il->_4965.max_delta, 0,
  220. sizeof(struct il_notif_stats));
  221. #endif
  222. D_RX("Statistics have been cleared\n");
  223. }
  224. il4965_rx_stats(il, rxb);
  225. }
  226. static const u8 tid_to_ac[] = {
  227. IEEE80211_AC_BE,
  228. IEEE80211_AC_BK,
  229. IEEE80211_AC_BK,
  230. IEEE80211_AC_BE,
  231. IEEE80211_AC_VI,
  232. IEEE80211_AC_VI,
  233. IEEE80211_AC_VO,
  234. IEEE80211_AC_VO
  235. };
  236. static inline int il4965_get_ac_from_tid(u16 tid)
  237. {
  238. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  239. return tid_to_ac[tid];
  240. /* no support for TIDs 8-15 yet */
  241. return -EINVAL;
  242. }
  243. static inline int
  244. il4965_get_fifo_from_tid(struct il_rxon_context *ctx, u16 tid)
  245. {
  246. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  247. return ctx->ac_to_fifo[tid_to_ac[tid]];
  248. /* no support for TIDs 8-15 yet */
  249. return -EINVAL;
  250. }
  251. /*
  252. * handle build REPLY_TX command notification.
  253. */
  254. static void il4965_tx_cmd_build_basic(struct il_priv *il,
  255. struct sk_buff *skb,
  256. struct il_tx_cmd *tx_cmd,
  257. struct ieee80211_tx_info *info,
  258. struct ieee80211_hdr *hdr,
  259. u8 std_id)
  260. {
  261. __le16 fc = hdr->frame_control;
  262. __le32 tx_flags = tx_cmd->tx_flags;
  263. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  264. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  265. tx_flags |= TX_CMD_FLG_ACK_MSK;
  266. if (ieee80211_is_mgmt(fc))
  267. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  268. if (ieee80211_is_probe_resp(fc) &&
  269. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  270. tx_flags |= TX_CMD_FLG_TSF_MSK;
  271. } else {
  272. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  273. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  274. }
  275. if (ieee80211_is_back_req(fc))
  276. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  277. tx_cmd->sta_id = std_id;
  278. if (ieee80211_has_morefrags(fc))
  279. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  280. if (ieee80211_is_data_qos(fc)) {
  281. u8 *qc = ieee80211_get_qos_ctl(hdr);
  282. tx_cmd->tid_tspec = qc[0] & 0xf;
  283. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  284. } else {
  285. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  286. }
  287. il_tx_cmd_protection(il, info, fc, &tx_flags);
  288. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  289. if (ieee80211_is_mgmt(fc)) {
  290. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  291. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  292. else
  293. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  294. } else {
  295. tx_cmd->timeout.pm_frame_timeout = 0;
  296. }
  297. tx_cmd->driver_txop = 0;
  298. tx_cmd->tx_flags = tx_flags;
  299. tx_cmd->next_frame_len = 0;
  300. }
  301. #define RTS_DFAULT_RETRY_LIMIT 60
  302. static void il4965_tx_cmd_build_rate(struct il_priv *il,
  303. struct il_tx_cmd *tx_cmd,
  304. struct ieee80211_tx_info *info,
  305. __le16 fc)
  306. {
  307. u32 rate_flags;
  308. int rate_idx;
  309. u8 rts_retry_limit;
  310. u8 data_retry_limit;
  311. u8 rate_plcp;
  312. /* Set retry limit on DATA packets and Probe Responses*/
  313. if (ieee80211_is_probe_resp(fc))
  314. data_retry_limit = 3;
  315. else
  316. data_retry_limit = IL4965_DEFAULT_TX_RETRY;
  317. tx_cmd->data_retry_limit = data_retry_limit;
  318. /* Set retry limit on RTS packets */
  319. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  320. if (data_retry_limit < rts_retry_limit)
  321. rts_retry_limit = data_retry_limit;
  322. tx_cmd->rts_retry_limit = rts_retry_limit;
  323. /* DATA packets will use the uCode station table for rate/antenna
  324. * selection */
  325. if (ieee80211_is_data(fc)) {
  326. tx_cmd->initial_rate_idx = 0;
  327. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  328. return;
  329. }
  330. /**
  331. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  332. * not really a TX rate. Thus, we use the lowest supported rate for
  333. * this band. Also use the lowest supported rate if the stored rate
  334. * idx is invalid.
  335. */
  336. rate_idx = info->control.rates[0].idx;
  337. if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) ||
  338. rate_idx < 0 || rate_idx > RATE_COUNT_LEGACY)
  339. rate_idx = rate_lowest_index(&il->bands[info->band],
  340. info->control.sta);
  341. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  342. if (info->band == IEEE80211_BAND_5GHZ)
  343. rate_idx += IL_FIRST_OFDM_RATE;
  344. /* Get PLCP rate for tx_cmd->rate_n_flags */
  345. rate_plcp = il_rates[rate_idx].plcp;
  346. /* Zero out flags for this packet */
  347. rate_flags = 0;
  348. /* Set CCK flag as needed */
  349. if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
  350. rate_flags |= RATE_MCS_CCK_MSK;
  351. /* Set up antennas */
  352. il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
  353. il->hw_params.valid_tx_ant);
  354. rate_flags |= il4965_ant_idx_to_flags(il->mgmt_tx_ant);
  355. /* Set the rate in the TX cmd */
  356. tx_cmd->rate_n_flags = il4965_hw_set_rate_n_flags(rate_plcp, rate_flags);
  357. }
  358. static void il4965_tx_cmd_build_hwcrypto(struct il_priv *il,
  359. struct ieee80211_tx_info *info,
  360. struct il_tx_cmd *tx_cmd,
  361. struct sk_buff *skb_frag,
  362. int sta_id)
  363. {
  364. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  365. switch (keyconf->cipher) {
  366. case WLAN_CIPHER_SUITE_CCMP:
  367. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  368. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  369. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  370. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  371. D_TX("tx_cmd with AES hwcrypto\n");
  372. break;
  373. case WLAN_CIPHER_SUITE_TKIP:
  374. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  375. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  376. D_TX("tx_cmd with tkip hwcrypto\n");
  377. break;
  378. case WLAN_CIPHER_SUITE_WEP104:
  379. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  380. /* fall through */
  381. case WLAN_CIPHER_SUITE_WEP40:
  382. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  383. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  384. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  385. D_TX("Configuring packet for WEP encryption "
  386. "with key %d\n", keyconf->keyidx);
  387. break;
  388. default:
  389. IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
  390. break;
  391. }
  392. }
  393. /*
  394. * start REPLY_TX command process
  395. */
  396. int il4965_tx_skb(struct il_priv *il, struct sk_buff *skb)
  397. {
  398. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  399. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  400. struct ieee80211_sta *sta = info->control.sta;
  401. struct il_station_priv *sta_priv = NULL;
  402. struct il_tx_queue *txq;
  403. struct il_queue *q;
  404. struct il_device_cmd *out_cmd;
  405. struct il_cmd_meta *out_meta;
  406. struct il_tx_cmd *tx_cmd;
  407. struct il_rxon_context *ctx = &il->ctx;
  408. int txq_id;
  409. dma_addr_t phys_addr;
  410. dma_addr_t txcmd_phys;
  411. dma_addr_t scratch_phys;
  412. u16 len, firstlen, secondlen;
  413. u16 seq_number = 0;
  414. __le16 fc;
  415. u8 hdr_len;
  416. u8 sta_id;
  417. u8 wait_write_ptr = 0;
  418. u8 tid = 0;
  419. u8 *qc = NULL;
  420. unsigned long flags;
  421. bool is_agg = false;
  422. if (info->control.vif)
  423. ctx = il_rxon_ctx_from_vif(info->control.vif);
  424. spin_lock_irqsave(&il->lock, flags);
  425. if (il_is_rfkill(il)) {
  426. D_DROP("Dropping - RF KILL\n");
  427. goto drop_unlock;
  428. }
  429. fc = hdr->frame_control;
  430. #ifdef CONFIG_IWLEGACY_DEBUG
  431. if (ieee80211_is_auth(fc))
  432. D_TX("Sending AUTH frame\n");
  433. else if (ieee80211_is_assoc_req(fc))
  434. D_TX("Sending ASSOC frame\n");
  435. else if (ieee80211_is_reassoc_req(fc))
  436. D_TX("Sending REASSOC frame\n");
  437. #endif
  438. hdr_len = ieee80211_hdrlen(fc);
  439. /* For management frames use broadcast id to do not break aggregation */
  440. if (!ieee80211_is_data(fc))
  441. sta_id = ctx->bcast_sta_id;
  442. else {
  443. /* Find idx into station table for destination station */
  444. sta_id = il_sta_id_or_broadcast(il, ctx, info->control.sta);
  445. if (sta_id == IL_INVALID_STATION) {
  446. D_DROP("Dropping - INVALID STATION: %pM\n",
  447. hdr->addr1);
  448. goto drop_unlock;
  449. }
  450. }
  451. D_TX("station Id %d\n", sta_id);
  452. if (sta)
  453. sta_priv = (void *)sta->drv_priv;
  454. if (sta_priv && sta_priv->asleep &&
  455. (info->flags & IEEE80211_TX_CTL_POLL_RESPONSE)) {
  456. /*
  457. * This sends an asynchronous command to the device,
  458. * but we can rely on it being processed before the
  459. * next frame is processed -- and the next frame to
  460. * this station is the one that will consume this
  461. * counter.
  462. * For now set the counter to just 1 since we do not
  463. * support uAPSD yet.
  464. */
  465. il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
  466. }
  467. /*
  468. * Send this frame after DTIM -- there's a special queue
  469. * reserved for this for contexts that support AP mode.
  470. */
  471. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  472. txq_id = ctx->mcast_queue;
  473. /*
  474. * The microcode will clear the more data
  475. * bit in the last frame it transmits.
  476. */
  477. hdr->frame_control |=
  478. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  479. } else
  480. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  481. /* irqs already disabled/saved above when locking il->lock */
  482. spin_lock(&il->sta_lock);
  483. if (ieee80211_is_data_qos(fc)) {
  484. qc = ieee80211_get_qos_ctl(hdr);
  485. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  486. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  487. spin_unlock(&il->sta_lock);
  488. goto drop_unlock;
  489. }
  490. seq_number = il->stations[sta_id].tid[tid].seq_number;
  491. seq_number &= IEEE80211_SCTL_SEQ;
  492. hdr->seq_ctrl = hdr->seq_ctrl &
  493. cpu_to_le16(IEEE80211_SCTL_FRAG);
  494. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  495. seq_number += 0x10;
  496. /* aggregation is on for this <sta,tid> */
  497. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  498. il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
  499. txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
  500. is_agg = true;
  501. }
  502. }
  503. txq = &il->txq[txq_id];
  504. q = &txq->q;
  505. if (unlikely(il_queue_space(q) < q->high_mark)) {
  506. spin_unlock(&il->sta_lock);
  507. goto drop_unlock;
  508. }
  509. if (ieee80211_is_data_qos(fc)) {
  510. il->stations[sta_id].tid[tid].tfds_in_queue++;
  511. if (!ieee80211_has_morefrags(fc))
  512. il->stations[sta_id].tid[tid].seq_number = seq_number;
  513. }
  514. spin_unlock(&il->sta_lock);
  515. /* Set up driver data for this TFD */
  516. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct il_tx_info));
  517. txq->txb[q->write_ptr].skb = skb;
  518. txq->txb[q->write_ptr].ctx = ctx;
  519. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  520. out_cmd = txq->cmd[q->write_ptr];
  521. out_meta = &txq->meta[q->write_ptr];
  522. tx_cmd = &out_cmd->cmd.tx;
  523. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  524. memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
  525. /*
  526. * Set up the Tx-command (not MAC!) header.
  527. * Store the chosen Tx queue and TFD idx within the sequence field;
  528. * after Tx, uCode's Tx response will return this value so driver can
  529. * locate the frame within the tx queue and do post-tx processing.
  530. */
  531. out_cmd->hdr.cmd = REPLY_TX;
  532. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  533. IDX_TO_SEQ(q->write_ptr)));
  534. /* Copy MAC header from skb into command buffer */
  535. memcpy(tx_cmd->hdr, hdr, hdr_len);
  536. /* Total # bytes to be transmitted */
  537. len = (u16)skb->len;
  538. tx_cmd->len = cpu_to_le16(len);
  539. if (info->control.hw_key)
  540. il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
  541. /* TODO need this for burst mode later on */
  542. il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
  543. il_dbg_log_tx_data_frame(il, len, hdr);
  544. il4965_tx_cmd_build_rate(il, tx_cmd, info, fc);
  545. il_update_stats(il, true, fc, len);
  546. /*
  547. * Use the first empty entry in this queue's command buffer array
  548. * to contain the Tx command and MAC header concatenated together
  549. * (payload data will be in another buffer).
  550. * Size of this varies, due to varying MAC header length.
  551. * If end is not dword aligned, we'll have 2 extra bytes at the end
  552. * of the MAC header (device reads on dword boundaries).
  553. * We'll tell device about this padding later.
  554. */
  555. len = sizeof(struct il_tx_cmd) +
  556. sizeof(struct il_cmd_header) + hdr_len;
  557. firstlen = (len + 3) & ~3;
  558. /* Tell NIC about any 2-byte padding after MAC header */
  559. if (firstlen != len)
  560. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  561. /* Physical address of this Tx command's header (not MAC header!),
  562. * within command buffer array. */
  563. txcmd_phys = pci_map_single(il->pci_dev,
  564. &out_cmd->hdr, firstlen,
  565. PCI_DMA_BIDIRECTIONAL);
  566. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  567. dma_unmap_len_set(out_meta, len, firstlen);
  568. /* Add buffer containing Tx command and MAC(!) header to TFD's
  569. * first entry */
  570. il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
  571. txcmd_phys, firstlen, 1, 0);
  572. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  573. txq->need_update = 1;
  574. } else {
  575. wait_write_ptr = 1;
  576. txq->need_update = 0;
  577. }
  578. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  579. * if any (802.11 null frames have no payload). */
  580. secondlen = skb->len - hdr_len;
  581. if (secondlen > 0) {
  582. phys_addr = pci_map_single(il->pci_dev, skb->data + hdr_len,
  583. secondlen, PCI_DMA_TODEVICE);
  584. il->cfg->ops->lib->txq_attach_buf_to_tfd(il, txq,
  585. phys_addr, secondlen,
  586. 0, 0);
  587. }
  588. scratch_phys = txcmd_phys + sizeof(struct il_cmd_header) +
  589. offsetof(struct il_tx_cmd, scratch);
  590. /* take back ownership of DMA buffer to enable update */
  591. pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys,
  592. firstlen, PCI_DMA_BIDIRECTIONAL);
  593. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  594. tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
  595. D_TX("sequence nr = 0X%x\n",
  596. le16_to_cpu(out_cmd->hdr.sequence));
  597. D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  598. il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  599. il_print_hex_dump(il, IL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  600. /* Set up entry for this TFD in Tx byte-count array */
  601. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  602. il->cfg->ops->lib->txq_update_byte_cnt_tbl(il, txq,
  603. le16_to_cpu(tx_cmd->len));
  604. pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys,
  605. firstlen, PCI_DMA_BIDIRECTIONAL);
  606. /* Tell device the write idx *just past* this latest filled TFD */
  607. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  608. il_txq_update_write_ptr(il, txq);
  609. spin_unlock_irqrestore(&il->lock, flags);
  610. /*
  611. * At this point the frame is "transmitted" successfully
  612. * and we will get a TX status notification eventually,
  613. * regardless of the value of ret. "ret" only indicates
  614. * whether or not we should update the write pointer.
  615. */
  616. /*
  617. * Avoid atomic ops if it isn't an associated client.
  618. * Also, if this is a packet for aggregation, don't
  619. * increase the counter because the ucode will stop
  620. * aggregation queues when their respective station
  621. * goes to sleep.
  622. */
  623. if (sta_priv && sta_priv->client && !is_agg)
  624. atomic_inc(&sta_priv->pending_frames);
  625. if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
  626. if (wait_write_ptr) {
  627. spin_lock_irqsave(&il->lock, flags);
  628. txq->need_update = 1;
  629. il_txq_update_write_ptr(il, txq);
  630. spin_unlock_irqrestore(&il->lock, flags);
  631. } else {
  632. il_stop_queue(il, txq);
  633. }
  634. }
  635. return 0;
  636. drop_unlock:
  637. spin_unlock_irqrestore(&il->lock, flags);
  638. return -1;
  639. }
  640. static inline int il4965_alloc_dma_ptr(struct il_priv *il,
  641. struct il_dma_ptr *ptr, size_t size)
  642. {
  643. ptr->addr = dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma,
  644. GFP_KERNEL);
  645. if (!ptr->addr)
  646. return -ENOMEM;
  647. ptr->size = size;
  648. return 0;
  649. }
  650. static inline void il4965_free_dma_ptr(struct il_priv *il,
  651. struct il_dma_ptr *ptr)
  652. {
  653. if (unlikely(!ptr->addr))
  654. return;
  655. dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  656. memset(ptr, 0, sizeof(*ptr));
  657. }
  658. /**
  659. * il4965_hw_txq_ctx_free - Free TXQ Context
  660. *
  661. * Destroy all TX DMA queues and structures
  662. */
  663. void il4965_hw_txq_ctx_free(struct il_priv *il)
  664. {
  665. int txq_id;
  666. /* Tx queues */
  667. if (il->txq) {
  668. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  669. if (txq_id == il->cmd_queue)
  670. il_cmd_queue_free(il);
  671. else
  672. il_tx_queue_free(il, txq_id);
  673. }
  674. il4965_free_dma_ptr(il, &il->kw);
  675. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  676. /* free tx queue structure */
  677. il_txq_mem(il);
  678. }
  679. /**
  680. * il4965_txq_ctx_alloc - allocate TX queue context
  681. * Allocate all Tx DMA structures and initialize them
  682. *
  683. * @param il
  684. * @return error code
  685. */
  686. int il4965_txq_ctx_alloc(struct il_priv *il)
  687. {
  688. int ret;
  689. int txq_id, slots_num;
  690. unsigned long flags;
  691. /* Free all tx/cmd queues and keep-warm buffer */
  692. il4965_hw_txq_ctx_free(il);
  693. ret = il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
  694. il->hw_params.scd_bc_tbls_size);
  695. if (ret) {
  696. IL_ERR("Scheduler BC Table allocation failed\n");
  697. goto error_bc_tbls;
  698. }
  699. /* Alloc keep-warm buffer */
  700. ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
  701. if (ret) {
  702. IL_ERR("Keep Warm allocation failed\n");
  703. goto error_kw;
  704. }
  705. /* allocate tx queue structure */
  706. ret = il_alloc_txq_mem(il);
  707. if (ret)
  708. goto error;
  709. spin_lock_irqsave(&il->lock, flags);
  710. /* Turn off all Tx DMA fifos */
  711. il4965_txq_set_sched(il, 0);
  712. /* Tell NIC where to find the "keep warm" buffer */
  713. il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  714. spin_unlock_irqrestore(&il->lock, flags);
  715. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  716. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  717. slots_num = (txq_id == il->cmd_queue) ?
  718. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  719. ret = il_tx_queue_init(il,
  720. &il->txq[txq_id], slots_num,
  721. txq_id);
  722. if (ret) {
  723. IL_ERR("Tx %d queue init failed\n", txq_id);
  724. goto error;
  725. }
  726. }
  727. return ret;
  728. error:
  729. il4965_hw_txq_ctx_free(il);
  730. il4965_free_dma_ptr(il, &il->kw);
  731. error_kw:
  732. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  733. error_bc_tbls:
  734. return ret;
  735. }
  736. void il4965_txq_ctx_reset(struct il_priv *il)
  737. {
  738. int txq_id, slots_num;
  739. unsigned long flags;
  740. spin_lock_irqsave(&il->lock, flags);
  741. /* Turn off all Tx DMA fifos */
  742. il4965_txq_set_sched(il, 0);
  743. /* Tell NIC where to find the "keep warm" buffer */
  744. il_wr(il, FH_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  745. spin_unlock_irqrestore(&il->lock, flags);
  746. /* Alloc and init all Tx queues, including the command queue (#4) */
  747. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  748. slots_num = txq_id == il->cmd_queue ?
  749. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  750. il_tx_queue_reset(il, &il->txq[txq_id],
  751. slots_num, txq_id);
  752. }
  753. }
  754. /**
  755. * il4965_txq_ctx_stop - Stop all Tx DMA channels
  756. */
  757. void il4965_txq_ctx_stop(struct il_priv *il)
  758. {
  759. int ch, txq_id;
  760. unsigned long flags;
  761. /* Turn off all Tx DMA fifos */
  762. spin_lock_irqsave(&il->lock, flags);
  763. il4965_txq_set_sched(il, 0);
  764. /* Stop each Tx DMA channel, and wait for it to be idle */
  765. for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
  766. il_wr(il,
  767. FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  768. if (il_poll_bit(il, FH_TSSR_TX_STATUS_REG,
  769. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  770. 1000))
  771. IL_ERR("Failing on timeout while stopping"
  772. " DMA channel %d [0x%08x]", ch,
  773. il_rd(il,
  774. FH_TSSR_TX_STATUS_REG));
  775. }
  776. spin_unlock_irqrestore(&il->lock, flags);
  777. if (!il->txq)
  778. return;
  779. /* Unmap DMA from host system and free skb's */
  780. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  781. if (txq_id == il->cmd_queue)
  782. il_cmd_queue_unmap(il);
  783. else
  784. il_tx_queue_unmap(il, txq_id);
  785. }
  786. /*
  787. * Find first available (lowest unused) Tx Queue, mark it "active".
  788. * Called only when finding queue for aggregation.
  789. * Should never return anything < 7, because they should already
  790. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  791. */
  792. static int il4965_txq_ctx_activate_free(struct il_priv *il)
  793. {
  794. int txq_id;
  795. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  796. if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
  797. return txq_id;
  798. return -1;
  799. }
  800. /**
  801. * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  802. */
  803. static void il4965_tx_queue_stop_scheduler(struct il_priv *il,
  804. u16 txq_id)
  805. {
  806. /* Simply stop the queue, but don't change any configuration;
  807. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  808. il_wr_prph(il,
  809. IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  810. (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  811. (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  812. }
  813. /**
  814. * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  815. */
  816. static int il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid,
  817. u16 txq_id)
  818. {
  819. u32 tbl_dw_addr;
  820. u32 tbl_dw;
  821. u16 scd_q2ratid;
  822. scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  823. tbl_dw_addr = il->scd_base_addr +
  824. IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  825. tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
  826. if (txq_id & 0x1)
  827. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  828. else
  829. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  830. il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
  831. return 0;
  832. }
  833. /**
  834. * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  835. *
  836. * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
  837. * i.e. it must be one of the higher queues used for aggregation
  838. */
  839. static int il4965_txq_agg_enable(struct il_priv *il, int txq_id,
  840. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  841. {
  842. unsigned long flags;
  843. u16 ra_tid;
  844. int ret;
  845. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  846. (IL49_FIRST_AMPDU_QUEUE +
  847. il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  848. IL_WARN(
  849. "queue number out of range: %d, must be %d to %d\n",
  850. txq_id, IL49_FIRST_AMPDU_QUEUE,
  851. IL49_FIRST_AMPDU_QUEUE +
  852. il->cfg->base_params->num_of_ampdu_queues - 1);
  853. return -EINVAL;
  854. }
  855. ra_tid = BUILD_RAxTID(sta_id, tid);
  856. /* Modify device's station table to Tx this TID */
  857. ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
  858. if (ret)
  859. return ret;
  860. spin_lock_irqsave(&il->lock, flags);
  861. /* Stop this Tx queue before configuring it */
  862. il4965_tx_queue_stop_scheduler(il, txq_id);
  863. /* Map receiver-address / traffic-ID to this queue */
  864. il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
  865. /* Set this queue as a chain-building queue */
  866. il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  867. /* Place first TFD at idx corresponding to start sequence number.
  868. * Assumes that ssn_idx is valid (!= 0xFFF) */
  869. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  870. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  871. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  872. /* Set up Tx win size and frame limit for this queue */
  873. il_write_targ_mem(il,
  874. il->scd_base_addr + IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  875. (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  876. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  877. il_write_targ_mem(il, il->scd_base_addr +
  878. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  879. (SCD_FRAME_LIMIT << IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS)
  880. & IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  881. il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  882. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  883. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
  884. spin_unlock_irqrestore(&il->lock, flags);
  885. return 0;
  886. }
  887. int il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
  888. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  889. {
  890. int sta_id;
  891. int tx_fifo;
  892. int txq_id;
  893. int ret;
  894. unsigned long flags;
  895. struct il_tid_data *tid_data;
  896. tx_fifo = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
  897. if (unlikely(tx_fifo < 0))
  898. return tx_fifo;
  899. IL_WARN("%s on ra = %pM tid = %d\n",
  900. __func__, sta->addr, tid);
  901. sta_id = il_sta_id(sta);
  902. if (sta_id == IL_INVALID_STATION) {
  903. IL_ERR("Start AGG on invalid station\n");
  904. return -ENXIO;
  905. }
  906. if (unlikely(tid >= MAX_TID_COUNT))
  907. return -EINVAL;
  908. if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
  909. IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
  910. return -ENXIO;
  911. }
  912. txq_id = il4965_txq_ctx_activate_free(il);
  913. if (txq_id == -1) {
  914. IL_ERR("No free aggregation queue available\n");
  915. return -ENXIO;
  916. }
  917. spin_lock_irqsave(&il->sta_lock, flags);
  918. tid_data = &il->stations[sta_id].tid[tid];
  919. *ssn = SEQ_TO_SN(tid_data->seq_number);
  920. tid_data->agg.txq_id = txq_id;
  921. il_set_swq_id(&il->txq[txq_id],
  922. il4965_get_ac_from_tid(tid), txq_id);
  923. spin_unlock_irqrestore(&il->sta_lock, flags);
  924. ret = il4965_txq_agg_enable(il, txq_id, tx_fifo,
  925. sta_id, tid, *ssn);
  926. if (ret)
  927. return ret;
  928. spin_lock_irqsave(&il->sta_lock, flags);
  929. tid_data = &il->stations[sta_id].tid[tid];
  930. if (tid_data->tfds_in_queue == 0) {
  931. D_HT("HW queue is empty\n");
  932. tid_data->agg.state = IL_AGG_ON;
  933. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  934. } else {
  935. D_HT(
  936. "HW queue is NOT empty: %d packets in HW queue\n",
  937. tid_data->tfds_in_queue);
  938. tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
  939. }
  940. spin_unlock_irqrestore(&il->sta_lock, flags);
  941. return ret;
  942. }
  943. /**
  944. * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
  945. * il->lock must be held by the caller
  946. */
  947. static int il4965_txq_agg_disable(struct il_priv *il, u16 txq_id,
  948. u16 ssn_idx, u8 tx_fifo)
  949. {
  950. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  951. (IL49_FIRST_AMPDU_QUEUE +
  952. il->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  953. IL_WARN(
  954. "queue number out of range: %d, must be %d to %d\n",
  955. txq_id, IL49_FIRST_AMPDU_QUEUE,
  956. IL49_FIRST_AMPDU_QUEUE +
  957. il->cfg->base_params->num_of_ampdu_queues - 1);
  958. return -EINVAL;
  959. }
  960. il4965_tx_queue_stop_scheduler(il, txq_id);
  961. il_clear_bits_prph(il,
  962. IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  963. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  964. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  965. /* supposes that ssn_idx is valid (!= 0xFFF) */
  966. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  967. il_clear_bits_prph(il,
  968. IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  969. il_txq_ctx_deactivate(il, txq_id);
  970. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
  971. return 0;
  972. }
  973. int il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
  974. struct ieee80211_sta *sta, u16 tid)
  975. {
  976. int tx_fifo_id, txq_id, sta_id, ssn;
  977. struct il_tid_data *tid_data;
  978. int write_ptr, read_ptr;
  979. unsigned long flags;
  980. tx_fifo_id = il4965_get_fifo_from_tid(il_rxon_ctx_from_vif(vif), tid);
  981. if (unlikely(tx_fifo_id < 0))
  982. return tx_fifo_id;
  983. sta_id = il_sta_id(sta);
  984. if (sta_id == IL_INVALID_STATION) {
  985. IL_ERR("Invalid station for AGG tid %d\n", tid);
  986. return -ENXIO;
  987. }
  988. spin_lock_irqsave(&il->sta_lock, flags);
  989. tid_data = &il->stations[sta_id].tid[tid];
  990. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  991. txq_id = tid_data->agg.txq_id;
  992. switch (il->stations[sta_id].tid[tid].agg.state) {
  993. case IL_EMPTYING_HW_QUEUE_ADDBA:
  994. /*
  995. * This can happen if the peer stops aggregation
  996. * again before we've had a chance to drain the
  997. * queue we selected previously, i.e. before the
  998. * session was really started completely.
  999. */
  1000. D_HT("AGG stop before setup done\n");
  1001. goto turn_off;
  1002. case IL_AGG_ON:
  1003. break;
  1004. default:
  1005. IL_WARN("Stopping AGG while state not ON or starting\n");
  1006. }
  1007. write_ptr = il->txq[txq_id].q.write_ptr;
  1008. read_ptr = il->txq[txq_id].q.read_ptr;
  1009. /* The queue is not empty */
  1010. if (write_ptr != read_ptr) {
  1011. D_HT("Stopping a non empty AGG HW QUEUE\n");
  1012. il->stations[sta_id].tid[tid].agg.state =
  1013. IL_EMPTYING_HW_QUEUE_DELBA;
  1014. spin_unlock_irqrestore(&il->sta_lock, flags);
  1015. return 0;
  1016. }
  1017. D_HT("HW queue is empty\n");
  1018. turn_off:
  1019. il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
  1020. /* do not restore/save irqs */
  1021. spin_unlock(&il->sta_lock);
  1022. spin_lock(&il->lock);
  1023. /*
  1024. * the only reason this call can fail is queue number out of range,
  1025. * which can happen if uCode is reloaded and all the station
  1026. * information are lost. if it is outside the range, there is no need
  1027. * to deactivate the uCode queue, just return "success" to allow
  1028. * mac80211 to clean up it own data.
  1029. */
  1030. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
  1031. spin_unlock_irqrestore(&il->lock, flags);
  1032. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1033. return 0;
  1034. }
  1035. int il4965_txq_check_empty(struct il_priv *il,
  1036. int sta_id, u8 tid, int txq_id)
  1037. {
  1038. struct il_queue *q = &il->txq[txq_id].q;
  1039. u8 *addr = il->stations[sta_id].sta.sta.addr;
  1040. struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
  1041. struct il_rxon_context *ctx;
  1042. ctx = &il->ctx;
  1043. lockdep_assert_held(&il->sta_lock);
  1044. switch (il->stations[sta_id].tid[tid].agg.state) {
  1045. case IL_EMPTYING_HW_QUEUE_DELBA:
  1046. /* We are reclaiming the last packet of the */
  1047. /* aggregated HW queue */
  1048. if (txq_id == tid_data->agg.txq_id &&
  1049. q->read_ptr == q->write_ptr) {
  1050. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  1051. int tx_fifo = il4965_get_fifo_from_tid(ctx, tid);
  1052. D_HT(
  1053. "HW queue empty: continue DELBA flow\n");
  1054. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
  1055. tid_data->agg.state = IL_AGG_OFF;
  1056. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  1057. }
  1058. break;
  1059. case IL_EMPTYING_HW_QUEUE_ADDBA:
  1060. /* We are reclaiming the last packet of the queue */
  1061. if (tid_data->tfds_in_queue == 0) {
  1062. D_HT(
  1063. "HW queue empty: continue ADDBA flow\n");
  1064. tid_data->agg.state = IL_AGG_ON;
  1065. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  1066. }
  1067. break;
  1068. }
  1069. return 0;
  1070. }
  1071. static void il4965_non_agg_tx_status(struct il_priv *il,
  1072. struct il_rxon_context *ctx,
  1073. const u8 *addr1)
  1074. {
  1075. struct ieee80211_sta *sta;
  1076. struct il_station_priv *sta_priv;
  1077. rcu_read_lock();
  1078. sta = ieee80211_find_sta(ctx->vif, addr1);
  1079. if (sta) {
  1080. sta_priv = (void *)sta->drv_priv;
  1081. /* avoid atomic ops if this isn't a client */
  1082. if (sta_priv->client &&
  1083. atomic_dec_return(&sta_priv->pending_frames) == 0)
  1084. ieee80211_sta_block_awake(il->hw, sta, false);
  1085. }
  1086. rcu_read_unlock();
  1087. }
  1088. static void
  1089. il4965_tx_status(struct il_priv *il, struct il_tx_info *tx_info,
  1090. bool is_agg)
  1091. {
  1092. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  1093. if (!is_agg)
  1094. il4965_non_agg_tx_status(il, tx_info->ctx, hdr->addr1);
  1095. ieee80211_tx_status_irqsafe(il->hw, tx_info->skb);
  1096. }
  1097. int il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
  1098. {
  1099. struct il_tx_queue *txq = &il->txq[txq_id];
  1100. struct il_queue *q = &txq->q;
  1101. struct il_tx_info *tx_info;
  1102. int nfreed = 0;
  1103. struct ieee80211_hdr *hdr;
  1104. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  1105. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  1106. "is out of range [0-%d] %d %d.\n", txq_id,
  1107. idx, q->n_bd, q->write_ptr, q->read_ptr);
  1108. return 0;
  1109. }
  1110. for (idx = il_queue_inc_wrap(idx, q->n_bd);
  1111. q->read_ptr != idx;
  1112. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1113. tx_info = &txq->txb[txq->q.read_ptr];
  1114. if (WARN_ON_ONCE(tx_info->skb == NULL))
  1115. continue;
  1116. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  1117. if (ieee80211_is_data_qos(hdr->frame_control))
  1118. nfreed++;
  1119. il4965_tx_status(il, tx_info,
  1120. txq_id >= IL4965_FIRST_AMPDU_QUEUE);
  1121. tx_info->skb = NULL;
  1122. il->cfg->ops->lib->txq_free_tfd(il, txq);
  1123. }
  1124. return nfreed;
  1125. }
  1126. /**
  1127. * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  1128. *
  1129. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  1130. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  1131. */
  1132. static int il4965_tx_status_reply_compressed_ba(struct il_priv *il,
  1133. struct il_ht_agg *agg,
  1134. struct il_compressed_ba_resp *ba_resp)
  1135. {
  1136. int i, sh, ack;
  1137. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1138. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1139. int successes = 0;
  1140. struct ieee80211_tx_info *info;
  1141. u64 bitmap, sent_bitmap;
  1142. if (unlikely(!agg->wait_for_ba)) {
  1143. if (unlikely(ba_resp->bitmap))
  1144. IL_ERR("Received BA when not expected\n");
  1145. return -EINVAL;
  1146. }
  1147. /* Mark that the expected block-ack response arrived */
  1148. agg->wait_for_ba = 0;
  1149. D_TX_REPLY("BA %d %d\n", agg->start_idx,
  1150. ba_resp->seq_ctl);
  1151. /* Calculate shift to align block-ack bits with our Tx win bits */
  1152. sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
  1153. if (sh < 0) /* tbw something is wrong with indices */
  1154. sh += 0x100;
  1155. if (agg->frame_count > (64 - sh)) {
  1156. D_TX_REPLY("more frames than bitmap size");
  1157. return -1;
  1158. }
  1159. /* don't use 64-bit values for now */
  1160. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1161. /* check for success or failure according to the
  1162. * transmitted bitmap and block-ack bitmap */
  1163. sent_bitmap = bitmap & agg->bitmap;
  1164. /* For each frame attempted in aggregation,
  1165. * update driver's record of tx frame's status. */
  1166. i = 0;
  1167. while (sent_bitmap) {
  1168. ack = sent_bitmap & 1ULL;
  1169. successes += ack;
  1170. D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  1171. ack ? "ACK" : "NACK", i,
  1172. (agg->start_idx + i) & 0xff,
  1173. agg->start_idx + i);
  1174. sent_bitmap >>= 1;
  1175. ++i;
  1176. }
  1177. D_TX_REPLY("Bitmap %llx\n",
  1178. (unsigned long long)bitmap);
  1179. info = IEEE80211_SKB_CB(il->txq[scd_flow].txb[agg->start_idx].skb);
  1180. memset(&info->status, 0, sizeof(info->status));
  1181. info->flags |= IEEE80211_TX_STAT_ACK;
  1182. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1183. info->status.ampdu_ack_len = successes;
  1184. info->status.ampdu_len = agg->frame_count;
  1185. il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
  1186. return 0;
  1187. }
  1188. /**
  1189. * translate ucode response to mac80211 tx status control values
  1190. */
  1191. void il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
  1192. struct ieee80211_tx_info *info)
  1193. {
  1194. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1195. info->antenna_sel_tx =
  1196. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1197. if (rate_n_flags & RATE_MCS_HT_MSK)
  1198. r->flags |= IEEE80211_TX_RC_MCS;
  1199. if (rate_n_flags & RATE_MCS_GF_MSK)
  1200. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1201. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1202. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1203. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1204. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1205. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1206. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1207. r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1208. }
  1209. /**
  1210. * il4965_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1211. *
  1212. * Handles block-acknowledge notification from device, which reports success
  1213. * of frames sent via aggregation.
  1214. */
  1215. void il4965_rx_reply_compressed_ba(struct il_priv *il,
  1216. struct il_rx_buf *rxb)
  1217. {
  1218. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1219. struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1220. struct il_tx_queue *txq = NULL;
  1221. struct il_ht_agg *agg;
  1222. int idx;
  1223. int sta_id;
  1224. int tid;
  1225. unsigned long flags;
  1226. /* "flow" corresponds to Tx queue */
  1227. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1228. /* "ssn" is start of block-ack Tx win, corresponds to idx
  1229. * (in Tx queue's circular buffer) of first TFD/frame in win */
  1230. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1231. if (scd_flow >= il->hw_params.max_txq_num) {
  1232. IL_ERR(
  1233. "BUG_ON scd_flow is bigger than number of queues\n");
  1234. return;
  1235. }
  1236. txq = &il->txq[scd_flow];
  1237. sta_id = ba_resp->sta_id;
  1238. tid = ba_resp->tid;
  1239. agg = &il->stations[sta_id].tid[tid].agg;
  1240. if (unlikely(agg->txq_id != scd_flow)) {
  1241. /*
  1242. * FIXME: this is a uCode bug which need to be addressed,
  1243. * log the information and return for now!
  1244. * since it is possible happen very often and in order
  1245. * not to fill the syslog, don't enable the logging by default
  1246. */
  1247. D_TX_REPLY(
  1248. "BA scd_flow %d does not match txq_id %d\n",
  1249. scd_flow, agg->txq_id);
  1250. return;
  1251. }
  1252. /* Find idx just before block-ack win */
  1253. idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1254. spin_lock_irqsave(&il->sta_lock, flags);
  1255. D_TX_REPLY("REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1256. "sta_id = %d\n",
  1257. agg->wait_for_ba,
  1258. (u8 *) &ba_resp->sta_addr_lo32,
  1259. ba_resp->sta_id);
  1260. D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx,"
  1261. "scd_flow = "
  1262. "%d, scd_ssn = %d\n",
  1263. ba_resp->tid,
  1264. ba_resp->seq_ctl,
  1265. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1266. ba_resp->scd_flow,
  1267. ba_resp->scd_ssn);
  1268. D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n",
  1269. agg->start_idx,
  1270. (unsigned long long)agg->bitmap);
  1271. /* Update driver's record of ACK vs. not for each frame in win */
  1272. il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
  1273. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1274. * block-ack win (we assume that they've been successfully
  1275. * transmitted ... if not, it's too late anyway). */
  1276. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1277. /* calculate mac80211 ampdu sw queue to wake */
  1278. int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
  1279. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  1280. if (il_queue_space(&txq->q) > txq->q.low_mark &&
  1281. il->mac80211_registered &&
  1282. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  1283. il_wake_queue(il, txq);
  1284. il4965_txq_check_empty(il, sta_id, tid, scd_flow);
  1285. }
  1286. spin_unlock_irqrestore(&il->sta_lock, flags);
  1287. }
  1288. #ifdef CONFIG_IWLEGACY_DEBUG
  1289. const char *il4965_get_tx_fail_reason(u32 status)
  1290. {
  1291. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  1292. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  1293. switch (status & TX_STATUS_MSK) {
  1294. case TX_STATUS_SUCCESS:
  1295. return "SUCCESS";
  1296. TX_STATUS_POSTPONE(DELAY);
  1297. TX_STATUS_POSTPONE(FEW_BYTES);
  1298. TX_STATUS_POSTPONE(QUIET_PERIOD);
  1299. TX_STATUS_POSTPONE(CALC_TTAK);
  1300. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  1301. TX_STATUS_FAIL(SHORT_LIMIT);
  1302. TX_STATUS_FAIL(LONG_LIMIT);
  1303. TX_STATUS_FAIL(FIFO_UNDERRUN);
  1304. TX_STATUS_FAIL(DRAIN_FLOW);
  1305. TX_STATUS_FAIL(RFKILL_FLUSH);
  1306. TX_STATUS_FAIL(LIFE_EXPIRE);
  1307. TX_STATUS_FAIL(DEST_PS);
  1308. TX_STATUS_FAIL(HOST_ABORTED);
  1309. TX_STATUS_FAIL(BT_RETRY);
  1310. TX_STATUS_FAIL(STA_INVALID);
  1311. TX_STATUS_FAIL(FRAG_DROPPED);
  1312. TX_STATUS_FAIL(TID_DISABLE);
  1313. TX_STATUS_FAIL(FIFO_FLUSHED);
  1314. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  1315. TX_STATUS_FAIL(PASSIVE_NO_RX);
  1316. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  1317. }
  1318. return "UNKNOWN";
  1319. #undef TX_STATUS_FAIL
  1320. #undef TX_STATUS_POSTPONE
  1321. }
  1322. #endif /* CONFIG_IWLEGACY_DEBUG */
  1323. static struct il_link_quality_cmd *
  1324. il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
  1325. {
  1326. int i, r;
  1327. struct il_link_quality_cmd *link_cmd;
  1328. u32 rate_flags = 0;
  1329. __le32 rate_n_flags;
  1330. link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
  1331. if (!link_cmd) {
  1332. IL_ERR("Unable to allocate memory for LQ cmd.\n");
  1333. return NULL;
  1334. }
  1335. /* Set up the rate scaling to start at selected rate, fall back
  1336. * all the way down to 1M in IEEE order, and then spin on 1M */
  1337. if (il->band == IEEE80211_BAND_5GHZ)
  1338. r = RATE_6M_IDX;
  1339. else
  1340. r = RATE_1M_IDX;
  1341. if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
  1342. rate_flags |= RATE_MCS_CCK_MSK;
  1343. rate_flags |= il4965_first_antenna(il->hw_params.valid_tx_ant) <<
  1344. RATE_MCS_ANT_POS;
  1345. rate_n_flags = il4965_hw_set_rate_n_flags(il_rates[r].plcp,
  1346. rate_flags);
  1347. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1348. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  1349. link_cmd->general_params.single_stream_ant_msk =
  1350. il4965_first_antenna(il->hw_params.valid_tx_ant);
  1351. link_cmd->general_params.dual_stream_ant_msk =
  1352. il->hw_params.valid_tx_ant &
  1353. ~il4965_first_antenna(il->hw_params.valid_tx_ant);
  1354. if (!link_cmd->general_params.dual_stream_ant_msk) {
  1355. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  1356. } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
  1357. link_cmd->general_params.dual_stream_ant_msk =
  1358. il->hw_params.valid_tx_ant;
  1359. }
  1360. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  1361. link_cmd->agg_params.agg_time_limit =
  1362. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  1363. link_cmd->sta_id = sta_id;
  1364. return link_cmd;
  1365. }
  1366. /*
  1367. * il4965_add_bssid_station - Add the special IBSS BSSID station
  1368. *
  1369. * Function sleeps.
  1370. */
  1371. int
  1372. il4965_add_bssid_station(struct il_priv *il, struct il_rxon_context *ctx,
  1373. const u8 *addr, u8 *sta_id_r)
  1374. {
  1375. int ret;
  1376. u8 sta_id;
  1377. struct il_link_quality_cmd *link_cmd;
  1378. unsigned long flags;
  1379. if (sta_id_r)
  1380. *sta_id_r = IL_INVALID_STATION;
  1381. ret = il_add_station_common(il, ctx, addr, 0, NULL, &sta_id);
  1382. if (ret) {
  1383. IL_ERR("Unable to add station %pM\n", addr);
  1384. return ret;
  1385. }
  1386. if (sta_id_r)
  1387. *sta_id_r = sta_id;
  1388. spin_lock_irqsave(&il->sta_lock, flags);
  1389. il->stations[sta_id].used |= IL_STA_LOCAL;
  1390. spin_unlock_irqrestore(&il->sta_lock, flags);
  1391. /* Set up default rate scaling table in device's station table */
  1392. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  1393. if (!link_cmd) {
  1394. IL_ERR(
  1395. "Unable to initialize rate scaling for station %pM.\n",
  1396. addr);
  1397. return -ENOMEM;
  1398. }
  1399. ret = il_send_lq_cmd(il, ctx, link_cmd, CMD_SYNC, true);
  1400. if (ret)
  1401. IL_ERR("Link quality command failed (%d)\n", ret);
  1402. spin_lock_irqsave(&il->sta_lock, flags);
  1403. il->stations[sta_id].lq = link_cmd;
  1404. spin_unlock_irqrestore(&il->sta_lock, flags);
  1405. return 0;
  1406. }
  1407. static int il4965_static_wepkey_cmd(struct il_priv *il,
  1408. struct il_rxon_context *ctx,
  1409. bool send_if_empty)
  1410. {
  1411. int i, not_empty = 0;
  1412. u8 buff[sizeof(struct il_wep_cmd) +
  1413. sizeof(struct il_wep_key) * WEP_KEYS_MAX];
  1414. struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
  1415. size_t cmd_size = sizeof(struct il_wep_cmd);
  1416. struct il_host_cmd cmd = {
  1417. .id = ctx->wep_key_cmd,
  1418. .data = wep_cmd,
  1419. .flags = CMD_SYNC,
  1420. };
  1421. might_sleep();
  1422. memset(wep_cmd, 0, cmd_size +
  1423. (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
  1424. for (i = 0; i < WEP_KEYS_MAX ; i++) {
  1425. wep_cmd->key[i].key_idx = i;
  1426. if (ctx->wep_keys[i].key_size) {
  1427. wep_cmd->key[i].key_offset = i;
  1428. not_empty = 1;
  1429. } else {
  1430. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  1431. }
  1432. wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
  1433. memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
  1434. ctx->wep_keys[i].key_size);
  1435. }
  1436. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  1437. wep_cmd->num_keys = WEP_KEYS_MAX;
  1438. cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
  1439. cmd.len = cmd_size;
  1440. if (not_empty || send_if_empty)
  1441. return il_send_cmd(il, &cmd);
  1442. else
  1443. return 0;
  1444. }
  1445. int il4965_restore_default_wep_keys(struct il_priv *il,
  1446. struct il_rxon_context *ctx)
  1447. {
  1448. lockdep_assert_held(&il->mutex);
  1449. return il4965_static_wepkey_cmd(il, ctx, false);
  1450. }
  1451. int il4965_remove_default_wep_key(struct il_priv *il,
  1452. struct il_rxon_context *ctx,
  1453. struct ieee80211_key_conf *keyconf)
  1454. {
  1455. int ret;
  1456. lockdep_assert_held(&il->mutex);
  1457. D_WEP("Removing default WEP key: idx=%d\n",
  1458. keyconf->keyidx);
  1459. memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
  1460. if (il_is_rfkill(il)) {
  1461. D_WEP(
  1462. "Not sending REPLY_WEPKEY command due to RFKILL.\n");
  1463. /* but keys in device are clear anyway so return success */
  1464. return 0;
  1465. }
  1466. ret = il4965_static_wepkey_cmd(il, ctx, 1);
  1467. D_WEP("Remove default WEP key: idx=%d ret=%d\n",
  1468. keyconf->keyidx, ret);
  1469. return ret;
  1470. }
  1471. int il4965_set_default_wep_key(struct il_priv *il,
  1472. struct il_rxon_context *ctx,
  1473. struct ieee80211_key_conf *keyconf)
  1474. {
  1475. int ret;
  1476. lockdep_assert_held(&il->mutex);
  1477. if (keyconf->keylen != WEP_KEY_LEN_128 &&
  1478. keyconf->keylen != WEP_KEY_LEN_64) {
  1479. D_WEP("Bad WEP key length %d\n", keyconf->keylen);
  1480. return -EINVAL;
  1481. }
  1482. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  1483. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  1484. il->stations[ctx->ap_sta_id].keyinfo.cipher = keyconf->cipher;
  1485. ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
  1486. memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
  1487. keyconf->keylen);
  1488. ret = il4965_static_wepkey_cmd(il, ctx, false);
  1489. D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n",
  1490. keyconf->keylen, keyconf->keyidx, ret);
  1491. return ret;
  1492. }
  1493. static int il4965_set_wep_dynamic_key_info(struct il_priv *il,
  1494. struct il_rxon_context *ctx,
  1495. struct ieee80211_key_conf *keyconf,
  1496. u8 sta_id)
  1497. {
  1498. unsigned long flags;
  1499. __le16 key_flags = 0;
  1500. struct il_addsta_cmd sta_cmd;
  1501. lockdep_assert_held(&il->mutex);
  1502. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  1503. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  1504. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1505. key_flags &= ~STA_KEY_FLG_INVALID;
  1506. if (keyconf->keylen == WEP_KEY_LEN_128)
  1507. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  1508. if (sta_id == ctx->bcast_sta_id)
  1509. key_flags |= STA_KEY_MULTICAST_MSK;
  1510. spin_lock_irqsave(&il->sta_lock, flags);
  1511. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  1512. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  1513. il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  1514. memcpy(il->stations[sta_id].keyinfo.key,
  1515. keyconf->key, keyconf->keylen);
  1516. memcpy(&il->stations[sta_id].sta.key.key[3],
  1517. keyconf->key, keyconf->keylen);
  1518. if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  1519. == STA_KEY_FLG_NO_ENC)
  1520. il->stations[sta_id].sta.key.key_offset =
  1521. il_get_free_ucode_key_idx(il);
  1522. /* else, we are overriding an existing key => no need to allocated room
  1523. * in uCode. */
  1524. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  1525. "no space for a new key");
  1526. il->stations[sta_id].sta.key.key_flags = key_flags;
  1527. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1528. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1529. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1530. sizeof(struct il_addsta_cmd));
  1531. spin_unlock_irqrestore(&il->sta_lock, flags);
  1532. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1533. }
  1534. static int il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
  1535. struct il_rxon_context *ctx,
  1536. struct ieee80211_key_conf *keyconf,
  1537. u8 sta_id)
  1538. {
  1539. unsigned long flags;
  1540. __le16 key_flags = 0;
  1541. struct il_addsta_cmd sta_cmd;
  1542. lockdep_assert_held(&il->mutex);
  1543. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  1544. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1545. key_flags &= ~STA_KEY_FLG_INVALID;
  1546. if (sta_id == ctx->bcast_sta_id)
  1547. key_flags |= STA_KEY_MULTICAST_MSK;
  1548. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1549. spin_lock_irqsave(&il->sta_lock, flags);
  1550. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  1551. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  1552. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key,
  1553. keyconf->keylen);
  1554. memcpy(il->stations[sta_id].sta.key.key, keyconf->key,
  1555. keyconf->keylen);
  1556. if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  1557. == STA_KEY_FLG_NO_ENC)
  1558. il->stations[sta_id].sta.key.key_offset =
  1559. il_get_free_ucode_key_idx(il);
  1560. /* else, we are overriding an existing key => no need to allocated room
  1561. * in uCode. */
  1562. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  1563. "no space for a new key");
  1564. il->stations[sta_id].sta.key.key_flags = key_flags;
  1565. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1566. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1567. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1568. sizeof(struct il_addsta_cmd));
  1569. spin_unlock_irqrestore(&il->sta_lock, flags);
  1570. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1571. }
  1572. static int il4965_set_tkip_dynamic_key_info(struct il_priv *il,
  1573. struct il_rxon_context *ctx,
  1574. struct ieee80211_key_conf *keyconf,
  1575. u8 sta_id)
  1576. {
  1577. unsigned long flags;
  1578. int ret = 0;
  1579. __le16 key_flags = 0;
  1580. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  1581. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1582. key_flags &= ~STA_KEY_FLG_INVALID;
  1583. if (sta_id == ctx->bcast_sta_id)
  1584. key_flags |= STA_KEY_MULTICAST_MSK;
  1585. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  1586. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  1587. spin_lock_irqsave(&il->sta_lock, flags);
  1588. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  1589. il->stations[sta_id].keyinfo.keylen = 16;
  1590. if ((il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  1591. == STA_KEY_FLG_NO_ENC)
  1592. il->stations[sta_id].sta.key.key_offset =
  1593. il_get_free_ucode_key_idx(il);
  1594. /* else, we are overriding an existing key => no need to allocated room
  1595. * in uCode. */
  1596. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  1597. "no space for a new key");
  1598. il->stations[sta_id].sta.key.key_flags = key_flags;
  1599. /* This copy is acutally not needed: we get the key with each TX */
  1600. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
  1601. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
  1602. spin_unlock_irqrestore(&il->sta_lock, flags);
  1603. return ret;
  1604. }
  1605. void il4965_update_tkip_key(struct il_priv *il,
  1606. struct il_rxon_context *ctx,
  1607. struct ieee80211_key_conf *keyconf,
  1608. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  1609. {
  1610. u8 sta_id;
  1611. unsigned long flags;
  1612. int i;
  1613. if (il_scan_cancel(il)) {
  1614. /* cancel scan failed, just live w/ bad key and rely
  1615. briefly on SW decryption */
  1616. return;
  1617. }
  1618. sta_id = il_sta_id_or_broadcast(il, ctx, sta);
  1619. if (sta_id == IL_INVALID_STATION)
  1620. return;
  1621. spin_lock_irqsave(&il->sta_lock, flags);
  1622. il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  1623. for (i = 0; i < 5; i++)
  1624. il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  1625. cpu_to_le16(phase1key[i]);
  1626. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1627. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1628. il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  1629. spin_unlock_irqrestore(&il->sta_lock, flags);
  1630. }
  1631. int il4965_remove_dynamic_key(struct il_priv *il,
  1632. struct il_rxon_context *ctx,
  1633. struct ieee80211_key_conf *keyconf,
  1634. u8 sta_id)
  1635. {
  1636. unsigned long flags;
  1637. u16 key_flags;
  1638. u8 keyidx;
  1639. struct il_addsta_cmd sta_cmd;
  1640. lockdep_assert_held(&il->mutex);
  1641. ctx->key_mapping_keys--;
  1642. spin_lock_irqsave(&il->sta_lock, flags);
  1643. key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
  1644. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  1645. D_WEP("Remove dynamic key: idx=%d sta=%d\n",
  1646. keyconf->keyidx, sta_id);
  1647. if (keyconf->keyidx != keyidx) {
  1648. /* We need to remove a key with idx different that the one
  1649. * in the uCode. This means that the key we need to remove has
  1650. * been replaced by another one with different idx.
  1651. * Don't do anything and return ok
  1652. */
  1653. spin_unlock_irqrestore(&il->sta_lock, flags);
  1654. return 0;
  1655. }
  1656. if (il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
  1657. IL_WARN("Removing wrong key %d 0x%x\n",
  1658. keyconf->keyidx, key_flags);
  1659. spin_unlock_irqrestore(&il->sta_lock, flags);
  1660. return 0;
  1661. }
  1662. if (!test_and_clear_bit(il->stations[sta_id].sta.key.key_offset,
  1663. &il->ucode_key_table))
  1664. IL_ERR("idx %d not used in uCode key table.\n",
  1665. il->stations[sta_id].sta.key.key_offset);
  1666. memset(&il->stations[sta_id].keyinfo, 0,
  1667. sizeof(struct il_hw_key));
  1668. memset(&il->stations[sta_id].sta.key, 0,
  1669. sizeof(struct il4965_keyinfo));
  1670. il->stations[sta_id].sta.key.key_flags =
  1671. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  1672. il->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
  1673. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1674. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1675. if (il_is_rfkill(il)) {
  1676. D_WEP(
  1677. "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
  1678. spin_unlock_irqrestore(&il->sta_lock, flags);
  1679. return 0;
  1680. }
  1681. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1682. sizeof(struct il_addsta_cmd));
  1683. spin_unlock_irqrestore(&il->sta_lock, flags);
  1684. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1685. }
  1686. int il4965_set_dynamic_key(struct il_priv *il, struct il_rxon_context *ctx,
  1687. struct ieee80211_key_conf *keyconf, u8 sta_id)
  1688. {
  1689. int ret;
  1690. lockdep_assert_held(&il->mutex);
  1691. ctx->key_mapping_keys++;
  1692. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  1693. switch (keyconf->cipher) {
  1694. case WLAN_CIPHER_SUITE_CCMP:
  1695. ret = il4965_set_ccmp_dynamic_key_info(il, ctx,
  1696. keyconf, sta_id);
  1697. break;
  1698. case WLAN_CIPHER_SUITE_TKIP:
  1699. ret = il4965_set_tkip_dynamic_key_info(il, ctx,
  1700. keyconf, sta_id);
  1701. break;
  1702. case WLAN_CIPHER_SUITE_WEP40:
  1703. case WLAN_CIPHER_SUITE_WEP104:
  1704. ret = il4965_set_wep_dynamic_key_info(il, ctx,
  1705. keyconf, sta_id);
  1706. break;
  1707. default:
  1708. IL_ERR(
  1709. "Unknown alg: %s cipher = %x\n", __func__,
  1710. keyconf->cipher);
  1711. ret = -EINVAL;
  1712. }
  1713. D_WEP(
  1714. "Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  1715. keyconf->cipher, keyconf->keylen, keyconf->keyidx,
  1716. sta_id, ret);
  1717. return ret;
  1718. }
  1719. /**
  1720. * il4965_alloc_bcast_station - add broadcast station into driver's station table.
  1721. *
  1722. * This adds the broadcast station into the driver's station table
  1723. * and marks it driver active, so that it will be restored to the
  1724. * device at the next best time.
  1725. */
  1726. int il4965_alloc_bcast_station(struct il_priv *il,
  1727. struct il_rxon_context *ctx)
  1728. {
  1729. struct il_link_quality_cmd *link_cmd;
  1730. unsigned long flags;
  1731. u8 sta_id;
  1732. spin_lock_irqsave(&il->sta_lock, flags);
  1733. sta_id = il_prep_station(il, ctx, il_bcast_addr,
  1734. false, NULL);
  1735. if (sta_id == IL_INVALID_STATION) {
  1736. IL_ERR("Unable to prepare broadcast station\n");
  1737. spin_unlock_irqrestore(&il->sta_lock, flags);
  1738. return -EINVAL;
  1739. }
  1740. il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
  1741. il->stations[sta_id].used |= IL_STA_BCAST;
  1742. spin_unlock_irqrestore(&il->sta_lock, flags);
  1743. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  1744. if (!link_cmd) {
  1745. IL_ERR(
  1746. "Unable to initialize rate scaling for bcast station.\n");
  1747. return -ENOMEM;
  1748. }
  1749. spin_lock_irqsave(&il->sta_lock, flags);
  1750. il->stations[sta_id].lq = link_cmd;
  1751. spin_unlock_irqrestore(&il->sta_lock, flags);
  1752. return 0;
  1753. }
  1754. /**
  1755. * il4965_update_bcast_station - update broadcast station's LQ command
  1756. *
  1757. * Only used by iwl4965. Placed here to have all bcast station management
  1758. * code together.
  1759. */
  1760. static int il4965_update_bcast_station(struct il_priv *il,
  1761. struct il_rxon_context *ctx)
  1762. {
  1763. unsigned long flags;
  1764. struct il_link_quality_cmd *link_cmd;
  1765. u8 sta_id = ctx->bcast_sta_id;
  1766. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  1767. if (!link_cmd) {
  1768. IL_ERR(
  1769. "Unable to initialize rate scaling for bcast station.\n");
  1770. return -ENOMEM;
  1771. }
  1772. spin_lock_irqsave(&il->sta_lock, flags);
  1773. if (il->stations[sta_id].lq)
  1774. kfree(il->stations[sta_id].lq);
  1775. else
  1776. D_INFO(
  1777. "Bcast station rate scaling has not been initialized yet.\n");
  1778. il->stations[sta_id].lq = link_cmd;
  1779. spin_unlock_irqrestore(&il->sta_lock, flags);
  1780. return 0;
  1781. }
  1782. int il4965_update_bcast_stations(struct il_priv *il)
  1783. {
  1784. return il4965_update_bcast_station(il, &il->ctx);
  1785. }
  1786. /**
  1787. * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  1788. */
  1789. int il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
  1790. {
  1791. unsigned long flags;
  1792. struct il_addsta_cmd sta_cmd;
  1793. lockdep_assert_held(&il->mutex);
  1794. /* Remove "disable" flag, to enable Tx for this TID */
  1795. spin_lock_irqsave(&il->sta_lock, flags);
  1796. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  1797. il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  1798. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1799. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1800. sizeof(struct il_addsta_cmd));
  1801. spin_unlock_irqrestore(&il->sta_lock, flags);
  1802. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1803. }
  1804. int il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta,
  1805. int tid, u16 ssn)
  1806. {
  1807. unsigned long flags;
  1808. int sta_id;
  1809. struct il_addsta_cmd sta_cmd;
  1810. lockdep_assert_held(&il->mutex);
  1811. sta_id = il_sta_id(sta);
  1812. if (sta_id == IL_INVALID_STATION)
  1813. return -ENXIO;
  1814. spin_lock_irqsave(&il->sta_lock, flags);
  1815. il->stations[sta_id].sta.station_flags_msk = 0;
  1816. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  1817. il->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
  1818. il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  1819. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1820. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1821. sizeof(struct il_addsta_cmd));
  1822. spin_unlock_irqrestore(&il->sta_lock, flags);
  1823. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1824. }
  1825. int il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta,
  1826. int tid)
  1827. {
  1828. unsigned long flags;
  1829. int sta_id;
  1830. struct il_addsta_cmd sta_cmd;
  1831. lockdep_assert_held(&il->mutex);
  1832. sta_id = il_sta_id(sta);
  1833. if (sta_id == IL_INVALID_STATION) {
  1834. IL_ERR("Invalid station for AGG tid %d\n", tid);
  1835. return -ENXIO;
  1836. }
  1837. spin_lock_irqsave(&il->sta_lock, flags);
  1838. il->stations[sta_id].sta.station_flags_msk = 0;
  1839. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  1840. il->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
  1841. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1842. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  1843. sizeof(struct il_addsta_cmd));
  1844. spin_unlock_irqrestore(&il->sta_lock, flags);
  1845. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  1846. }
  1847. void
  1848. il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
  1849. {
  1850. unsigned long flags;
  1851. spin_lock_irqsave(&il->sta_lock, flags);
  1852. il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  1853. il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  1854. il->stations[sta_id].sta.sta.modify_mask =
  1855. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  1856. il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  1857. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1858. il_send_add_sta(il,
  1859. &il->stations[sta_id].sta, CMD_ASYNC);
  1860. spin_unlock_irqrestore(&il->sta_lock, flags);
  1861. }
  1862. void il4965_update_chain_flags(struct il_priv *il)
  1863. {
  1864. if (il->cfg->ops->hcmd->set_rxon_chain) {
  1865. il->cfg->ops->hcmd->set_rxon_chain(il, &il->ctx);
  1866. if (il->ctx.active.rx_chain != il->ctx.staging.rx_chain)
  1867. il_commit_rxon(il, &il->ctx);
  1868. }
  1869. }
  1870. static void il4965_clear_free_frames(struct il_priv *il)
  1871. {
  1872. struct list_head *element;
  1873. D_INFO("%d frames on pre-allocated heap on clear.\n",
  1874. il->frames_count);
  1875. while (!list_empty(&il->free_frames)) {
  1876. element = il->free_frames.next;
  1877. list_del(element);
  1878. kfree(list_entry(element, struct il_frame, list));
  1879. il->frames_count--;
  1880. }
  1881. if (il->frames_count) {
  1882. IL_WARN("%d frames still in use. Did we lose one?\n",
  1883. il->frames_count);
  1884. il->frames_count = 0;
  1885. }
  1886. }
  1887. static struct il_frame *il4965_get_free_frame(struct il_priv *il)
  1888. {
  1889. struct il_frame *frame;
  1890. struct list_head *element;
  1891. if (list_empty(&il->free_frames)) {
  1892. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  1893. if (!frame) {
  1894. IL_ERR("Could not allocate frame!\n");
  1895. return NULL;
  1896. }
  1897. il->frames_count++;
  1898. return frame;
  1899. }
  1900. element = il->free_frames.next;
  1901. list_del(element);
  1902. return list_entry(element, struct il_frame, list);
  1903. }
  1904. static void il4965_free_frame(struct il_priv *il, struct il_frame *frame)
  1905. {
  1906. memset(frame, 0, sizeof(*frame));
  1907. list_add(&frame->list, &il->free_frames);
  1908. }
  1909. static u32 il4965_fill_beacon_frame(struct il_priv *il,
  1910. struct ieee80211_hdr *hdr,
  1911. int left)
  1912. {
  1913. lockdep_assert_held(&il->mutex);
  1914. if (!il->beacon_skb)
  1915. return 0;
  1916. if (il->beacon_skb->len > left)
  1917. return 0;
  1918. memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
  1919. return il->beacon_skb->len;
  1920. }
  1921. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  1922. static void il4965_set_beacon_tim(struct il_priv *il,
  1923. struct il_tx_beacon_cmd *tx_beacon_cmd,
  1924. u8 *beacon, u32 frame_size)
  1925. {
  1926. u16 tim_idx;
  1927. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  1928. /*
  1929. * The idx is relative to frame start but we start looking at the
  1930. * variable-length part of the beacon.
  1931. */
  1932. tim_idx = mgmt->u.beacon.variable - beacon;
  1933. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  1934. while ((tim_idx < (frame_size - 2)) &&
  1935. (beacon[tim_idx] != WLAN_EID_TIM))
  1936. tim_idx += beacon[tim_idx+1] + 2;
  1937. /* If TIM field was found, set variables */
  1938. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  1939. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  1940. tx_beacon_cmd->tim_size = beacon[tim_idx+1];
  1941. } else
  1942. IL_WARN("Unable to find TIM Element in beacon\n");
  1943. }
  1944. static unsigned int il4965_hw_get_beacon_cmd(struct il_priv *il,
  1945. struct il_frame *frame)
  1946. {
  1947. struct il_tx_beacon_cmd *tx_beacon_cmd;
  1948. u32 frame_size;
  1949. u32 rate_flags;
  1950. u32 rate;
  1951. /*
  1952. * We have to set up the TX command, the TX Beacon command, and the
  1953. * beacon contents.
  1954. */
  1955. lockdep_assert_held(&il->mutex);
  1956. if (!il->beacon_ctx) {
  1957. IL_ERR("trying to build beacon w/o beacon context!\n");
  1958. return 0;
  1959. }
  1960. /* Initialize memory */
  1961. tx_beacon_cmd = &frame->u.beacon;
  1962. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  1963. /* Set up TX beacon contents */
  1964. frame_size = il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
  1965. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  1966. if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
  1967. return 0;
  1968. if (!frame_size)
  1969. return 0;
  1970. /* Set up TX command fields */
  1971. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  1972. tx_beacon_cmd->tx.sta_id = il->beacon_ctx->bcast_sta_id;
  1973. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1974. tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
  1975. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
  1976. /* Set up TX beacon command fields */
  1977. il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
  1978. frame_size);
  1979. /* Set up packet rate and flags */
  1980. rate = il_get_lowest_plcp(il, il->beacon_ctx);
  1981. il->mgmt_tx_ant = il4965_toggle_tx_ant(il, il->mgmt_tx_ant,
  1982. il->hw_params.valid_tx_ant);
  1983. rate_flags = il4965_ant_idx_to_flags(il->mgmt_tx_ant);
  1984. if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
  1985. rate_flags |= RATE_MCS_CCK_MSK;
  1986. tx_beacon_cmd->tx.rate_n_flags = il4965_hw_set_rate_n_flags(rate,
  1987. rate_flags);
  1988. return sizeof(*tx_beacon_cmd) + frame_size;
  1989. }
  1990. int il4965_send_beacon_cmd(struct il_priv *il)
  1991. {
  1992. struct il_frame *frame;
  1993. unsigned int frame_size;
  1994. int rc;
  1995. frame = il4965_get_free_frame(il);
  1996. if (!frame) {
  1997. IL_ERR("Could not obtain free frame buffer for beacon "
  1998. "command.\n");
  1999. return -ENOMEM;
  2000. }
  2001. frame_size = il4965_hw_get_beacon_cmd(il, frame);
  2002. if (!frame_size) {
  2003. IL_ERR("Error configuring the beacon command\n");
  2004. il4965_free_frame(il, frame);
  2005. return -EINVAL;
  2006. }
  2007. rc = il_send_cmd_pdu(il, REPLY_TX_BEACON, frame_size,
  2008. &frame->u.cmd[0]);
  2009. il4965_free_frame(il, frame);
  2010. return rc;
  2011. }
  2012. static inline dma_addr_t il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
  2013. {
  2014. struct il_tfd_tb *tb = &tfd->tbs[idx];
  2015. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  2016. if (sizeof(dma_addr_t) > sizeof(u32))
  2017. addr |=
  2018. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  2019. return addr;
  2020. }
  2021. static inline u16 il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
  2022. {
  2023. struct il_tfd_tb *tb = &tfd->tbs[idx];
  2024. return le16_to_cpu(tb->hi_n_len) >> 4;
  2025. }
  2026. static inline void il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx,
  2027. dma_addr_t addr, u16 len)
  2028. {
  2029. struct il_tfd_tb *tb = &tfd->tbs[idx];
  2030. u16 hi_n_len = len << 4;
  2031. put_unaligned_le32(addr, &tb->lo);
  2032. if (sizeof(dma_addr_t) > sizeof(u32))
  2033. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  2034. tb->hi_n_len = cpu_to_le16(hi_n_len);
  2035. tfd->num_tbs = idx + 1;
  2036. }
  2037. static inline u8 il4965_tfd_get_num_tbs(struct il_tfd *tfd)
  2038. {
  2039. return tfd->num_tbs & 0x1f;
  2040. }
  2041. /**
  2042. * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  2043. * @il - driver ilate data
  2044. * @txq - tx queue
  2045. *
  2046. * Does NOT advance any TFD circular buffer read/write idxes
  2047. * Does NOT free the TFD itself (which is within circular buffer)
  2048. */
  2049. void il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
  2050. {
  2051. struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
  2052. struct il_tfd *tfd;
  2053. struct pci_dev *dev = il->pci_dev;
  2054. int idx = txq->q.read_ptr;
  2055. int i;
  2056. int num_tbs;
  2057. tfd = &tfd_tmp[idx];
  2058. /* Sanity check on number of chunks */
  2059. num_tbs = il4965_tfd_get_num_tbs(tfd);
  2060. if (num_tbs >= IL_NUM_OF_TBS) {
  2061. IL_ERR("Too many chunks: %i\n", num_tbs);
  2062. /* @todo issue fatal error, it is quite serious situation */
  2063. return;
  2064. }
  2065. /* Unmap tx_cmd */
  2066. if (num_tbs)
  2067. pci_unmap_single(dev,
  2068. dma_unmap_addr(&txq->meta[idx], mapping),
  2069. dma_unmap_len(&txq->meta[idx], len),
  2070. PCI_DMA_BIDIRECTIONAL);
  2071. /* Unmap chunks, if any. */
  2072. for (i = 1; i < num_tbs; i++)
  2073. pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
  2074. il4965_tfd_tb_get_len(tfd, i),
  2075. PCI_DMA_TODEVICE);
  2076. /* free SKB */
  2077. if (txq->txb) {
  2078. struct sk_buff *skb;
  2079. skb = txq->txb[txq->q.read_ptr].skb;
  2080. /* can be called from irqs-disabled context */
  2081. if (skb) {
  2082. dev_kfree_skb_any(skb);
  2083. txq->txb[txq->q.read_ptr].skb = NULL;
  2084. }
  2085. }
  2086. }
  2087. int il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il,
  2088. struct il_tx_queue *txq,
  2089. dma_addr_t addr, u16 len,
  2090. u8 reset, u8 pad)
  2091. {
  2092. struct il_queue *q;
  2093. struct il_tfd *tfd, *tfd_tmp;
  2094. u32 num_tbs;
  2095. q = &txq->q;
  2096. tfd_tmp = (struct il_tfd *)txq->tfds;
  2097. tfd = &tfd_tmp[q->write_ptr];
  2098. if (reset)
  2099. memset(tfd, 0, sizeof(*tfd));
  2100. num_tbs = il4965_tfd_get_num_tbs(tfd);
  2101. /* Each TFD can point to a maximum 20 Tx buffers */
  2102. if (num_tbs >= IL_NUM_OF_TBS) {
  2103. IL_ERR("Error can not send more than %d chunks\n",
  2104. IL_NUM_OF_TBS);
  2105. return -EINVAL;
  2106. }
  2107. BUG_ON(addr & ~DMA_BIT_MASK(36));
  2108. if (unlikely(addr & ~IL_TX_DMA_MASK))
  2109. IL_ERR("Unaligned address = %llx\n",
  2110. (unsigned long long)addr);
  2111. il4965_tfd_set_tb(tfd, num_tbs, addr, len);
  2112. return 0;
  2113. }
  2114. /*
  2115. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  2116. * given Tx queue, and enable the DMA channel used for that queue.
  2117. *
  2118. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  2119. * channels supported in hardware.
  2120. */
  2121. int il4965_hw_tx_queue_init(struct il_priv *il,
  2122. struct il_tx_queue *txq)
  2123. {
  2124. int txq_id = txq->q.id;
  2125. /* Circular buffer (TFD queue in DRAM) physical base address */
  2126. il_wr(il, FH_MEM_CBBC_QUEUE(txq_id),
  2127. txq->q.dma_addr >> 8);
  2128. return 0;
  2129. }
  2130. /******************************************************************************
  2131. *
  2132. * Generic RX handler implementations
  2133. *
  2134. ******************************************************************************/
  2135. static void il4965_rx_reply_alive(struct il_priv *il,
  2136. struct il_rx_buf *rxb)
  2137. {
  2138. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2139. struct il_alive_resp *palive;
  2140. struct delayed_work *pwork;
  2141. palive = &pkt->u.alive_frame;
  2142. D_INFO("Alive ucode status 0x%08X revision "
  2143. "0x%01X 0x%01X\n",
  2144. palive->is_valid, palive->ver_type,
  2145. palive->ver_subtype);
  2146. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  2147. D_INFO("Initialization Alive received.\n");
  2148. memcpy(&il->card_alive_init,
  2149. &pkt->u.alive_frame,
  2150. sizeof(struct il_init_alive_resp));
  2151. pwork = &il->init_alive_start;
  2152. } else {
  2153. D_INFO("Runtime Alive received.\n");
  2154. memcpy(&il->card_alive, &pkt->u.alive_frame,
  2155. sizeof(struct il_alive_resp));
  2156. pwork = &il->alive_start;
  2157. }
  2158. /* We delay the ALIVE response by 5ms to
  2159. * give the HW RF Kill time to activate... */
  2160. if (palive->is_valid == UCODE_VALID_OK)
  2161. queue_delayed_work(il->workqueue, pwork,
  2162. msecs_to_jiffies(5));
  2163. else
  2164. IL_WARN("uCode did not respond OK.\n");
  2165. }
  2166. /**
  2167. * il4965_bg_stats_periodic - Timer callback to queue stats
  2168. *
  2169. * This callback is provided in order to send a stats request.
  2170. *
  2171. * This timer function is continually reset to execute within
  2172. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  2173. * was received. We need to ensure we receive the stats in order
  2174. * to update the temperature used for calibrating the TXPOWER.
  2175. */
  2176. static void il4965_bg_stats_periodic(unsigned long data)
  2177. {
  2178. struct il_priv *il = (struct il_priv *)data;
  2179. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  2180. return;
  2181. /* dont send host command if rf-kill is on */
  2182. if (!il_is_ready_rf(il))
  2183. return;
  2184. il_send_stats_request(il, CMD_ASYNC, false);
  2185. }
  2186. static void il4965_rx_beacon_notif(struct il_priv *il,
  2187. struct il_rx_buf *rxb)
  2188. {
  2189. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2190. struct il4965_beacon_notif *beacon =
  2191. (struct il4965_beacon_notif *)pkt->u.raw;
  2192. #ifdef CONFIG_IWLEGACY_DEBUG
  2193. u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  2194. D_RX("beacon status %x retries %d iss %d "
  2195. "tsf %d %d rate %d\n",
  2196. le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
  2197. beacon->beacon_notify_hdr.failure_frame,
  2198. le32_to_cpu(beacon->ibss_mgr_status),
  2199. le32_to_cpu(beacon->high_tsf),
  2200. le32_to_cpu(beacon->low_tsf), rate);
  2201. #endif
  2202. il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  2203. }
  2204. static void il4965_perform_ct_kill_task(struct il_priv *il)
  2205. {
  2206. unsigned long flags;
  2207. D_POWER("Stop all queues\n");
  2208. if (il->mac80211_registered)
  2209. ieee80211_stop_queues(il->hw);
  2210. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  2211. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  2212. _il_rd(il, CSR_UCODE_DRV_GP1);
  2213. spin_lock_irqsave(&il->reg_lock, flags);
  2214. if (!_il_grab_nic_access(il))
  2215. _il_release_nic_access(il);
  2216. spin_unlock_irqrestore(&il->reg_lock, flags);
  2217. }
  2218. /* Handle notification from uCode that card's power state is changing
  2219. * due to software, hardware, or critical temperature RFKILL */
  2220. static void il4965_rx_card_state_notif(struct il_priv *il,
  2221. struct il_rx_buf *rxb)
  2222. {
  2223. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2224. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  2225. unsigned long status = il->status;
  2226. D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
  2227. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  2228. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  2229. (flags & CT_CARD_DISABLED) ?
  2230. "Reached" : "Not reached");
  2231. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
  2232. CT_CARD_DISABLED)) {
  2233. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  2234. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2235. il_wr(il, HBUS_TARG_MBX_C,
  2236. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  2237. if (!(flags & RXON_CARD_DISABLED)) {
  2238. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  2239. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2240. il_wr(il, HBUS_TARG_MBX_C,
  2241. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  2242. }
  2243. }
  2244. if (flags & CT_CARD_DISABLED)
  2245. il4965_perform_ct_kill_task(il);
  2246. if (flags & HW_CARD_DISABLED)
  2247. set_bit(STATUS_RF_KILL_HW, &il->status);
  2248. else
  2249. clear_bit(STATUS_RF_KILL_HW, &il->status);
  2250. if (!(flags & RXON_CARD_DISABLED))
  2251. il_scan_cancel(il);
  2252. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  2253. test_bit(STATUS_RF_KILL_HW, &il->status)))
  2254. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  2255. test_bit(STATUS_RF_KILL_HW, &il->status));
  2256. else
  2257. wake_up(&il->wait_command_queue);
  2258. }
  2259. /**
  2260. * il4965_setup_rx_handlers - Initialize Rx handler callbacks
  2261. *
  2262. * Setup the RX handlers for each of the reply types sent from the uCode
  2263. * to the host.
  2264. *
  2265. * This function chains into the hardware specific files for them to setup
  2266. * any hardware specific handlers as well.
  2267. */
  2268. static void il4965_setup_rx_handlers(struct il_priv *il)
  2269. {
  2270. il->rx_handlers[REPLY_ALIVE] = il4965_rx_reply_alive;
  2271. il->rx_handlers[REPLY_ERROR] = il_rx_reply_error;
  2272. il->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = il_rx_csa;
  2273. il->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  2274. il_rx_spectrum_measure_notif;
  2275. il->rx_handlers[PM_SLEEP_NOTIFICATION] = il_rx_pm_sleep_notif;
  2276. il->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  2277. il_rx_pm_debug_stats_notif;
  2278. il->rx_handlers[BEACON_NOTIFICATION] = il4965_rx_beacon_notif;
  2279. /*
  2280. * The same handler is used for both the REPLY to a discrete
  2281. * stats request from the host as well as for the periodic
  2282. * stats notifications (after received beacons) from the uCode.
  2283. */
  2284. il->rx_handlers[REPLY_STATISTICS_CMD] = il4965_reply_stats;
  2285. il->rx_handlers[STATISTICS_NOTIFICATION] = il4965_rx_stats;
  2286. il_setup_rx_scan_handlers(il);
  2287. /* status change handler */
  2288. il->rx_handlers[CARD_STATE_NOTIFICATION] =
  2289. il4965_rx_card_state_notif;
  2290. il->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
  2291. il4965_rx_missed_beacon_notif;
  2292. /* Rx handlers */
  2293. il->rx_handlers[REPLY_RX_PHY_CMD] = il4965_rx_reply_rx_phy;
  2294. il->rx_handlers[REPLY_RX_MPDU_CMD] = il4965_rx_reply_rx;
  2295. /* block ack */
  2296. il->rx_handlers[REPLY_COMPRESSED_BA] = il4965_rx_reply_compressed_ba;
  2297. /* Set up hardware specific Rx handlers */
  2298. il->cfg->ops->lib->rx_handler_setup(il);
  2299. }
  2300. /**
  2301. * il4965_rx_handle - Main entry function for receiving responses from uCode
  2302. *
  2303. * Uses the il->rx_handlers callback function array to invoke
  2304. * the appropriate handlers, including command responses,
  2305. * frame-received notifications, and other notifications.
  2306. */
  2307. void il4965_rx_handle(struct il_priv *il)
  2308. {
  2309. struct il_rx_buf *rxb;
  2310. struct il_rx_pkt *pkt;
  2311. struct il_rx_queue *rxq = &il->rxq;
  2312. u32 r, i;
  2313. int reclaim;
  2314. unsigned long flags;
  2315. u8 fill_rx = 0;
  2316. u32 count = 8;
  2317. int total_empty;
  2318. /* uCode's read idx (stored in shared DRAM) indicates the last Rx
  2319. * buffer that the driver may process (last buffer filled by ucode). */
  2320. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  2321. i = rxq->read;
  2322. /* Rx interrupt, but nothing sent from uCode */
  2323. if (i == r)
  2324. D_RX("r = %d, i = %d\n", r, i);
  2325. /* calculate total frames need to be restock after handling RX */
  2326. total_empty = r - rxq->write_actual;
  2327. if (total_empty < 0)
  2328. total_empty += RX_QUEUE_SIZE;
  2329. if (total_empty > (RX_QUEUE_SIZE / 2))
  2330. fill_rx = 1;
  2331. while (i != r) {
  2332. int len;
  2333. rxb = rxq->queue[i];
  2334. /* If an RXB doesn't have a Rx queue slot associated with it,
  2335. * then a bug has been introduced in the queue refilling
  2336. * routines -- catch it here */
  2337. BUG_ON(rxb == NULL);
  2338. rxq->queue[i] = NULL;
  2339. pci_unmap_page(il->pci_dev, rxb->page_dma,
  2340. PAGE_SIZE << il->hw_params.rx_page_order,
  2341. PCI_DMA_FROMDEVICE);
  2342. pkt = rxb_addr(rxb);
  2343. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  2344. len += sizeof(u32); /* account for status word */
  2345. /* Reclaim a command buffer only if this packet is a response
  2346. * to a (driver-originated) command.
  2347. * If the packet (e.g. Rx frame) originated from uCode,
  2348. * there is no command buffer to reclaim.
  2349. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  2350. * but apparently a few don't get set; catch them here. */
  2351. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  2352. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  2353. (pkt->hdr.cmd != REPLY_RX) &&
  2354. (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
  2355. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  2356. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  2357. (pkt->hdr.cmd != REPLY_TX);
  2358. /* Based on type of command response or notification,
  2359. * handle those that need handling via function in
  2360. * rx_handlers table. See il4965_setup_rx_handlers() */
  2361. if (il->rx_handlers[pkt->hdr.cmd]) {
  2362. D_RX("r = %d, i = %d, %s, 0x%02x\n", r,
  2363. i, il_get_cmd_string(pkt->hdr.cmd),
  2364. pkt->hdr.cmd);
  2365. il->isr_stats.rx_handlers[pkt->hdr.cmd]++;
  2366. il->rx_handlers[pkt->hdr.cmd] (il, rxb);
  2367. } else {
  2368. /* No handling needed */
  2369. D_RX(
  2370. "r %d i %d No handler needed for %s, 0x%02x\n",
  2371. r, i, il_get_cmd_string(pkt->hdr.cmd),
  2372. pkt->hdr.cmd);
  2373. }
  2374. /*
  2375. * XXX: After here, we should always check rxb->page
  2376. * against NULL before touching it or its virtual
  2377. * memory (pkt). Because some rx_handler might have
  2378. * already taken or freed the pages.
  2379. */
  2380. if (reclaim) {
  2381. /* Invoke any callbacks, transfer the buffer to caller,
  2382. * and fire off the (possibly) blocking il_send_cmd()
  2383. * as we reclaim the driver command queue */
  2384. if (rxb->page)
  2385. il_tx_cmd_complete(il, rxb);
  2386. else
  2387. IL_WARN("Claim null rxb?\n");
  2388. }
  2389. /* Reuse the page if possible. For notification packets and
  2390. * SKBs that fail to Rx correctly, add them back into the
  2391. * rx_free list for reuse later. */
  2392. spin_lock_irqsave(&rxq->lock, flags);
  2393. if (rxb->page != NULL) {
  2394. rxb->page_dma = pci_map_page(il->pci_dev, rxb->page,
  2395. 0, PAGE_SIZE << il->hw_params.rx_page_order,
  2396. PCI_DMA_FROMDEVICE);
  2397. list_add_tail(&rxb->list, &rxq->rx_free);
  2398. rxq->free_count++;
  2399. } else
  2400. list_add_tail(&rxb->list, &rxq->rx_used);
  2401. spin_unlock_irqrestore(&rxq->lock, flags);
  2402. i = (i + 1) & RX_QUEUE_MASK;
  2403. /* If there are a lot of unused frames,
  2404. * restock the Rx queue so ucode wont assert. */
  2405. if (fill_rx) {
  2406. count++;
  2407. if (count >= 8) {
  2408. rxq->read = i;
  2409. il4965_rx_replenish_now(il);
  2410. count = 0;
  2411. }
  2412. }
  2413. }
  2414. /* Backtrack one entry */
  2415. rxq->read = i;
  2416. if (fill_rx)
  2417. il4965_rx_replenish_now(il);
  2418. else
  2419. il4965_rx_queue_restock(il);
  2420. }
  2421. /* call this function to flush any scheduled tasklet */
  2422. static inline void il4965_synchronize_irq(struct il_priv *il)
  2423. {
  2424. /* wait to make sure we flush pending tasklet*/
  2425. synchronize_irq(il->pci_dev->irq);
  2426. tasklet_kill(&il->irq_tasklet);
  2427. }
  2428. static void il4965_irq_tasklet(struct il_priv *il)
  2429. {
  2430. u32 inta, handled = 0;
  2431. u32 inta_fh;
  2432. unsigned long flags;
  2433. u32 i;
  2434. #ifdef CONFIG_IWLEGACY_DEBUG
  2435. u32 inta_mask;
  2436. #endif
  2437. spin_lock_irqsave(&il->lock, flags);
  2438. /* Ack/clear/reset pending uCode interrupts.
  2439. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  2440. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  2441. inta = _il_rd(il, CSR_INT);
  2442. _il_wr(il, CSR_INT, inta);
  2443. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  2444. * Any new interrupts that happen after this, either while we're
  2445. * in this tasklet, or later, will show up in next ISR/tasklet. */
  2446. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  2447. _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
  2448. #ifdef CONFIG_IWLEGACY_DEBUG
  2449. if (il_get_debug_level(il) & IL_DL_ISR) {
  2450. /* just for debug */
  2451. inta_mask = _il_rd(il, CSR_INT_MASK);
  2452. D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  2453. inta, inta_mask, inta_fh);
  2454. }
  2455. #endif
  2456. spin_unlock_irqrestore(&il->lock, flags);
  2457. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  2458. * atomic, make sure that inta covers all the interrupts that
  2459. * we've discovered, even if FH interrupt came in just after
  2460. * reading CSR_INT. */
  2461. if (inta_fh & CSR49_FH_INT_RX_MASK)
  2462. inta |= CSR_INT_BIT_FH_RX;
  2463. if (inta_fh & CSR49_FH_INT_TX_MASK)
  2464. inta |= CSR_INT_BIT_FH_TX;
  2465. /* Now service all interrupt bits discovered above. */
  2466. if (inta & CSR_INT_BIT_HW_ERR) {
  2467. IL_ERR("Hardware error detected. Restarting.\n");
  2468. /* Tell the device to stop sending interrupts */
  2469. il_disable_interrupts(il);
  2470. il->isr_stats.hw++;
  2471. il_irq_handle_error(il);
  2472. handled |= CSR_INT_BIT_HW_ERR;
  2473. return;
  2474. }
  2475. #ifdef CONFIG_IWLEGACY_DEBUG
  2476. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  2477. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  2478. if (inta & CSR_INT_BIT_SCD) {
  2479. D_ISR("Scheduler finished to transmit "
  2480. "the frame/frames.\n");
  2481. il->isr_stats.sch++;
  2482. }
  2483. /* Alive notification via Rx interrupt will do the real work */
  2484. if (inta & CSR_INT_BIT_ALIVE) {
  2485. D_ISR("Alive interrupt\n");
  2486. il->isr_stats.alive++;
  2487. }
  2488. }
  2489. #endif
  2490. /* Safely ignore these bits for debug checks below */
  2491. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  2492. /* HW RF KILL switch toggled */
  2493. if (inta & CSR_INT_BIT_RF_KILL) {
  2494. int hw_rf_kill = 0;
  2495. if (!(_il_rd(il, CSR_GP_CNTRL) &
  2496. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  2497. hw_rf_kill = 1;
  2498. IL_WARN("RF_KILL bit toggled to %s.\n",
  2499. hw_rf_kill ? "disable radio" : "enable radio");
  2500. il->isr_stats.rfkill++;
  2501. /* driver only loads ucode once setting the interface up.
  2502. * the driver allows loading the ucode even if the radio
  2503. * is killed. Hence update the killswitch state here. The
  2504. * rfkill handler will care about restarting if needed.
  2505. */
  2506. if (!test_bit(STATUS_ALIVE, &il->status)) {
  2507. if (hw_rf_kill)
  2508. set_bit(STATUS_RF_KILL_HW, &il->status);
  2509. else
  2510. clear_bit(STATUS_RF_KILL_HW, &il->status);
  2511. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
  2512. }
  2513. handled |= CSR_INT_BIT_RF_KILL;
  2514. }
  2515. /* Chip got too hot and stopped itself */
  2516. if (inta & CSR_INT_BIT_CT_KILL) {
  2517. IL_ERR("Microcode CT kill error detected.\n");
  2518. il->isr_stats.ctkill++;
  2519. handled |= CSR_INT_BIT_CT_KILL;
  2520. }
  2521. /* Error detected by uCode */
  2522. if (inta & CSR_INT_BIT_SW_ERR) {
  2523. IL_ERR("Microcode SW error detected. "
  2524. " Restarting 0x%X.\n", inta);
  2525. il->isr_stats.sw++;
  2526. il_irq_handle_error(il);
  2527. handled |= CSR_INT_BIT_SW_ERR;
  2528. }
  2529. /*
  2530. * uCode wakes up after power-down sleep.
  2531. * Tell device about any new tx or host commands enqueued,
  2532. * and about any Rx buffers made available while asleep.
  2533. */
  2534. if (inta & CSR_INT_BIT_WAKEUP) {
  2535. D_ISR("Wakeup interrupt\n");
  2536. il_rx_queue_update_write_ptr(il, &il->rxq);
  2537. for (i = 0; i < il->hw_params.max_txq_num; i++)
  2538. il_txq_update_write_ptr(il, &il->txq[i]);
  2539. il->isr_stats.wakeup++;
  2540. handled |= CSR_INT_BIT_WAKEUP;
  2541. }
  2542. /* All uCode command responses, including Tx command responses,
  2543. * Rx "responses" (frame-received notification), and other
  2544. * notifications from uCode come through here*/
  2545. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  2546. il4965_rx_handle(il);
  2547. il->isr_stats.rx++;
  2548. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  2549. }
  2550. /* This "Tx" DMA channel is used only for loading uCode */
  2551. if (inta & CSR_INT_BIT_FH_TX) {
  2552. D_ISR("uCode load interrupt\n");
  2553. il->isr_stats.tx++;
  2554. handled |= CSR_INT_BIT_FH_TX;
  2555. /* Wake up uCode load routine, now that load is complete */
  2556. il->ucode_write_complete = 1;
  2557. wake_up(&il->wait_command_queue);
  2558. }
  2559. if (inta & ~handled) {
  2560. IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  2561. il->isr_stats.unhandled++;
  2562. }
  2563. if (inta & ~(il->inta_mask)) {
  2564. IL_WARN("Disabled INTA bits 0x%08x were pending\n",
  2565. inta & ~il->inta_mask);
  2566. IL_WARN(" with FH_INT = 0x%08x\n", inta_fh);
  2567. }
  2568. /* Re-enable all interrupts */
  2569. /* only Re-enable if disabled by irq */
  2570. if (test_bit(STATUS_INT_ENABLED, &il->status))
  2571. il_enable_interrupts(il);
  2572. /* Re-enable RF_KILL if it occurred */
  2573. else if (handled & CSR_INT_BIT_RF_KILL)
  2574. il_enable_rfkill_int(il);
  2575. #ifdef CONFIG_IWLEGACY_DEBUG
  2576. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  2577. inta = _il_rd(il, CSR_INT);
  2578. inta_mask = _il_rd(il, CSR_INT_MASK);
  2579. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  2580. D_ISR(
  2581. "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  2582. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  2583. }
  2584. #endif
  2585. }
  2586. /*****************************************************************************
  2587. *
  2588. * sysfs attributes
  2589. *
  2590. *****************************************************************************/
  2591. #ifdef CONFIG_IWLEGACY_DEBUG
  2592. /*
  2593. * The following adds a new attribute to the sysfs representation
  2594. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  2595. * used for controlling the debug level.
  2596. *
  2597. * See the level definitions in iwl for details.
  2598. *
  2599. * The debug_level being managed using sysfs below is a per device debug
  2600. * level that is used instead of the global debug level if it (the per
  2601. * device debug level) is set.
  2602. */
  2603. static ssize_t il4965_show_debug_level(struct device *d,
  2604. struct device_attribute *attr, char *buf)
  2605. {
  2606. struct il_priv *il = dev_get_drvdata(d);
  2607. return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
  2608. }
  2609. static ssize_t il4965_store_debug_level(struct device *d,
  2610. struct device_attribute *attr,
  2611. const char *buf, size_t count)
  2612. {
  2613. struct il_priv *il = dev_get_drvdata(d);
  2614. unsigned long val;
  2615. int ret;
  2616. ret = strict_strtoul(buf, 0, &val);
  2617. if (ret)
  2618. IL_ERR("%s is not in hex or decimal form.\n", buf);
  2619. else {
  2620. il->debug_level = val;
  2621. if (il_alloc_traffic_mem(il))
  2622. IL_ERR(
  2623. "Not enough memory to generate traffic log\n");
  2624. }
  2625. return strnlen(buf, count);
  2626. }
  2627. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  2628. il4965_show_debug_level, il4965_store_debug_level);
  2629. #endif /* CONFIG_IWLEGACY_DEBUG */
  2630. static ssize_t il4965_show_temperature(struct device *d,
  2631. struct device_attribute *attr, char *buf)
  2632. {
  2633. struct il_priv *il = dev_get_drvdata(d);
  2634. if (!il_is_alive(il))
  2635. return -EAGAIN;
  2636. return sprintf(buf, "%d\n", il->temperature);
  2637. }
  2638. static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
  2639. static ssize_t il4965_show_tx_power(struct device *d,
  2640. struct device_attribute *attr, char *buf)
  2641. {
  2642. struct il_priv *il = dev_get_drvdata(d);
  2643. if (!il_is_ready_rf(il))
  2644. return sprintf(buf, "off\n");
  2645. else
  2646. return sprintf(buf, "%d\n", il->tx_power_user_lmt);
  2647. }
  2648. static ssize_t il4965_store_tx_power(struct device *d,
  2649. struct device_attribute *attr,
  2650. const char *buf, size_t count)
  2651. {
  2652. struct il_priv *il = dev_get_drvdata(d);
  2653. unsigned long val;
  2654. int ret;
  2655. ret = strict_strtoul(buf, 10, &val);
  2656. if (ret)
  2657. IL_INFO("%s is not in decimal form.\n", buf);
  2658. else {
  2659. ret = il_set_tx_power(il, val, false);
  2660. if (ret)
  2661. IL_ERR("failed setting tx power (0x%d).\n",
  2662. ret);
  2663. else
  2664. ret = count;
  2665. }
  2666. return ret;
  2667. }
  2668. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO,
  2669. il4965_show_tx_power, il4965_store_tx_power);
  2670. static struct attribute *il_sysfs_entries[] = {
  2671. &dev_attr_temperature.attr,
  2672. &dev_attr_tx_power.attr,
  2673. #ifdef CONFIG_IWLEGACY_DEBUG
  2674. &dev_attr_debug_level.attr,
  2675. #endif
  2676. NULL
  2677. };
  2678. static struct attribute_group il_attribute_group = {
  2679. .name = NULL, /* put in device directory */
  2680. .attrs = il_sysfs_entries,
  2681. };
  2682. /******************************************************************************
  2683. *
  2684. * uCode download functions
  2685. *
  2686. ******************************************************************************/
  2687. static void il4965_dealloc_ucode_pci(struct il_priv *il)
  2688. {
  2689. il_free_fw_desc(il->pci_dev, &il->ucode_code);
  2690. il_free_fw_desc(il->pci_dev, &il->ucode_data);
  2691. il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
  2692. il_free_fw_desc(il->pci_dev, &il->ucode_init);
  2693. il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
  2694. il_free_fw_desc(il->pci_dev, &il->ucode_boot);
  2695. }
  2696. static void il4965_nic_start(struct il_priv *il)
  2697. {
  2698. /* Remove all resets to allow NIC to operate */
  2699. _il_wr(il, CSR_RESET, 0);
  2700. }
  2701. static void il4965_ucode_callback(const struct firmware *ucode_raw,
  2702. void *context);
  2703. static int il4965_mac_setup_register(struct il_priv *il,
  2704. u32 max_probe_length);
  2705. static int __must_check il4965_request_firmware(struct il_priv *il, bool first)
  2706. {
  2707. const char *name_pre = il->cfg->fw_name_pre;
  2708. char tag[8];
  2709. if (first) {
  2710. il->fw_idx = il->cfg->ucode_api_max;
  2711. sprintf(tag, "%d", il->fw_idx);
  2712. } else {
  2713. il->fw_idx--;
  2714. sprintf(tag, "%d", il->fw_idx);
  2715. }
  2716. if (il->fw_idx < il->cfg->ucode_api_min) {
  2717. IL_ERR("no suitable firmware found!\n");
  2718. return -ENOENT;
  2719. }
  2720. sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  2721. D_INFO("attempting to load firmware '%s'\n",
  2722. il->firmware_name);
  2723. return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
  2724. &il->pci_dev->dev, GFP_KERNEL, il,
  2725. il4965_ucode_callback);
  2726. }
  2727. struct il4965_firmware_pieces {
  2728. const void *inst, *data, *init, *init_data, *boot;
  2729. size_t inst_size, data_size, init_size, init_data_size, boot_size;
  2730. };
  2731. static int il4965_load_firmware(struct il_priv *il,
  2732. const struct firmware *ucode_raw,
  2733. struct il4965_firmware_pieces *pieces)
  2734. {
  2735. struct il_ucode_header *ucode = (void *)ucode_raw->data;
  2736. u32 api_ver, hdr_size;
  2737. const u8 *src;
  2738. il->ucode_ver = le32_to_cpu(ucode->ver);
  2739. api_ver = IL_UCODE_API(il->ucode_ver);
  2740. switch (api_ver) {
  2741. default:
  2742. case 0:
  2743. case 1:
  2744. case 2:
  2745. hdr_size = 24;
  2746. if (ucode_raw->size < hdr_size) {
  2747. IL_ERR("File size too small!\n");
  2748. return -EINVAL;
  2749. }
  2750. pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
  2751. pieces->data_size = le32_to_cpu(ucode->v1.data_size);
  2752. pieces->init_size = le32_to_cpu(ucode->v1.init_size);
  2753. pieces->init_data_size =
  2754. le32_to_cpu(ucode->v1.init_data_size);
  2755. pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
  2756. src = ucode->v1.data;
  2757. break;
  2758. }
  2759. /* Verify size of file vs. image size info in file's header */
  2760. if (ucode_raw->size != hdr_size + pieces->inst_size +
  2761. pieces->data_size + pieces->init_size +
  2762. pieces->init_data_size + pieces->boot_size) {
  2763. IL_ERR(
  2764. "uCode file size %d does not match expected size\n",
  2765. (int)ucode_raw->size);
  2766. return -EINVAL;
  2767. }
  2768. pieces->inst = src;
  2769. src += pieces->inst_size;
  2770. pieces->data = src;
  2771. src += pieces->data_size;
  2772. pieces->init = src;
  2773. src += pieces->init_size;
  2774. pieces->init_data = src;
  2775. src += pieces->init_data_size;
  2776. pieces->boot = src;
  2777. src += pieces->boot_size;
  2778. return 0;
  2779. }
  2780. /**
  2781. * il4965_ucode_callback - callback when firmware was loaded
  2782. *
  2783. * If loaded successfully, copies the firmware into buffers
  2784. * for the card to fetch (via DMA).
  2785. */
  2786. static void
  2787. il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
  2788. {
  2789. struct il_priv *il = context;
  2790. struct il_ucode_header *ucode;
  2791. int err;
  2792. struct il4965_firmware_pieces pieces;
  2793. const unsigned int api_max = il->cfg->ucode_api_max;
  2794. const unsigned int api_min = il->cfg->ucode_api_min;
  2795. u32 api_ver;
  2796. u32 max_probe_length = 200;
  2797. u32 standard_phy_calibration_size =
  2798. IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  2799. memset(&pieces, 0, sizeof(pieces));
  2800. if (!ucode_raw) {
  2801. if (il->fw_idx <= il->cfg->ucode_api_max)
  2802. IL_ERR(
  2803. "request for firmware file '%s' failed.\n",
  2804. il->firmware_name);
  2805. goto try_again;
  2806. }
  2807. D_INFO("Loaded firmware file '%s' (%zd bytes).\n",
  2808. il->firmware_name, ucode_raw->size);
  2809. /* Make sure that we got at least the API version number */
  2810. if (ucode_raw->size < 4) {
  2811. IL_ERR("File size way too small!\n");
  2812. goto try_again;
  2813. }
  2814. /* Data from ucode file: header followed by uCode images */
  2815. ucode = (struct il_ucode_header *)ucode_raw->data;
  2816. err = il4965_load_firmware(il, ucode_raw, &pieces);
  2817. if (err)
  2818. goto try_again;
  2819. api_ver = IL_UCODE_API(il->ucode_ver);
  2820. /*
  2821. * api_ver should match the api version forming part of the
  2822. * firmware filename ... but we don't check for that and only rely
  2823. * on the API version read from firmware header from here on forward
  2824. */
  2825. if (api_ver < api_min || api_ver > api_max) {
  2826. IL_ERR(
  2827. "Driver unable to support your firmware API. "
  2828. "Driver supports v%u, firmware is v%u.\n",
  2829. api_max, api_ver);
  2830. goto try_again;
  2831. }
  2832. if (api_ver != api_max)
  2833. IL_ERR(
  2834. "Firmware has old API version. Expected v%u, "
  2835. "got v%u. New firmware can be obtained "
  2836. "from http://www.intellinuxwireless.org.\n",
  2837. api_max, api_ver);
  2838. IL_INFO("loaded firmware version %u.%u.%u.%u\n",
  2839. IL_UCODE_MAJOR(il->ucode_ver),
  2840. IL_UCODE_MINOR(il->ucode_ver),
  2841. IL_UCODE_API(il->ucode_ver),
  2842. IL_UCODE_SERIAL(il->ucode_ver));
  2843. snprintf(il->hw->wiphy->fw_version,
  2844. sizeof(il->hw->wiphy->fw_version),
  2845. "%u.%u.%u.%u",
  2846. IL_UCODE_MAJOR(il->ucode_ver),
  2847. IL_UCODE_MINOR(il->ucode_ver),
  2848. IL_UCODE_API(il->ucode_ver),
  2849. IL_UCODE_SERIAL(il->ucode_ver));
  2850. /*
  2851. * For any of the failures below (before allocating pci memory)
  2852. * we will try to load a version with a smaller API -- maybe the
  2853. * user just got a corrupted version of the latest API.
  2854. */
  2855. D_INFO("f/w package hdr ucode version raw = 0x%x\n",
  2856. il->ucode_ver);
  2857. D_INFO("f/w package hdr runtime inst size = %Zd\n",
  2858. pieces.inst_size);
  2859. D_INFO("f/w package hdr runtime data size = %Zd\n",
  2860. pieces.data_size);
  2861. D_INFO("f/w package hdr init inst size = %Zd\n",
  2862. pieces.init_size);
  2863. D_INFO("f/w package hdr init data size = %Zd\n",
  2864. pieces.init_data_size);
  2865. D_INFO("f/w package hdr boot inst size = %Zd\n",
  2866. pieces.boot_size);
  2867. /* Verify that uCode images will fit in card's SRAM */
  2868. if (pieces.inst_size > il->hw_params.max_inst_size) {
  2869. IL_ERR("uCode instr len %Zd too large to fit in\n",
  2870. pieces.inst_size);
  2871. goto try_again;
  2872. }
  2873. if (pieces.data_size > il->hw_params.max_data_size) {
  2874. IL_ERR("uCode data len %Zd too large to fit in\n",
  2875. pieces.data_size);
  2876. goto try_again;
  2877. }
  2878. if (pieces.init_size > il->hw_params.max_inst_size) {
  2879. IL_ERR("uCode init instr len %Zd too large to fit in\n",
  2880. pieces.init_size);
  2881. goto try_again;
  2882. }
  2883. if (pieces.init_data_size > il->hw_params.max_data_size) {
  2884. IL_ERR("uCode init data len %Zd too large to fit in\n",
  2885. pieces.init_data_size);
  2886. goto try_again;
  2887. }
  2888. if (pieces.boot_size > il->hw_params.max_bsm_size) {
  2889. IL_ERR("uCode boot instr len %Zd too large to fit in\n",
  2890. pieces.boot_size);
  2891. goto try_again;
  2892. }
  2893. /* Allocate ucode buffers for card's bus-master loading ... */
  2894. /* Runtime instructions and 2 copies of data:
  2895. * 1) unmodified from disk
  2896. * 2) backup cache for save/restore during power-downs */
  2897. il->ucode_code.len = pieces.inst_size;
  2898. il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
  2899. il->ucode_data.len = pieces.data_size;
  2900. il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
  2901. il->ucode_data_backup.len = pieces.data_size;
  2902. il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
  2903. if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
  2904. !il->ucode_data_backup.v_addr)
  2905. goto err_pci_alloc;
  2906. /* Initialization instructions and data */
  2907. if (pieces.init_size && pieces.init_data_size) {
  2908. il->ucode_init.len = pieces.init_size;
  2909. il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
  2910. il->ucode_init_data.len = pieces.init_data_size;
  2911. il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
  2912. if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
  2913. goto err_pci_alloc;
  2914. }
  2915. /* Bootstrap (instructions only, no data) */
  2916. if (pieces.boot_size) {
  2917. il->ucode_boot.len = pieces.boot_size;
  2918. il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
  2919. if (!il->ucode_boot.v_addr)
  2920. goto err_pci_alloc;
  2921. }
  2922. /* Now that we can no longer fail, copy information */
  2923. il->sta_key_max_num = STA_KEY_MAX_NUM;
  2924. /* Copy images into buffers for card's bus-master reads ... */
  2925. /* Runtime instructions (first block of data in file) */
  2926. D_INFO("Copying (but not loading) uCode instr len %Zd\n",
  2927. pieces.inst_size);
  2928. memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
  2929. D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  2930. il->ucode_code.v_addr, (u32)il->ucode_code.p_addr);
  2931. /*
  2932. * Runtime data
  2933. * NOTE: Copy into backup buffer will be done in il_up()
  2934. */
  2935. D_INFO("Copying (but not loading) uCode data len %Zd\n",
  2936. pieces.data_size);
  2937. memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
  2938. memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
  2939. /* Initialization instructions */
  2940. if (pieces.init_size) {
  2941. D_INFO(
  2942. "Copying (but not loading) init instr len %Zd\n",
  2943. pieces.init_size);
  2944. memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
  2945. }
  2946. /* Initialization data */
  2947. if (pieces.init_data_size) {
  2948. D_INFO(
  2949. "Copying (but not loading) init data len %Zd\n",
  2950. pieces.init_data_size);
  2951. memcpy(il->ucode_init_data.v_addr, pieces.init_data,
  2952. pieces.init_data_size);
  2953. }
  2954. /* Bootstrap instructions */
  2955. D_INFO("Copying (but not loading) boot instr len %Zd\n",
  2956. pieces.boot_size);
  2957. memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
  2958. /*
  2959. * figure out the offset of chain noise reset and gain commands
  2960. * base on the size of standard phy calibration commands table size
  2961. */
  2962. il->_4965.phy_calib_chain_noise_reset_cmd =
  2963. standard_phy_calibration_size;
  2964. il->_4965.phy_calib_chain_noise_gain_cmd =
  2965. standard_phy_calibration_size + 1;
  2966. /**************************************************
  2967. * This is still part of probe() in a sense...
  2968. *
  2969. * 9. Setup and register with mac80211 and debugfs
  2970. **************************************************/
  2971. err = il4965_mac_setup_register(il, max_probe_length);
  2972. if (err)
  2973. goto out_unbind;
  2974. err = il_dbgfs_register(il, DRV_NAME);
  2975. if (err)
  2976. IL_ERR(
  2977. "failed to create debugfs files. Ignoring error: %d\n", err);
  2978. err = sysfs_create_group(&il->pci_dev->dev.kobj,
  2979. &il_attribute_group);
  2980. if (err) {
  2981. IL_ERR("failed to create sysfs device attributes\n");
  2982. goto out_unbind;
  2983. }
  2984. /* We have our copies now, allow OS release its copies */
  2985. release_firmware(ucode_raw);
  2986. complete(&il->_4965.firmware_loading_complete);
  2987. return;
  2988. try_again:
  2989. /* try next, if any */
  2990. if (il4965_request_firmware(il, false))
  2991. goto out_unbind;
  2992. release_firmware(ucode_raw);
  2993. return;
  2994. err_pci_alloc:
  2995. IL_ERR("failed to allocate pci memory\n");
  2996. il4965_dealloc_ucode_pci(il);
  2997. out_unbind:
  2998. complete(&il->_4965.firmware_loading_complete);
  2999. device_release_driver(&il->pci_dev->dev);
  3000. release_firmware(ucode_raw);
  3001. }
  3002. static const char * const desc_lookup_text[] = {
  3003. "OK",
  3004. "FAIL",
  3005. "BAD_PARAM",
  3006. "BAD_CHECKSUM",
  3007. "NMI_INTERRUPT_WDG",
  3008. "SYSASSERT",
  3009. "FATAL_ERROR",
  3010. "BAD_COMMAND",
  3011. "HW_ERROR_TUNE_LOCK",
  3012. "HW_ERROR_TEMPERATURE",
  3013. "ILLEGAL_CHAN_FREQ",
  3014. "VCC_NOT_STBL",
  3015. "FH_ERROR",
  3016. "NMI_INTERRUPT_HOST",
  3017. "NMI_INTERRUPT_ACTION_PT",
  3018. "NMI_INTERRUPT_UNKNOWN",
  3019. "UCODE_VERSION_MISMATCH",
  3020. "HW_ERROR_ABS_LOCK",
  3021. "HW_ERROR_CAL_LOCK_FAIL",
  3022. "NMI_INTERRUPT_INST_ACTION_PT",
  3023. "NMI_INTERRUPT_DATA_ACTION_PT",
  3024. "NMI_TRM_HW_ER",
  3025. "NMI_INTERRUPT_TRM",
  3026. "NMI_INTERRUPT_BREAK_POINT",
  3027. "DEBUG_0",
  3028. "DEBUG_1",
  3029. "DEBUG_2",
  3030. "DEBUG_3",
  3031. };
  3032. static struct { char *name; u8 num; } advanced_lookup[] = {
  3033. { "NMI_INTERRUPT_WDG", 0x34 },
  3034. { "SYSASSERT", 0x35 },
  3035. { "UCODE_VERSION_MISMATCH", 0x37 },
  3036. { "BAD_COMMAND", 0x38 },
  3037. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  3038. { "FATAL_ERROR", 0x3D },
  3039. { "NMI_TRM_HW_ERR", 0x46 },
  3040. { "NMI_INTERRUPT_TRM", 0x4C },
  3041. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  3042. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  3043. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  3044. { "NMI_INTERRUPT_HOST", 0x66 },
  3045. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  3046. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  3047. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  3048. { "ADVANCED_SYSASSERT", 0 },
  3049. };
  3050. static const char *il4965_desc_lookup(u32 num)
  3051. {
  3052. int i;
  3053. int max = ARRAY_SIZE(desc_lookup_text);
  3054. if (num < max)
  3055. return desc_lookup_text[num];
  3056. max = ARRAY_SIZE(advanced_lookup) - 1;
  3057. for (i = 0; i < max; i++) {
  3058. if (advanced_lookup[i].num == num)
  3059. break;
  3060. }
  3061. return advanced_lookup[i].name;
  3062. }
  3063. #define ERROR_START_OFFSET (1 * sizeof(u32))
  3064. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  3065. void il4965_dump_nic_error_log(struct il_priv *il)
  3066. {
  3067. u32 data2, line;
  3068. u32 desc, time, count, base, data1;
  3069. u32 blink1, blink2, ilink1, ilink2;
  3070. u32 pc, hcmd;
  3071. if (il->ucode_type == UCODE_INIT) {
  3072. base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
  3073. } else {
  3074. base = le32_to_cpu(il->card_alive.error_event_table_ptr);
  3075. }
  3076. if (!il->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  3077. IL_ERR(
  3078. "Not valid error log pointer 0x%08X for %s uCode\n",
  3079. base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
  3080. return;
  3081. }
  3082. count = il_read_targ_mem(il, base);
  3083. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  3084. IL_ERR("Start IWL Error Log Dump:\n");
  3085. IL_ERR("Status: 0x%08lX, count: %d\n",
  3086. il->status, count);
  3087. }
  3088. desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
  3089. il->isr_stats.err_code = desc;
  3090. pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
  3091. blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
  3092. blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
  3093. ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
  3094. ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
  3095. data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
  3096. data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
  3097. line = il_read_targ_mem(il, base + 9 * sizeof(u32));
  3098. time = il_read_targ_mem(il, base + 11 * sizeof(u32));
  3099. hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
  3100. IL_ERR("Desc Time "
  3101. "data1 data2 line\n");
  3102. IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
  3103. il4965_desc_lookup(desc), desc, time, data1, data2, line);
  3104. IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
  3105. IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n",
  3106. pc, blink1, blink2, ilink1, ilink2, hcmd);
  3107. }
  3108. static void il4965_rf_kill_ct_config(struct il_priv *il)
  3109. {
  3110. struct il_ct_kill_config cmd;
  3111. unsigned long flags;
  3112. int ret = 0;
  3113. spin_lock_irqsave(&il->lock, flags);
  3114. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  3115. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  3116. spin_unlock_irqrestore(&il->lock, flags);
  3117. cmd.critical_temperature_R =
  3118. cpu_to_le32(il->hw_params.ct_kill_threshold);
  3119. ret = il_send_cmd_pdu(il, REPLY_CT_KILL_CONFIG_CMD,
  3120. sizeof(cmd), &cmd);
  3121. if (ret)
  3122. IL_ERR("REPLY_CT_KILL_CONFIG_CMD failed\n");
  3123. else
  3124. D_INFO("REPLY_CT_KILL_CONFIG_CMD "
  3125. "succeeded, "
  3126. "critical temperature is %d\n",
  3127. il->hw_params.ct_kill_threshold);
  3128. }
  3129. static const s8 default_queue_to_tx_fifo[] = {
  3130. IL_TX_FIFO_VO,
  3131. IL_TX_FIFO_VI,
  3132. IL_TX_FIFO_BE,
  3133. IL_TX_FIFO_BK,
  3134. IL49_CMD_FIFO_NUM,
  3135. IL_TX_FIFO_UNUSED,
  3136. IL_TX_FIFO_UNUSED,
  3137. };
  3138. static int il4965_alive_notify(struct il_priv *il)
  3139. {
  3140. u32 a;
  3141. unsigned long flags;
  3142. int i, chan;
  3143. u32 reg_val;
  3144. spin_lock_irqsave(&il->lock, flags);
  3145. /* Clear 4965's internal Tx Scheduler data base */
  3146. il->scd_base_addr = il_rd_prph(il,
  3147. IL49_SCD_SRAM_BASE_ADDR);
  3148. a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
  3149. for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  3150. il_write_targ_mem(il, a, 0);
  3151. for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
  3152. il_write_targ_mem(il, a, 0);
  3153. for (; a < il->scd_base_addr +
  3154. IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num); a += 4)
  3155. il_write_targ_mem(il, a, 0);
  3156. /* Tel 4965 where to find Tx byte count tables */
  3157. il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR,
  3158. il->scd_bc_tbls.dma >> 10);
  3159. /* Enable DMA channel */
  3160. for (chan = 0; chan < FH49_TCSR_CHNL_NUM ; chan++)
  3161. il_wr(il,
  3162. FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  3163. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  3164. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  3165. /* Update FH chicken bits */
  3166. reg_val = il_rd(il, FH_TX_CHICKEN_BITS_REG);
  3167. il_wr(il, FH_TX_CHICKEN_BITS_REG,
  3168. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  3169. /* Disable chain mode for all queues */
  3170. il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
  3171. /* Initialize each Tx queue (including the command queue) */
  3172. for (i = 0; i < il->hw_params.max_txq_num; i++) {
  3173. /* TFD circular buffer read/write idxes */
  3174. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
  3175. il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
  3176. /* Max Tx Window size for Scheduler-ACK mode */
  3177. il_write_targ_mem(il, il->scd_base_addr +
  3178. IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
  3179. (SCD_WIN_SIZE <<
  3180. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  3181. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  3182. /* Frame limit */
  3183. il_write_targ_mem(il, il->scd_base_addr +
  3184. IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
  3185. sizeof(u32),
  3186. (SCD_FRAME_LIMIT <<
  3187. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  3188. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  3189. }
  3190. il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
  3191. (1 << il->hw_params.max_txq_num) - 1);
  3192. /* Activate all Tx DMA/FIFO channels */
  3193. il4965_txq_set_sched(il, IL_MASK(0, 6));
  3194. il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
  3195. /* make sure all queue are not stopped */
  3196. memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
  3197. for (i = 0; i < 4; i++)
  3198. atomic_set(&il->queue_stop_count[i], 0);
  3199. /* reset to 0 to enable all the queue first */
  3200. il->txq_ctx_active_msk = 0;
  3201. /* Map each Tx/cmd queue to its corresponding fifo */
  3202. BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
  3203. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  3204. int ac = default_queue_to_tx_fifo[i];
  3205. il_txq_ctx_activate(il, i);
  3206. if (ac == IL_TX_FIFO_UNUSED)
  3207. continue;
  3208. il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
  3209. }
  3210. spin_unlock_irqrestore(&il->lock, flags);
  3211. return 0;
  3212. }
  3213. /**
  3214. * il4965_alive_start - called after REPLY_ALIVE notification received
  3215. * from protocol/runtime uCode (initialization uCode's
  3216. * Alive gets handled by il_init_alive_start()).
  3217. */
  3218. static void il4965_alive_start(struct il_priv *il)
  3219. {
  3220. int ret = 0;
  3221. struct il_rxon_context *ctx = &il->ctx;
  3222. D_INFO("Runtime Alive received.\n");
  3223. if (il->card_alive.is_valid != UCODE_VALID_OK) {
  3224. /* We had an error bringing up the hardware, so take it
  3225. * all the way back down so we can try again */
  3226. D_INFO("Alive failed.\n");
  3227. goto restart;
  3228. }
  3229. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  3230. * This is a paranoid check, because we would not have gotten the
  3231. * "runtime" alive if code weren't properly loaded. */
  3232. if (il4965_verify_ucode(il)) {
  3233. /* Runtime instruction load was bad;
  3234. * take it all the way back down so we can try again */
  3235. D_INFO("Bad runtime uCode load.\n");
  3236. goto restart;
  3237. }
  3238. ret = il4965_alive_notify(il);
  3239. if (ret) {
  3240. IL_WARN(
  3241. "Could not complete ALIVE transition [ntf]: %d\n", ret);
  3242. goto restart;
  3243. }
  3244. /* After the ALIVE response, we can send host commands to the uCode */
  3245. set_bit(STATUS_ALIVE, &il->status);
  3246. /* Enable watchdog to monitor the driver tx queues */
  3247. il_setup_watchdog(il);
  3248. if (il_is_rfkill(il))
  3249. return;
  3250. ieee80211_wake_queues(il->hw);
  3251. il->active_rate = RATES_MASK;
  3252. if (il_is_associated_ctx(ctx)) {
  3253. struct il_rxon_cmd *active_rxon =
  3254. (struct il_rxon_cmd *)&ctx->active;
  3255. /* apply any changes in staging */
  3256. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  3257. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3258. } else {
  3259. /* Initialize our rx_config data */
  3260. il_connection_init_rx_config(il, &il->ctx);
  3261. if (il->cfg->ops->hcmd->set_rxon_chain)
  3262. il->cfg->ops->hcmd->set_rxon_chain(il, ctx);
  3263. }
  3264. /* Configure bluetooth coexistence if enabled */
  3265. il_send_bt_config(il);
  3266. il4965_reset_run_time_calib(il);
  3267. set_bit(STATUS_READY, &il->status);
  3268. /* Configure the adapter for unassociated operation */
  3269. il_commit_rxon(il, ctx);
  3270. /* At this point, the NIC is initialized and operational */
  3271. il4965_rf_kill_ct_config(il);
  3272. D_INFO("ALIVE processing complete.\n");
  3273. wake_up(&il->wait_command_queue);
  3274. il_power_update_mode(il, true);
  3275. D_INFO("Updated power mode\n");
  3276. return;
  3277. restart:
  3278. queue_work(il->workqueue, &il->restart);
  3279. }
  3280. static void il4965_cancel_deferred_work(struct il_priv *il);
  3281. static void __il4965_down(struct il_priv *il)
  3282. {
  3283. unsigned long flags;
  3284. int exit_pending;
  3285. D_INFO(DRV_NAME " is going down\n");
  3286. il_scan_cancel_timeout(il, 200);
  3287. exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &il->status);
  3288. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  3289. * to prevent rearm timer */
  3290. del_timer_sync(&il->watchdog);
  3291. il_clear_ucode_stations(il, NULL);
  3292. il_dealloc_bcast_stations(il);
  3293. il_clear_driver_stations(il);
  3294. /* Unblock any waiting calls */
  3295. wake_up_all(&il->wait_command_queue);
  3296. /* Wipe out the EXIT_PENDING status bit if we are not actually
  3297. * exiting the module */
  3298. if (!exit_pending)
  3299. clear_bit(STATUS_EXIT_PENDING, &il->status);
  3300. /* stop and reset the on-board processor */
  3301. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  3302. /* tell the device to stop sending interrupts */
  3303. spin_lock_irqsave(&il->lock, flags);
  3304. il_disable_interrupts(il);
  3305. spin_unlock_irqrestore(&il->lock, flags);
  3306. il4965_synchronize_irq(il);
  3307. if (il->mac80211_registered)
  3308. ieee80211_stop_queues(il->hw);
  3309. /* If we have not previously called il_init() then
  3310. * clear all bits but the RF Kill bit and return */
  3311. if (!il_is_init(il)) {
  3312. il->status = test_bit(STATUS_RF_KILL_HW, &il->status) <<
  3313. STATUS_RF_KILL_HW |
  3314. test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
  3315. STATUS_GEO_CONFIGURED |
  3316. test_bit(STATUS_EXIT_PENDING, &il->status) <<
  3317. STATUS_EXIT_PENDING;
  3318. goto exit;
  3319. }
  3320. /* ...otherwise clear out all the status bits but the RF Kill
  3321. * bit and continue taking the NIC down. */
  3322. il->status &= test_bit(STATUS_RF_KILL_HW, &il->status) <<
  3323. STATUS_RF_KILL_HW |
  3324. test_bit(STATUS_GEO_CONFIGURED, &il->status) <<
  3325. STATUS_GEO_CONFIGURED |
  3326. test_bit(STATUS_FW_ERROR, &il->status) <<
  3327. STATUS_FW_ERROR |
  3328. test_bit(STATUS_EXIT_PENDING, &il->status) <<
  3329. STATUS_EXIT_PENDING;
  3330. il4965_txq_ctx_stop(il);
  3331. il4965_rxq_stop(il);
  3332. /* Power-down device's busmaster DMA clocks */
  3333. il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  3334. udelay(5);
  3335. /* Make sure (redundant) we've released our request to stay awake */
  3336. il_clear_bit(il, CSR_GP_CNTRL,
  3337. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  3338. /* Stop the device, and put it in low power state */
  3339. il_apm_stop(il);
  3340. exit:
  3341. memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
  3342. dev_kfree_skb(il->beacon_skb);
  3343. il->beacon_skb = NULL;
  3344. /* clear out any free frames */
  3345. il4965_clear_free_frames(il);
  3346. }
  3347. static void il4965_down(struct il_priv *il)
  3348. {
  3349. mutex_lock(&il->mutex);
  3350. __il4965_down(il);
  3351. mutex_unlock(&il->mutex);
  3352. il4965_cancel_deferred_work(il);
  3353. }
  3354. #define HW_READY_TIMEOUT (50)
  3355. static int il4965_set_hw_ready(struct il_priv *il)
  3356. {
  3357. int ret = 0;
  3358. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  3359. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  3360. /* See if we got it */
  3361. ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  3362. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  3363. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  3364. HW_READY_TIMEOUT);
  3365. if (ret != -ETIMEDOUT)
  3366. il->hw_ready = true;
  3367. else
  3368. il->hw_ready = false;
  3369. D_INFO("hardware %s\n",
  3370. (il->hw_ready == 1) ? "ready" : "not ready");
  3371. return ret;
  3372. }
  3373. static int il4965_prepare_card_hw(struct il_priv *il)
  3374. {
  3375. int ret = 0;
  3376. D_INFO("il4965_prepare_card_hw enter\n");
  3377. ret = il4965_set_hw_ready(il);
  3378. if (il->hw_ready)
  3379. return ret;
  3380. /* If HW is not ready, prepare the conditions to check again */
  3381. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  3382. CSR_HW_IF_CONFIG_REG_PREPARE);
  3383. ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  3384. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  3385. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  3386. /* HW should be ready by now, check again. */
  3387. if (ret != -ETIMEDOUT)
  3388. il4965_set_hw_ready(il);
  3389. return ret;
  3390. }
  3391. #define MAX_HW_RESTARTS 5
  3392. static int __il4965_up(struct il_priv *il)
  3393. {
  3394. int i;
  3395. int ret;
  3396. if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
  3397. IL_WARN("Exit pending; will not bring the NIC up\n");
  3398. return -EIO;
  3399. }
  3400. if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
  3401. IL_ERR("ucode not available for device bringup\n");
  3402. return -EIO;
  3403. }
  3404. ret = il4965_alloc_bcast_station(il, &il->ctx);
  3405. if (ret) {
  3406. il_dealloc_bcast_stations(il);
  3407. return ret;
  3408. }
  3409. il4965_prepare_card_hw(il);
  3410. if (!il->hw_ready) {
  3411. IL_WARN("Exit HW not ready\n");
  3412. return -EIO;
  3413. }
  3414. /* If platform's RF_KILL switch is NOT set to KILL */
  3415. if (_il_rd(il,
  3416. CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  3417. clear_bit(STATUS_RF_KILL_HW, &il->status);
  3418. else
  3419. set_bit(STATUS_RF_KILL_HW, &il->status);
  3420. if (il_is_rfkill(il)) {
  3421. wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
  3422. il_enable_interrupts(il);
  3423. IL_WARN("Radio disabled by HW RF Kill switch\n");
  3424. return 0;
  3425. }
  3426. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  3427. /* must be initialised before il_hw_nic_init */
  3428. il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
  3429. ret = il4965_hw_nic_init(il);
  3430. if (ret) {
  3431. IL_ERR("Unable to init nic\n");
  3432. return ret;
  3433. }
  3434. /* make sure rfkill handshake bits are cleared */
  3435. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3436. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  3437. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3438. /* clear (again), then enable host interrupts */
  3439. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  3440. il_enable_interrupts(il);
  3441. /* really make sure rfkill handshake bits are cleared */
  3442. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3443. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  3444. /* Copy original ucode data image from disk into backup cache.
  3445. * This will be used to initialize the on-board processor's
  3446. * data SRAM for a clean start when the runtime program first loads. */
  3447. memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
  3448. il->ucode_data.len);
  3449. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  3450. /* load bootstrap state machine,
  3451. * load bootstrap program into processor's memory,
  3452. * prepare to load the "initialize" uCode */
  3453. ret = il->cfg->ops->lib->load_ucode(il);
  3454. if (ret) {
  3455. IL_ERR("Unable to set up bootstrap uCode: %d\n",
  3456. ret);
  3457. continue;
  3458. }
  3459. /* start card; "initialize" will load runtime ucode */
  3460. il4965_nic_start(il);
  3461. D_INFO(DRV_NAME " is coming up\n");
  3462. return 0;
  3463. }
  3464. set_bit(STATUS_EXIT_PENDING, &il->status);
  3465. __il4965_down(il);
  3466. clear_bit(STATUS_EXIT_PENDING, &il->status);
  3467. /* tried to restart and config the device for as long as our
  3468. * patience could withstand */
  3469. IL_ERR("Unable to initialize device after %d attempts.\n", i);
  3470. return -EIO;
  3471. }
  3472. /*****************************************************************************
  3473. *
  3474. * Workqueue callbacks
  3475. *
  3476. *****************************************************************************/
  3477. static void il4965_bg_init_alive_start(struct work_struct *data)
  3478. {
  3479. struct il_priv *il =
  3480. container_of(data, struct il_priv, init_alive_start.work);
  3481. mutex_lock(&il->mutex);
  3482. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3483. goto out;
  3484. il->cfg->ops->lib->init_alive_start(il);
  3485. out:
  3486. mutex_unlock(&il->mutex);
  3487. }
  3488. static void il4965_bg_alive_start(struct work_struct *data)
  3489. {
  3490. struct il_priv *il =
  3491. container_of(data, struct il_priv, alive_start.work);
  3492. mutex_lock(&il->mutex);
  3493. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3494. goto out;
  3495. il4965_alive_start(il);
  3496. out:
  3497. mutex_unlock(&il->mutex);
  3498. }
  3499. static void il4965_bg_run_time_calib_work(struct work_struct *work)
  3500. {
  3501. struct il_priv *il = container_of(work, struct il_priv,
  3502. run_time_calib_work);
  3503. mutex_lock(&il->mutex);
  3504. if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
  3505. test_bit(STATUS_SCANNING, &il->status)) {
  3506. mutex_unlock(&il->mutex);
  3507. return;
  3508. }
  3509. if (il->start_calib) {
  3510. il4965_chain_noise_calibration(il,
  3511. (void *)&il->_4965.stats);
  3512. il4965_sensitivity_calibration(il,
  3513. (void *)&il->_4965.stats);
  3514. }
  3515. mutex_unlock(&il->mutex);
  3516. }
  3517. static void il4965_bg_restart(struct work_struct *data)
  3518. {
  3519. struct il_priv *il = container_of(data, struct il_priv, restart);
  3520. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3521. return;
  3522. if (test_and_clear_bit(STATUS_FW_ERROR, &il->status)) {
  3523. mutex_lock(&il->mutex);
  3524. il->ctx.vif = NULL;
  3525. il->is_open = 0;
  3526. __il4965_down(il);
  3527. mutex_unlock(&il->mutex);
  3528. il4965_cancel_deferred_work(il);
  3529. ieee80211_restart_hw(il->hw);
  3530. } else {
  3531. il4965_down(il);
  3532. mutex_lock(&il->mutex);
  3533. if (test_bit(STATUS_EXIT_PENDING, &il->status)) {
  3534. mutex_unlock(&il->mutex);
  3535. return;
  3536. }
  3537. __il4965_up(il);
  3538. mutex_unlock(&il->mutex);
  3539. }
  3540. }
  3541. static void il4965_bg_rx_replenish(struct work_struct *data)
  3542. {
  3543. struct il_priv *il =
  3544. container_of(data, struct il_priv, rx_replenish);
  3545. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3546. return;
  3547. mutex_lock(&il->mutex);
  3548. il4965_rx_replenish(il);
  3549. mutex_unlock(&il->mutex);
  3550. }
  3551. /*****************************************************************************
  3552. *
  3553. * mac80211 entry point functions
  3554. *
  3555. *****************************************************************************/
  3556. #define UCODE_READY_TIMEOUT (4 * HZ)
  3557. /*
  3558. * Not a mac80211 entry point function, but it fits in with all the
  3559. * other mac80211 functions grouped here.
  3560. */
  3561. static int il4965_mac_setup_register(struct il_priv *il,
  3562. u32 max_probe_length)
  3563. {
  3564. int ret;
  3565. struct ieee80211_hw *hw = il->hw;
  3566. hw->rate_control_algorithm = "iwl-4965-rs";
  3567. /* Tell mac80211 our characteristics */
  3568. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3569. IEEE80211_HW_AMPDU_AGGREGATION |
  3570. IEEE80211_HW_NEED_DTIM_PERIOD |
  3571. IEEE80211_HW_SPECTRUM_MGMT |
  3572. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  3573. if (il->cfg->sku & IL_SKU_N)
  3574. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  3575. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  3576. hw->sta_data_size = sizeof(struct il_station_priv);
  3577. hw->vif_data_size = sizeof(struct il_vif_priv);
  3578. hw->wiphy->interface_modes |= il->ctx.interface_modes;
  3579. hw->wiphy->interface_modes |= il->ctx.exclusive_interface_modes;
  3580. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  3581. WIPHY_FLAG_DISABLE_BEACON_HINTS;
  3582. /*
  3583. * For now, disable PS by default because it affects
  3584. * RX performance significantly.
  3585. */
  3586. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  3587. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  3588. /* we create the 802.11 header and a zero-length SSID element */
  3589. hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
  3590. /* Default value; 4 EDCA QOS priorities */
  3591. hw->queues = 4;
  3592. hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
  3593. if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
  3594. il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3595. &il->bands[IEEE80211_BAND_2GHZ];
  3596. if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
  3597. il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3598. &il->bands[IEEE80211_BAND_5GHZ];
  3599. il_leds_init(il);
  3600. ret = ieee80211_register_hw(il->hw);
  3601. if (ret) {
  3602. IL_ERR("Failed to register hw (error %d)\n", ret);
  3603. return ret;
  3604. }
  3605. il->mac80211_registered = 1;
  3606. return 0;
  3607. }
  3608. int il4965_mac_start(struct ieee80211_hw *hw)
  3609. {
  3610. struct il_priv *il = hw->priv;
  3611. int ret;
  3612. D_MAC80211("enter\n");
  3613. /* we should be verifying the device is ready to be opened */
  3614. mutex_lock(&il->mutex);
  3615. ret = __il4965_up(il);
  3616. mutex_unlock(&il->mutex);
  3617. if (ret)
  3618. return ret;
  3619. if (il_is_rfkill(il))
  3620. goto out;
  3621. D_INFO("Start UP work done.\n");
  3622. /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
  3623. * mac80211 will not be run successfully. */
  3624. ret = wait_event_timeout(il->wait_command_queue,
  3625. test_bit(STATUS_READY, &il->status),
  3626. UCODE_READY_TIMEOUT);
  3627. if (!ret) {
  3628. if (!test_bit(STATUS_READY, &il->status)) {
  3629. IL_ERR("START_ALIVE timeout after %dms.\n",
  3630. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  3631. return -ETIMEDOUT;
  3632. }
  3633. }
  3634. il4965_led_enable(il);
  3635. out:
  3636. il->is_open = 1;
  3637. D_MAC80211("leave\n");
  3638. return 0;
  3639. }
  3640. void il4965_mac_stop(struct ieee80211_hw *hw)
  3641. {
  3642. struct il_priv *il = hw->priv;
  3643. D_MAC80211("enter\n");
  3644. if (!il->is_open)
  3645. return;
  3646. il->is_open = 0;
  3647. il4965_down(il);
  3648. flush_workqueue(il->workqueue);
  3649. /* User space software may expect getting rfkill changes
  3650. * even if interface is down */
  3651. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  3652. il_enable_rfkill_int(il);
  3653. D_MAC80211("leave\n");
  3654. }
  3655. void il4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  3656. {
  3657. struct il_priv *il = hw->priv;
  3658. D_MACDUMP("enter\n");
  3659. D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  3660. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  3661. if (il4965_tx_skb(il, skb))
  3662. dev_kfree_skb_any(skb);
  3663. D_MACDUMP("leave\n");
  3664. }
  3665. void il4965_mac_update_tkip_key(struct ieee80211_hw *hw,
  3666. struct ieee80211_vif *vif,
  3667. struct ieee80211_key_conf *keyconf,
  3668. struct ieee80211_sta *sta,
  3669. u32 iv32, u16 *phase1key)
  3670. {
  3671. struct il_priv *il = hw->priv;
  3672. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  3673. D_MAC80211("enter\n");
  3674. il4965_update_tkip_key(il, vif_priv->ctx, keyconf, sta,
  3675. iv32, phase1key);
  3676. D_MAC80211("leave\n");
  3677. }
  3678. int il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3679. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  3680. struct ieee80211_key_conf *key)
  3681. {
  3682. struct il_priv *il = hw->priv;
  3683. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  3684. struct il_rxon_context *ctx = vif_priv->ctx;
  3685. int ret;
  3686. u8 sta_id;
  3687. bool is_default_wep_key = false;
  3688. D_MAC80211("enter\n");
  3689. if (il->cfg->mod_params->sw_crypto) {
  3690. D_MAC80211("leave - hwcrypto disabled\n");
  3691. return -EOPNOTSUPP;
  3692. }
  3693. sta_id = il_sta_id_or_broadcast(il, vif_priv->ctx, sta);
  3694. if (sta_id == IL_INVALID_STATION)
  3695. return -EINVAL;
  3696. mutex_lock(&il->mutex);
  3697. il_scan_cancel_timeout(il, 100);
  3698. /*
  3699. * If we are getting WEP group key and we didn't receive any key mapping
  3700. * so far, we are in legacy wep mode (group key only), otherwise we are
  3701. * in 1X mode.
  3702. * In legacy wep mode, we use another host command to the uCode.
  3703. */
  3704. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  3705. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  3706. !sta) {
  3707. if (cmd == SET_KEY)
  3708. is_default_wep_key = !ctx->key_mapping_keys;
  3709. else
  3710. is_default_wep_key =
  3711. (key->hw_key_idx == HW_KEY_DEFAULT);
  3712. }
  3713. switch (cmd) {
  3714. case SET_KEY:
  3715. if (is_default_wep_key)
  3716. ret = il4965_set_default_wep_key(il,
  3717. vif_priv->ctx, key);
  3718. else
  3719. ret = il4965_set_dynamic_key(il, vif_priv->ctx,
  3720. key, sta_id);
  3721. D_MAC80211("enable hwcrypto key\n");
  3722. break;
  3723. case DISABLE_KEY:
  3724. if (is_default_wep_key)
  3725. ret = il4965_remove_default_wep_key(il, ctx, key);
  3726. else
  3727. ret = il4965_remove_dynamic_key(il, ctx,
  3728. key, sta_id);
  3729. D_MAC80211("disable hwcrypto key\n");
  3730. break;
  3731. default:
  3732. ret = -EINVAL;
  3733. }
  3734. mutex_unlock(&il->mutex);
  3735. D_MAC80211("leave\n");
  3736. return ret;
  3737. }
  3738. int il4965_mac_ampdu_action(struct ieee80211_hw *hw,
  3739. struct ieee80211_vif *vif,
  3740. enum ieee80211_ampdu_mlme_action action,
  3741. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  3742. u8 buf_size)
  3743. {
  3744. struct il_priv *il = hw->priv;
  3745. int ret = -EINVAL;
  3746. D_HT("A-MPDU action on addr %pM tid %d\n",
  3747. sta->addr, tid);
  3748. if (!(il->cfg->sku & IL_SKU_N))
  3749. return -EACCES;
  3750. mutex_lock(&il->mutex);
  3751. switch (action) {
  3752. case IEEE80211_AMPDU_RX_START:
  3753. D_HT("start Rx\n");
  3754. ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
  3755. break;
  3756. case IEEE80211_AMPDU_RX_STOP:
  3757. D_HT("stop Rx\n");
  3758. ret = il4965_sta_rx_agg_stop(il, sta, tid);
  3759. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3760. ret = 0;
  3761. break;
  3762. case IEEE80211_AMPDU_TX_START:
  3763. D_HT("start Tx\n");
  3764. ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
  3765. break;
  3766. case IEEE80211_AMPDU_TX_STOP:
  3767. D_HT("stop Tx\n");
  3768. ret = il4965_tx_agg_stop(il, vif, sta, tid);
  3769. if (test_bit(STATUS_EXIT_PENDING, &il->status))
  3770. ret = 0;
  3771. break;
  3772. case IEEE80211_AMPDU_TX_OPERATIONAL:
  3773. ret = 0;
  3774. break;
  3775. }
  3776. mutex_unlock(&il->mutex);
  3777. return ret;
  3778. }
  3779. int il4965_mac_sta_add(struct ieee80211_hw *hw,
  3780. struct ieee80211_vif *vif,
  3781. struct ieee80211_sta *sta)
  3782. {
  3783. struct il_priv *il = hw->priv;
  3784. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  3785. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  3786. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  3787. int ret;
  3788. u8 sta_id;
  3789. D_INFO("received request to add station %pM\n",
  3790. sta->addr);
  3791. mutex_lock(&il->mutex);
  3792. D_INFO("proceeding to add station %pM\n",
  3793. sta->addr);
  3794. sta_priv->common.sta_id = IL_INVALID_STATION;
  3795. atomic_set(&sta_priv->pending_frames, 0);
  3796. ret = il_add_station_common(il, vif_priv->ctx, sta->addr,
  3797. is_ap, sta, &sta_id);
  3798. if (ret) {
  3799. IL_ERR("Unable to add station %pM (%d)\n",
  3800. sta->addr, ret);
  3801. /* Should we return success if return code is EEXIST ? */
  3802. mutex_unlock(&il->mutex);
  3803. return ret;
  3804. }
  3805. sta_priv->common.sta_id = sta_id;
  3806. /* Initialize rate scaling */
  3807. D_INFO("Initializing rate scaling for station %pM\n",
  3808. sta->addr);
  3809. il4965_rs_rate_init(il, sta, sta_id);
  3810. mutex_unlock(&il->mutex);
  3811. return 0;
  3812. }
  3813. void il4965_mac_channel_switch(struct ieee80211_hw *hw,
  3814. struct ieee80211_channel_switch *ch_switch)
  3815. {
  3816. struct il_priv *il = hw->priv;
  3817. const struct il_channel_info *ch_info;
  3818. struct ieee80211_conf *conf = &hw->conf;
  3819. struct ieee80211_channel *channel = ch_switch->channel;
  3820. struct il_ht_config *ht_conf = &il->current_ht_config;
  3821. struct il_rxon_context *ctx = &il->ctx;
  3822. u16 ch;
  3823. D_MAC80211("enter\n");
  3824. mutex_lock(&il->mutex);
  3825. if (il_is_rfkill(il))
  3826. goto out;
  3827. if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
  3828. test_bit(STATUS_SCANNING, &il->status) ||
  3829. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status))
  3830. goto out;
  3831. if (!il_is_associated_ctx(ctx))
  3832. goto out;
  3833. if (!il->cfg->ops->lib->set_channel_switch)
  3834. goto out;
  3835. ch = channel->hw_value;
  3836. if (le16_to_cpu(ctx->active.channel) == ch)
  3837. goto out;
  3838. ch_info = il_get_channel_info(il, channel->band, ch);
  3839. if (!il_is_channel_valid(ch_info)) {
  3840. D_MAC80211("invalid channel\n");
  3841. goto out;
  3842. }
  3843. spin_lock_irq(&il->lock);
  3844. il->current_ht_config.smps = conf->smps_mode;
  3845. /* Configure HT40 channels */
  3846. ctx->ht.enabled = conf_is_ht(conf);
  3847. if (ctx->ht.enabled) {
  3848. if (conf_is_ht40_minus(conf)) {
  3849. ctx->ht.extension_chan_offset =
  3850. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  3851. ctx->ht.is_40mhz = true;
  3852. } else if (conf_is_ht40_plus(conf)) {
  3853. ctx->ht.extension_chan_offset =
  3854. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  3855. ctx->ht.is_40mhz = true;
  3856. } else {
  3857. ctx->ht.extension_chan_offset =
  3858. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  3859. ctx->ht.is_40mhz = false;
  3860. }
  3861. } else
  3862. ctx->ht.is_40mhz = false;
  3863. if ((le16_to_cpu(ctx->staging.channel) != ch))
  3864. ctx->staging.flags = 0;
  3865. il_set_rxon_channel(il, channel, ctx);
  3866. il_set_rxon_ht(il, ht_conf);
  3867. il_set_flags_for_band(il, ctx, channel->band, ctx->vif);
  3868. spin_unlock_irq(&il->lock);
  3869. il_set_rate(il);
  3870. /*
  3871. * at this point, staging_rxon has the
  3872. * configuration for channel switch
  3873. */
  3874. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
  3875. il->switch_channel = cpu_to_le16(ch);
  3876. if (il->cfg->ops->lib->set_channel_switch(il, ch_switch)) {
  3877. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &il->status);
  3878. il->switch_channel = 0;
  3879. ieee80211_chswitch_done(ctx->vif, false);
  3880. }
  3881. out:
  3882. mutex_unlock(&il->mutex);
  3883. D_MAC80211("leave\n");
  3884. }
  3885. void il4965_configure_filter(struct ieee80211_hw *hw,
  3886. unsigned int changed_flags,
  3887. unsigned int *total_flags,
  3888. u64 multicast)
  3889. {
  3890. struct il_priv *il = hw->priv;
  3891. __le32 filter_or = 0, filter_nand = 0;
  3892. #define CHK(test, flag) do { \
  3893. if (*total_flags & (test)) \
  3894. filter_or |= (flag); \
  3895. else \
  3896. filter_nand |= (flag); \
  3897. } while (0)
  3898. D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
  3899. changed_flags, *total_flags);
  3900. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  3901. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  3902. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  3903. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  3904. #undef CHK
  3905. mutex_lock(&il->mutex);
  3906. il->ctx.staging.filter_flags &= ~filter_nand;
  3907. il->ctx.staging.filter_flags |= filter_or;
  3908. /*
  3909. * Not committing directly because hardware can perform a scan,
  3910. * but we'll eventually commit the filter flags change anyway.
  3911. */
  3912. mutex_unlock(&il->mutex);
  3913. /*
  3914. * Receiving all multicast frames is always enabled by the
  3915. * default flags setup in il_connection_init_rx_config()
  3916. * since we currently do not support programming multicast
  3917. * filters into the device.
  3918. */
  3919. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  3920. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  3921. }
  3922. /*****************************************************************************
  3923. *
  3924. * driver setup and teardown
  3925. *
  3926. *****************************************************************************/
  3927. static void il4965_bg_txpower_work(struct work_struct *work)
  3928. {
  3929. struct il_priv *il = container_of(work, struct il_priv,
  3930. txpower_work);
  3931. mutex_lock(&il->mutex);
  3932. /* If a scan happened to start before we got here
  3933. * then just return; the stats notification will
  3934. * kick off another scheduled work to compensate for
  3935. * any temperature delta we missed here. */
  3936. if (test_bit(STATUS_EXIT_PENDING, &il->status) ||
  3937. test_bit(STATUS_SCANNING, &il->status))
  3938. goto out;
  3939. /* Regardless of if we are associated, we must reconfigure the
  3940. * TX power since frames can be sent on non-radar channels while
  3941. * not associated */
  3942. il->cfg->ops->lib->send_tx_power(il);
  3943. /* Update last_temperature to keep is_calib_needed from running
  3944. * when it isn't needed... */
  3945. il->last_temperature = il->temperature;
  3946. out:
  3947. mutex_unlock(&il->mutex);
  3948. }
  3949. static void il4965_setup_deferred_work(struct il_priv *il)
  3950. {
  3951. il->workqueue = create_singlethread_workqueue(DRV_NAME);
  3952. init_waitqueue_head(&il->wait_command_queue);
  3953. INIT_WORK(&il->restart, il4965_bg_restart);
  3954. INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
  3955. INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
  3956. INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
  3957. INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
  3958. il_setup_scan_deferred_work(il);
  3959. INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
  3960. init_timer(&il->stats_periodic);
  3961. il->stats_periodic.data = (unsigned long)il;
  3962. il->stats_periodic.function = il4965_bg_stats_periodic;
  3963. init_timer(&il->watchdog);
  3964. il->watchdog.data = (unsigned long)il;
  3965. il->watchdog.function = il_bg_watchdog;
  3966. tasklet_init(&il->irq_tasklet, (void (*)(unsigned long))
  3967. il4965_irq_tasklet, (unsigned long)il);
  3968. }
  3969. static void il4965_cancel_deferred_work(struct il_priv *il)
  3970. {
  3971. cancel_work_sync(&il->txpower_work);
  3972. cancel_delayed_work_sync(&il->init_alive_start);
  3973. cancel_delayed_work(&il->alive_start);
  3974. cancel_work_sync(&il->run_time_calib_work);
  3975. il_cancel_scan_deferred_work(il);
  3976. del_timer_sync(&il->stats_periodic);
  3977. }
  3978. static void il4965_init_hw_rates(struct il_priv *il,
  3979. struct ieee80211_rate *rates)
  3980. {
  3981. int i;
  3982. for (i = 0; i < RATE_COUNT_LEGACY; i++) {
  3983. rates[i].bitrate = il_rates[i].ieee * 5;
  3984. rates[i].hw_value = i; /* Rate scaling will work on idxes */
  3985. rates[i].hw_value_short = i;
  3986. rates[i].flags = 0;
  3987. if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
  3988. /*
  3989. * If CCK != 1M then set short preamble rate flag.
  3990. */
  3991. rates[i].flags |=
  3992. (il_rates[i].plcp == RATE_1M_PLCP) ?
  3993. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  3994. }
  3995. }
  3996. }
  3997. /*
  3998. * Acquire il->lock before calling this function !
  3999. */
  4000. void il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
  4001. {
  4002. il_wr(il, HBUS_TARG_WRPTR,
  4003. (idx & 0xff) | (txq_id << 8));
  4004. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
  4005. }
  4006. void il4965_tx_queue_set_status(struct il_priv *il,
  4007. struct il_tx_queue *txq,
  4008. int tx_fifo_id, int scd_retry)
  4009. {
  4010. int txq_id = txq->q.id;
  4011. /* Find out whether to activate Tx queue */
  4012. int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
  4013. /* Set up and activate */
  4014. il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  4015. (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  4016. (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
  4017. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
  4018. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  4019. IL49_SCD_QUEUE_STTS_REG_MSK);
  4020. txq->sched_retry = scd_retry;
  4021. D_INFO("%s %s Queue %d on AC %d\n",
  4022. active ? "Activate" : "Deactivate",
  4023. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  4024. }
  4025. static int il4965_init_drv(struct il_priv *il)
  4026. {
  4027. int ret;
  4028. spin_lock_init(&il->sta_lock);
  4029. spin_lock_init(&il->hcmd_lock);
  4030. INIT_LIST_HEAD(&il->free_frames);
  4031. mutex_init(&il->mutex);
  4032. il->ieee_channels = NULL;
  4033. il->ieee_rates = NULL;
  4034. il->band = IEEE80211_BAND_2GHZ;
  4035. il->iw_mode = NL80211_IFTYPE_STATION;
  4036. il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  4037. il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
  4038. /* initialize force reset */
  4039. il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
  4040. /* Choose which receivers/antennas to use */
  4041. if (il->cfg->ops->hcmd->set_rxon_chain)
  4042. il->cfg->ops->hcmd->set_rxon_chain(il,
  4043. &il->ctx);
  4044. il_init_scan_params(il);
  4045. ret = il_init_channel_map(il);
  4046. if (ret) {
  4047. IL_ERR("initializing regulatory failed: %d\n", ret);
  4048. goto err;
  4049. }
  4050. ret = il_init_geos(il);
  4051. if (ret) {
  4052. IL_ERR("initializing geos failed: %d\n", ret);
  4053. goto err_free_channel_map;
  4054. }
  4055. il4965_init_hw_rates(il, il->ieee_rates);
  4056. return 0;
  4057. err_free_channel_map:
  4058. il_free_channel_map(il);
  4059. err:
  4060. return ret;
  4061. }
  4062. static void il4965_uninit_drv(struct il_priv *il)
  4063. {
  4064. il4965_calib_free_results(il);
  4065. il_free_geos(il);
  4066. il_free_channel_map(il);
  4067. kfree(il->scan_cmd);
  4068. }
  4069. static void il4965_hw_detect(struct il_priv *il)
  4070. {
  4071. il->hw_rev = _il_rd(il, CSR_HW_REV);
  4072. il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
  4073. il->rev_id = il->pci_dev->revision;
  4074. D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
  4075. }
  4076. static int il4965_set_hw_params(struct il_priv *il)
  4077. {
  4078. il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  4079. il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  4080. if (il->cfg->mod_params->amsdu_size_8K)
  4081. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
  4082. else
  4083. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
  4084. il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
  4085. if (il->cfg->mod_params->disable_11n)
  4086. il->cfg->sku &= ~IL_SKU_N;
  4087. /* Device-specific setup */
  4088. return il->cfg->ops->lib->set_hw_params(il);
  4089. }
  4090. static const u8 il4965_bss_ac_to_fifo[] = {
  4091. IL_TX_FIFO_VO,
  4092. IL_TX_FIFO_VI,
  4093. IL_TX_FIFO_BE,
  4094. IL_TX_FIFO_BK,
  4095. };
  4096. static const u8 il4965_bss_ac_to_queue[] = {
  4097. 0, 1, 2, 3,
  4098. };
  4099. static int
  4100. il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  4101. {
  4102. int err = 0;
  4103. struct il_priv *il;
  4104. struct ieee80211_hw *hw;
  4105. struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
  4106. unsigned long flags;
  4107. u16 pci_cmd;
  4108. /************************
  4109. * 1. Allocating HW data
  4110. ************************/
  4111. hw = il_alloc_all(cfg);
  4112. if (!hw) {
  4113. err = -ENOMEM;
  4114. goto out;
  4115. }
  4116. il = hw->priv;
  4117. /* At this point both hw and il are allocated. */
  4118. il->ctx.ctxid = 0;
  4119. il->ctx.always_active = true;
  4120. il->ctx.is_active = true;
  4121. il->ctx.rxon_cmd = REPLY_RXON;
  4122. il->ctx.rxon_timing_cmd = REPLY_RXON_TIMING;
  4123. il->ctx.rxon_assoc_cmd = REPLY_RXON_ASSOC;
  4124. il->ctx.qos_cmd = REPLY_QOS_PARAM;
  4125. il->ctx.ap_sta_id = IL_AP_ID;
  4126. il->ctx.wep_key_cmd = REPLY_WEPKEY;
  4127. il->ctx.ac_to_fifo = il4965_bss_ac_to_fifo;
  4128. il->ctx.ac_to_queue = il4965_bss_ac_to_queue;
  4129. il->ctx.exclusive_interface_modes =
  4130. BIT(NL80211_IFTYPE_ADHOC);
  4131. il->ctx.interface_modes =
  4132. BIT(NL80211_IFTYPE_STATION);
  4133. il->ctx.ap_devtype = RXON_DEV_TYPE_AP;
  4134. il->ctx.ibss_devtype = RXON_DEV_TYPE_IBSS;
  4135. il->ctx.station_devtype = RXON_DEV_TYPE_ESS;
  4136. il->ctx.unused_devtype = RXON_DEV_TYPE_ESS;
  4137. SET_IEEE80211_DEV(hw, &pdev->dev);
  4138. D_INFO("*** LOAD DRIVER ***\n");
  4139. il->cfg = cfg;
  4140. il->pci_dev = pdev;
  4141. il->inta_mask = CSR_INI_SET_MASK;
  4142. if (il_alloc_traffic_mem(il))
  4143. IL_ERR("Not enough memory to generate traffic log\n");
  4144. /**************************
  4145. * 2. Initializing PCI bus
  4146. **************************/
  4147. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  4148. PCIE_LINK_STATE_CLKPM);
  4149. if (pci_enable_device(pdev)) {
  4150. err = -ENODEV;
  4151. goto out_ieee80211_free_hw;
  4152. }
  4153. pci_set_master(pdev);
  4154. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  4155. if (!err)
  4156. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  4157. if (err) {
  4158. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4159. if (!err)
  4160. err = pci_set_consistent_dma_mask(pdev,
  4161. DMA_BIT_MASK(32));
  4162. /* both attempts failed: */
  4163. if (err) {
  4164. IL_WARN("No suitable DMA available.\n");
  4165. goto out_pci_disable_device;
  4166. }
  4167. }
  4168. err = pci_request_regions(pdev, DRV_NAME);
  4169. if (err)
  4170. goto out_pci_disable_device;
  4171. pci_set_drvdata(pdev, il);
  4172. /***********************
  4173. * 3. Read REV register
  4174. ***********************/
  4175. il->hw_base = pci_iomap(pdev, 0, 0);
  4176. if (!il->hw_base) {
  4177. err = -ENODEV;
  4178. goto out_pci_release_regions;
  4179. }
  4180. D_INFO("pci_resource_len = 0x%08llx\n",
  4181. (unsigned long long) pci_resource_len(pdev, 0));
  4182. D_INFO("pci_resource_base = %p\n", il->hw_base);
  4183. /* these spin locks will be used in apm_ops.init and EEPROM access
  4184. * we should init now
  4185. */
  4186. spin_lock_init(&il->reg_lock);
  4187. spin_lock_init(&il->lock);
  4188. /*
  4189. * stop and reset the on-board processor just in case it is in a
  4190. * strange state ... like being left stranded by a primary kernel
  4191. * and this is now the kdump kernel trying to start up
  4192. */
  4193. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4194. il4965_hw_detect(il);
  4195. IL_INFO("Detected %s, REV=0x%X\n",
  4196. il->cfg->name, il->hw_rev);
  4197. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  4198. * PCI Tx retries from interfering with C3 CPU state */
  4199. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  4200. il4965_prepare_card_hw(il);
  4201. if (!il->hw_ready) {
  4202. IL_WARN("Failed, HW not ready\n");
  4203. goto out_iounmap;
  4204. }
  4205. /*****************
  4206. * 4. Read EEPROM
  4207. *****************/
  4208. /* Read the EEPROM */
  4209. err = il_eeprom_init(il);
  4210. if (err) {
  4211. IL_ERR("Unable to init EEPROM\n");
  4212. goto out_iounmap;
  4213. }
  4214. err = il4965_eeprom_check_version(il);
  4215. if (err)
  4216. goto out_free_eeprom;
  4217. if (err)
  4218. goto out_free_eeprom;
  4219. /* extract MAC Address */
  4220. il4965_eeprom_get_mac(il, il->addresses[0].addr);
  4221. D_INFO("MAC address: %pM\n", il->addresses[0].addr);
  4222. il->hw->wiphy->addresses = il->addresses;
  4223. il->hw->wiphy->n_addresses = 1;
  4224. /************************
  4225. * 5. Setup HW constants
  4226. ************************/
  4227. if (il4965_set_hw_params(il)) {
  4228. IL_ERR("failed to set hw parameters\n");
  4229. goto out_free_eeprom;
  4230. }
  4231. /*******************
  4232. * 6. Setup il
  4233. *******************/
  4234. err = il4965_init_drv(il);
  4235. if (err)
  4236. goto out_free_eeprom;
  4237. /* At this point both hw and il are initialized. */
  4238. /********************
  4239. * 7. Setup services
  4240. ********************/
  4241. spin_lock_irqsave(&il->lock, flags);
  4242. il_disable_interrupts(il);
  4243. spin_unlock_irqrestore(&il->lock, flags);
  4244. pci_enable_msi(il->pci_dev);
  4245. err = request_irq(il->pci_dev->irq, il_isr,
  4246. IRQF_SHARED, DRV_NAME, il);
  4247. if (err) {
  4248. IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
  4249. goto out_disable_msi;
  4250. }
  4251. il4965_setup_deferred_work(il);
  4252. il4965_setup_rx_handlers(il);
  4253. /*********************************************
  4254. * 8. Enable interrupts and read RFKILL state
  4255. *********************************************/
  4256. /* enable rfkill interrupt: hw bug w/a */
  4257. pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
  4258. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  4259. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  4260. pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
  4261. }
  4262. il_enable_rfkill_int(il);
  4263. /* If platform's RF_KILL switch is NOT set to KILL */
  4264. if (_il_rd(il, CSR_GP_CNTRL) &
  4265. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4266. clear_bit(STATUS_RF_KILL_HW, &il->status);
  4267. else
  4268. set_bit(STATUS_RF_KILL_HW, &il->status);
  4269. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  4270. test_bit(STATUS_RF_KILL_HW, &il->status));
  4271. il_power_initialize(il);
  4272. init_completion(&il->_4965.firmware_loading_complete);
  4273. err = il4965_request_firmware(il, true);
  4274. if (err)
  4275. goto out_destroy_workqueue;
  4276. return 0;
  4277. out_destroy_workqueue:
  4278. destroy_workqueue(il->workqueue);
  4279. il->workqueue = NULL;
  4280. free_irq(il->pci_dev->irq, il);
  4281. out_disable_msi:
  4282. pci_disable_msi(il->pci_dev);
  4283. il4965_uninit_drv(il);
  4284. out_free_eeprom:
  4285. il_eeprom_free(il);
  4286. out_iounmap:
  4287. pci_iounmap(pdev, il->hw_base);
  4288. out_pci_release_regions:
  4289. pci_set_drvdata(pdev, NULL);
  4290. pci_release_regions(pdev);
  4291. out_pci_disable_device:
  4292. pci_disable_device(pdev);
  4293. out_ieee80211_free_hw:
  4294. il_free_traffic_mem(il);
  4295. ieee80211_free_hw(il->hw);
  4296. out:
  4297. return err;
  4298. }
  4299. static void __devexit il4965_pci_remove(struct pci_dev *pdev)
  4300. {
  4301. struct il_priv *il = pci_get_drvdata(pdev);
  4302. unsigned long flags;
  4303. if (!il)
  4304. return;
  4305. wait_for_completion(&il->_4965.firmware_loading_complete);
  4306. D_INFO("*** UNLOAD DRIVER ***\n");
  4307. il_dbgfs_unregister(il);
  4308. sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
  4309. /* ieee80211_unregister_hw call wil cause il_mac_stop to
  4310. * to be called and il4965_down since we are removing the device
  4311. * we need to set STATUS_EXIT_PENDING bit.
  4312. */
  4313. set_bit(STATUS_EXIT_PENDING, &il->status);
  4314. il_leds_exit(il);
  4315. if (il->mac80211_registered) {
  4316. ieee80211_unregister_hw(il->hw);
  4317. il->mac80211_registered = 0;
  4318. } else {
  4319. il4965_down(il);
  4320. }
  4321. /*
  4322. * Make sure device is reset to low power before unloading driver.
  4323. * This may be redundant with il4965_down(), but there are paths to
  4324. * run il4965_down() without calling apm_ops.stop(), and there are
  4325. * paths to avoid running il4965_down() at all before leaving driver.
  4326. * This (inexpensive) call *makes sure* device is reset.
  4327. */
  4328. il_apm_stop(il);
  4329. /* make sure we flush any pending irq or
  4330. * tasklet for the driver
  4331. */
  4332. spin_lock_irqsave(&il->lock, flags);
  4333. il_disable_interrupts(il);
  4334. spin_unlock_irqrestore(&il->lock, flags);
  4335. il4965_synchronize_irq(il);
  4336. il4965_dealloc_ucode_pci(il);
  4337. if (il->rxq.bd)
  4338. il4965_rx_queue_free(il, &il->rxq);
  4339. il4965_hw_txq_ctx_free(il);
  4340. il_eeprom_free(il);
  4341. /*netif_stop_queue(dev); */
  4342. flush_workqueue(il->workqueue);
  4343. /* ieee80211_unregister_hw calls il_mac_stop, which flushes
  4344. * il->workqueue... so we can't take down the workqueue
  4345. * until now... */
  4346. destroy_workqueue(il->workqueue);
  4347. il->workqueue = NULL;
  4348. il_free_traffic_mem(il);
  4349. free_irq(il->pci_dev->irq, il);
  4350. pci_disable_msi(il->pci_dev);
  4351. pci_iounmap(pdev, il->hw_base);
  4352. pci_release_regions(pdev);
  4353. pci_disable_device(pdev);
  4354. pci_set_drvdata(pdev, NULL);
  4355. il4965_uninit_drv(il);
  4356. dev_kfree_skb(il->beacon_skb);
  4357. ieee80211_free_hw(il->hw);
  4358. }
  4359. /*
  4360. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  4361. * must be called under il->lock and mac access
  4362. */
  4363. void il4965_txq_set_sched(struct il_priv *il, u32 mask)
  4364. {
  4365. il_wr_prph(il, IL49_SCD_TXFACT, mask);
  4366. }
  4367. /*****************************************************************************
  4368. *
  4369. * driver and module entry point
  4370. *
  4371. *****************************************************************************/
  4372. /* Hardware specific file defines the PCI IDs table for that hardware module */
  4373. static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
  4374. {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
  4375. {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
  4376. {0}
  4377. };
  4378. MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
  4379. static struct pci_driver il4965_driver = {
  4380. .name = DRV_NAME,
  4381. .id_table = il4965_hw_card_ids,
  4382. .probe = il4965_pci_probe,
  4383. .remove = __devexit_p(il4965_pci_remove),
  4384. .driver.pm = IL_LEGACY_PM_OPS,
  4385. };
  4386. static int __init il4965_init(void)
  4387. {
  4388. int ret;
  4389. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  4390. pr_info(DRV_COPYRIGHT "\n");
  4391. ret = il4965_rate_control_register();
  4392. if (ret) {
  4393. pr_err("Unable to register rate control algorithm: %d\n", ret);
  4394. return ret;
  4395. }
  4396. ret = pci_register_driver(&il4965_driver);
  4397. if (ret) {
  4398. pr_err("Unable to initialize PCI module\n");
  4399. goto error_register;
  4400. }
  4401. return ret;
  4402. error_register:
  4403. il4965_rate_control_unregister();
  4404. return ret;
  4405. }
  4406. static void __exit il4965_exit(void)
  4407. {
  4408. pci_unregister_driver(&il4965_driver);
  4409. il4965_rate_control_unregister();
  4410. }
  4411. module_exit(il4965_exit);
  4412. module_init(il4965_init);
  4413. #ifdef CONFIG_IWLEGACY_DEBUG
  4414. module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
  4415. MODULE_PARM_DESC(debug, "debug output mask");
  4416. #endif
  4417. module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
  4418. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  4419. module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
  4420. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  4421. module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
  4422. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  4423. module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K,
  4424. int, S_IRUGO);
  4425. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  4426. module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
  4427. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");