iwl3945-base.c 148 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 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. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/wireless.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/ieee80211_radiotap.h>
  42. #include <net/lib80211.h>
  43. #include <net/mac80211.h>
  44. #include <asm/div64.h>
  45. #define DRV_NAME "iwl3945"
  46. #include "iwl-fh.h"
  47. #include "iwl-3945-fh.h"
  48. #include "iwl-commands.h"
  49. #include "iwl-sta.h"
  50. #include "iwl-3945.h"
  51. #include "iwl-helpers.h"
  52. #include "iwl-core.h"
  53. #include "iwl-dev.h"
  54. /*
  55. * module name, copyright, version, etc.
  56. */
  57. #define DRV_DESCRIPTION \
  58. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  59. #ifdef CONFIG_IWLWIFI_DEBUG
  60. #define VD "d"
  61. #else
  62. #define VD
  63. #endif
  64. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  65. #define VS "s"
  66. #else
  67. #define VS
  68. #endif
  69. #define IWL39_VERSION "1.2.26k" VD VS
  70. #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
  71. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  72. #define DRV_VERSION IWL39_VERSION
  73. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  74. MODULE_VERSION(DRV_VERSION);
  75. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  76. MODULE_LICENSE("GPL");
  77. /* module parameters */
  78. struct iwl_mod_params iwl3945_mod_params = {
  79. .num_of_queues = IWL39_MAX_NUM_QUEUES,
  80. .sw_crypto = 1,
  81. .restart_fw = 1,
  82. /* the rest are 0 by default */
  83. };
  84. /*************** STATION TABLE MANAGEMENT ****
  85. * mac80211 should be examined to determine if sta_info is duplicating
  86. * the functionality provided here
  87. */
  88. /**************************************************************/
  89. #if 0 /* temporary disable till we add real remove station */
  90. /**
  91. * iwl3945_remove_station - Remove driver's knowledge of station.
  92. *
  93. * NOTE: This does not remove station from device's station table.
  94. */
  95. static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  96. {
  97. int index = IWL_INVALID_STATION;
  98. int i;
  99. unsigned long flags;
  100. spin_lock_irqsave(&priv->sta_lock, flags);
  101. if (is_ap)
  102. index = IWL_AP_ID;
  103. else if (is_broadcast_ether_addr(addr))
  104. index = priv->hw_params.bcast_sta_id;
  105. else
  106. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  107. if (priv->stations_39[i].used &&
  108. !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  109. addr)) {
  110. index = i;
  111. break;
  112. }
  113. if (unlikely(index == IWL_INVALID_STATION))
  114. goto out;
  115. if (priv->stations_39[index].used) {
  116. priv->stations_39[index].used = 0;
  117. priv->num_stations--;
  118. }
  119. BUG_ON(priv->num_stations < 0);
  120. out:
  121. spin_unlock_irqrestore(&priv->sta_lock, flags);
  122. return 0;
  123. }
  124. #endif
  125. /**
  126. * iwl3945_clear_stations_table - Clear the driver's station table
  127. *
  128. * NOTE: This does not clear or otherwise alter the device's station table.
  129. */
  130. static void iwl3945_clear_stations_table(struct iwl_priv *priv)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&priv->sta_lock, flags);
  134. priv->num_stations = 0;
  135. memset(priv->stations_39, 0, sizeof(priv->stations_39));
  136. spin_unlock_irqrestore(&priv->sta_lock, flags);
  137. }
  138. /**
  139. * iwl3945_add_station - Add station to station tables in driver and device
  140. */
  141. u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
  142. {
  143. int i;
  144. int index = IWL_INVALID_STATION;
  145. struct iwl3945_station_entry *station;
  146. unsigned long flags_spin;
  147. u8 rate;
  148. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  149. if (is_ap)
  150. index = IWL_AP_ID;
  151. else if (is_broadcast_ether_addr(addr))
  152. index = priv->hw_params.bcast_sta_id;
  153. else
  154. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  155. if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  156. addr)) {
  157. index = i;
  158. break;
  159. }
  160. if (!priv->stations_39[i].used &&
  161. index == IWL_INVALID_STATION)
  162. index = i;
  163. }
  164. /* These two conditions has the same outcome but keep them separate
  165. since they have different meaning */
  166. if (unlikely(index == IWL_INVALID_STATION)) {
  167. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  168. return index;
  169. }
  170. if (priv->stations_39[index].used &&
  171. !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
  172. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  173. return index;
  174. }
  175. IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
  176. station = &priv->stations_39[index];
  177. station->used = 1;
  178. priv->num_stations++;
  179. /* Set up the REPLY_ADD_STA command to send to device */
  180. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  181. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  182. station->sta.mode = 0;
  183. station->sta.sta.sta_id = index;
  184. station->sta.station_flags = 0;
  185. if (priv->band == IEEE80211_BAND_5GHZ)
  186. rate = IWL_RATE_6M_PLCP;
  187. else
  188. rate = IWL_RATE_1M_PLCP;
  189. /* Turn on both antennas for the station... */
  190. station->sta.rate_n_flags =
  191. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  192. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  193. /* Add station to device's station table */
  194. iwl_send_add_sta(priv,
  195. (struct iwl_addsta_cmd *)&station->sta, flags);
  196. return index;
  197. }
  198. static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
  199. {
  200. int rc = 0;
  201. struct iwl_rx_packet *res = NULL;
  202. struct iwl3945_rxon_assoc_cmd rxon_assoc;
  203. struct iwl_host_cmd cmd = {
  204. .id = REPLY_RXON_ASSOC,
  205. .len = sizeof(rxon_assoc),
  206. .meta.flags = CMD_WANT_SKB,
  207. .data = &rxon_assoc,
  208. };
  209. const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
  210. const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
  211. if ((rxon1->flags == rxon2->flags) &&
  212. (rxon1->filter_flags == rxon2->filter_flags) &&
  213. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  214. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  215. IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC. Not resending.\n");
  216. return 0;
  217. }
  218. rxon_assoc.flags = priv->staging_rxon.flags;
  219. rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
  220. rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
  221. rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
  222. rxon_assoc.reserved = 0;
  223. rc = iwl_send_cmd_sync(priv, &cmd);
  224. if (rc)
  225. return rc;
  226. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  227. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  228. IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
  229. rc = -EIO;
  230. }
  231. priv->alloc_rxb_skb--;
  232. dev_kfree_skb_any(cmd.meta.u.skb);
  233. return rc;
  234. }
  235. /**
  236. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  237. * @priv: eeprom and antenna fields are used to determine antenna flags
  238. *
  239. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  240. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  241. *
  242. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  243. * IWL_ANTENNA_MAIN - Force MAIN antenna
  244. * IWL_ANTENNA_AUX - Force AUX antenna
  245. */
  246. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  247. {
  248. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  249. switch (iwl3945_mod_params.antenna) {
  250. case IWL_ANTENNA_DIVERSITY:
  251. return 0;
  252. case IWL_ANTENNA_MAIN:
  253. if (eeprom->antenna_switch_type)
  254. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  255. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  256. case IWL_ANTENNA_AUX:
  257. if (eeprom->antenna_switch_type)
  258. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  259. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  260. }
  261. /* bad antenna selector value */
  262. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  263. iwl3945_mod_params.antenna);
  264. return 0; /* "diversity" is default if error */
  265. }
  266. /**
  267. * iwl3945_commit_rxon - commit staging_rxon to hardware
  268. *
  269. * The RXON command in staging_rxon is committed to the hardware and
  270. * the active_rxon structure is updated with the new data. This
  271. * function correctly transitions out of the RXON_ASSOC_MSK state if
  272. * a HW tune is required based on the RXON structure changes.
  273. */
  274. static int iwl3945_commit_rxon(struct iwl_priv *priv)
  275. {
  276. /* cast away the const for active_rxon in this function */
  277. struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
  278. struct iwl3945_rxon_cmd *staging_rxon = (void *)&priv->staging_rxon;
  279. int rc = 0;
  280. bool new_assoc =
  281. !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
  282. if (!iwl_is_alive(priv))
  283. return -1;
  284. /* always get timestamp with Rx frame */
  285. staging_rxon->flags |= RXON_FLG_TSF2HOST_MSK;
  286. /* select antenna */
  287. staging_rxon->flags &=
  288. ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
  289. staging_rxon->flags |= iwl3945_get_antenna_flags(priv);
  290. rc = iwl_check_rxon_cmd(priv);
  291. if (rc) {
  292. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  293. return -EINVAL;
  294. }
  295. /* If we don't need to send a full RXON, we can use
  296. * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
  297. * and other flags for the current radio configuration. */
  298. if (!iwl_full_rxon_required(priv)) {
  299. rc = iwl3945_send_rxon_assoc(priv);
  300. if (rc) {
  301. IWL_ERR(priv, "Error setting RXON_ASSOC "
  302. "configuration (%d).\n", rc);
  303. return rc;
  304. }
  305. memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
  306. return 0;
  307. }
  308. /* If we are currently associated and the new config requires
  309. * an RXON_ASSOC and the new config wants the associated mask enabled,
  310. * we must clear the associated from the active configuration
  311. * before we apply the new config */
  312. if (iwl_is_associated(priv) && new_assoc) {
  313. IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
  314. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  315. /*
  316. * reserved4 and 5 could have been filled by the iwlcore code.
  317. * Let's clear them before pushing to the 3945.
  318. */
  319. active_rxon->reserved4 = 0;
  320. active_rxon->reserved5 = 0;
  321. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  322. sizeof(struct iwl3945_rxon_cmd),
  323. &priv->active_rxon);
  324. /* If the mask clearing failed then we set
  325. * active_rxon back to what it was previously */
  326. if (rc) {
  327. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  328. IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
  329. "configuration (%d).\n", rc);
  330. return rc;
  331. }
  332. }
  333. IWL_DEBUG_INFO(priv, "Sending RXON\n"
  334. "* with%s RXON_FILTER_ASSOC_MSK\n"
  335. "* channel = %d\n"
  336. "* bssid = %pM\n",
  337. (new_assoc ? "" : "out"),
  338. le16_to_cpu(staging_rxon->channel),
  339. staging_rxon->bssid_addr);
  340. /*
  341. * reserved4 and 5 could have been filled by the iwlcore code.
  342. * Let's clear them before pushing to the 3945.
  343. */
  344. staging_rxon->reserved4 = 0;
  345. staging_rxon->reserved5 = 0;
  346. iwl_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
  347. /* Apply the new configuration */
  348. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  349. sizeof(struct iwl3945_rxon_cmd),
  350. staging_rxon);
  351. if (rc) {
  352. IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
  353. return rc;
  354. }
  355. memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
  356. iwl3945_clear_stations_table(priv);
  357. /* If we issue a new RXON command which required a tune then we must
  358. * send a new TXPOWER command or we won't be able to Tx any frames */
  359. rc = priv->cfg->ops->lib->send_tx_power(priv);
  360. if (rc) {
  361. IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
  362. return rc;
  363. }
  364. /* Add the broadcast address so we can send broadcast frames */
  365. if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
  366. IWL_INVALID_STATION) {
  367. IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
  368. return -EIO;
  369. }
  370. /* If we have set the ASSOC_MSK and we are in BSS mode then
  371. * add the IWL_AP_ID to the station rate table */
  372. if (iwl_is_associated(priv) &&
  373. (priv->iw_mode == NL80211_IFTYPE_STATION))
  374. if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr,
  375. 1, 0)
  376. == IWL_INVALID_STATION) {
  377. IWL_ERR(priv, "Error adding AP address for transmit\n");
  378. return -EIO;
  379. }
  380. /* Init the hardware's rate fallback order based on the band */
  381. rc = iwl3945_init_hw_rate_table(priv);
  382. if (rc) {
  383. IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
  384. return -EIO;
  385. }
  386. return 0;
  387. }
  388. static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  389. struct ieee80211_key_conf *keyconf,
  390. u8 sta_id)
  391. {
  392. unsigned long flags;
  393. __le16 key_flags = 0;
  394. int ret;
  395. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  396. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  397. if (sta_id == priv->hw_params.bcast_sta_id)
  398. key_flags |= STA_KEY_MULTICAST_MSK;
  399. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  400. keyconf->hw_key_idx = keyconf->keyidx;
  401. key_flags &= ~STA_KEY_FLG_INVALID;
  402. spin_lock_irqsave(&priv->sta_lock, flags);
  403. priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
  404. priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
  405. memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
  406. keyconf->keylen);
  407. memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
  408. keyconf->keylen);
  409. if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  410. == STA_KEY_FLG_NO_ENC)
  411. priv->stations_39[sta_id].sta.key.key_offset =
  412. iwl_get_free_ucode_key_index(priv);
  413. /* else, we are overriding an existing key => no need to allocated room
  414. * in uCode. */
  415. WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  416. "no space for a new key");
  417. priv->stations_39[sta_id].sta.key.key_flags = key_flags;
  418. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  419. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  420. IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
  421. ret = iwl_send_add_sta(priv,
  422. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
  423. spin_unlock_irqrestore(&priv->sta_lock, flags);
  424. return ret;
  425. }
  426. static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  427. struct ieee80211_key_conf *keyconf,
  428. u8 sta_id)
  429. {
  430. return -EOPNOTSUPP;
  431. }
  432. static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
  433. struct ieee80211_key_conf *keyconf,
  434. u8 sta_id)
  435. {
  436. return -EOPNOTSUPP;
  437. }
  438. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  439. {
  440. unsigned long flags;
  441. spin_lock_irqsave(&priv->sta_lock, flags);
  442. memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  443. memset(&priv->stations_39[sta_id].sta.key, 0,
  444. sizeof(struct iwl4965_keyinfo));
  445. priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  446. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  447. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  448. spin_unlock_irqrestore(&priv->sta_lock, flags);
  449. IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
  450. iwl_send_add_sta(priv,
  451. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
  452. return 0;
  453. }
  454. static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
  455. struct ieee80211_key_conf *keyconf, u8 sta_id)
  456. {
  457. int ret = 0;
  458. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  459. switch (keyconf->alg) {
  460. case ALG_CCMP:
  461. ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  462. break;
  463. case ALG_TKIP:
  464. ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  465. break;
  466. case ALG_WEP:
  467. ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  468. break;
  469. default:
  470. IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  471. ret = -EINVAL;
  472. }
  473. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  474. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  475. sta_id, ret);
  476. return ret;
  477. }
  478. static int iwl3945_remove_static_key(struct iwl_priv *priv)
  479. {
  480. int ret = -EOPNOTSUPP;
  481. return ret;
  482. }
  483. static int iwl3945_set_static_key(struct iwl_priv *priv,
  484. struct ieee80211_key_conf *key)
  485. {
  486. if (key->alg == ALG_WEP)
  487. return -EOPNOTSUPP;
  488. IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
  489. return -EINVAL;
  490. }
  491. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  492. {
  493. struct list_head *element;
  494. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  495. priv->frames_count);
  496. while (!list_empty(&priv->free_frames)) {
  497. element = priv->free_frames.next;
  498. list_del(element);
  499. kfree(list_entry(element, struct iwl3945_frame, list));
  500. priv->frames_count--;
  501. }
  502. if (priv->frames_count) {
  503. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  504. priv->frames_count);
  505. priv->frames_count = 0;
  506. }
  507. }
  508. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  509. {
  510. struct iwl3945_frame *frame;
  511. struct list_head *element;
  512. if (list_empty(&priv->free_frames)) {
  513. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  514. if (!frame) {
  515. IWL_ERR(priv, "Could not allocate frame!\n");
  516. return NULL;
  517. }
  518. priv->frames_count++;
  519. return frame;
  520. }
  521. element = priv->free_frames.next;
  522. list_del(element);
  523. return list_entry(element, struct iwl3945_frame, list);
  524. }
  525. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  526. {
  527. memset(frame, 0, sizeof(*frame));
  528. list_add(&frame->list, &priv->free_frames);
  529. }
  530. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  531. struct ieee80211_hdr *hdr,
  532. int left)
  533. {
  534. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  535. ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
  536. (priv->iw_mode != NL80211_IFTYPE_AP)))
  537. return 0;
  538. if (priv->ibss_beacon->len > left)
  539. return 0;
  540. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  541. return priv->ibss_beacon->len;
  542. }
  543. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  544. {
  545. struct iwl3945_frame *frame;
  546. unsigned int frame_size;
  547. int rc;
  548. u8 rate;
  549. frame = iwl3945_get_free_frame(priv);
  550. if (!frame) {
  551. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  552. "command.\n");
  553. return -ENOMEM;
  554. }
  555. rate = iwl_rate_get_lowest_plcp(priv);
  556. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  557. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  558. &frame->u.cmd[0]);
  559. iwl3945_free_frame(priv, frame);
  560. return rc;
  561. }
  562. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  563. {
  564. if (priv->shared_virt)
  565. pci_free_consistent(priv->pci_dev,
  566. sizeof(struct iwl3945_shared),
  567. priv->shared_virt,
  568. priv->shared_phys);
  569. }
  570. #define MAX_UCODE_BEACON_INTERVAL 1024
  571. #define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
  572. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  573. {
  574. u16 new_val = 0;
  575. u16 beacon_factor = 0;
  576. beacon_factor =
  577. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  578. / MAX_UCODE_BEACON_INTERVAL;
  579. new_val = beacon_val / beacon_factor;
  580. return cpu_to_le16(new_val);
  581. }
  582. static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
  583. {
  584. u64 interval_tm_unit;
  585. u64 tsf, result;
  586. unsigned long flags;
  587. struct ieee80211_conf *conf = NULL;
  588. u16 beacon_int = 0;
  589. conf = ieee80211_get_hw_conf(priv->hw);
  590. spin_lock_irqsave(&priv->lock, flags);
  591. priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
  592. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  593. tsf = priv->timestamp;
  594. beacon_int = priv->beacon_int;
  595. spin_unlock_irqrestore(&priv->lock, flags);
  596. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  597. if (beacon_int == 0) {
  598. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  599. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  600. } else {
  601. priv->rxon_timing.beacon_interval =
  602. cpu_to_le16(beacon_int);
  603. priv->rxon_timing.beacon_interval =
  604. iwl3945_adjust_beacon_interval(
  605. le16_to_cpu(priv->rxon_timing.beacon_interval));
  606. }
  607. priv->rxon_timing.atim_window = 0;
  608. } else {
  609. priv->rxon_timing.beacon_interval =
  610. iwl3945_adjust_beacon_interval(conf->beacon_int);
  611. /* TODO: we need to get atim_window from upper stack
  612. * for now we set to 0 */
  613. priv->rxon_timing.atim_window = 0;
  614. }
  615. interval_tm_unit =
  616. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  617. result = do_div(tsf, interval_tm_unit);
  618. priv->rxon_timing.beacon_init_val =
  619. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  620. IWL_DEBUG_ASSOC(priv,
  621. "beacon interval %d beacon timer %d beacon tim %d\n",
  622. le16_to_cpu(priv->rxon_timing.beacon_interval),
  623. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  624. le16_to_cpu(priv->rxon_timing.atim_window));
  625. }
  626. static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
  627. {
  628. if (mode == NL80211_IFTYPE_ADHOC) {
  629. const struct iwl_channel_info *ch_info;
  630. ch_info = iwl_get_channel_info(priv,
  631. priv->band,
  632. le16_to_cpu(priv->staging_rxon.channel));
  633. if (!ch_info || !is_channel_ibss(ch_info)) {
  634. IWL_ERR(priv, "channel %d not IBSS channel\n",
  635. le16_to_cpu(priv->staging_rxon.channel));
  636. return -EINVAL;
  637. }
  638. }
  639. iwl_connection_init_rx_config(priv, mode);
  640. iwl3945_clear_stations_table(priv);
  641. /* don't commit rxon if rf-kill is on*/
  642. if (!iwl_is_ready_rf(priv))
  643. return -EAGAIN;
  644. iwl3945_commit_rxon(priv);
  645. return 0;
  646. }
  647. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  648. struct ieee80211_tx_info *info,
  649. struct iwl_cmd *cmd,
  650. struct sk_buff *skb_frag,
  651. int sta_id)
  652. {
  653. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  654. struct iwl3945_hw_key *keyinfo =
  655. &priv->stations_39[sta_id].keyinfo;
  656. switch (keyinfo->alg) {
  657. case ALG_CCMP:
  658. tx->sec_ctl = TX_CMD_SEC_CCM;
  659. memcpy(tx->key, keyinfo->key, keyinfo->keylen);
  660. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  661. break;
  662. case ALG_TKIP:
  663. break;
  664. case ALG_WEP:
  665. tx->sec_ctl = TX_CMD_SEC_WEP |
  666. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  667. if (keyinfo->keylen == 13)
  668. tx->sec_ctl |= TX_CMD_SEC_KEY128;
  669. memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
  670. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  671. "with key %d\n", info->control.hw_key->hw_key_idx);
  672. break;
  673. default:
  674. IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
  675. break;
  676. }
  677. }
  678. /*
  679. * handle build REPLY_TX command notification.
  680. */
  681. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  682. struct iwl_cmd *cmd,
  683. struct ieee80211_tx_info *info,
  684. struct ieee80211_hdr *hdr, u8 std_id)
  685. {
  686. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  687. __le32 tx_flags = tx->tx_flags;
  688. __le16 fc = hdr->frame_control;
  689. u8 rc_flags = info->control.rates[0].flags;
  690. tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  691. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  692. tx_flags |= TX_CMD_FLG_ACK_MSK;
  693. if (ieee80211_is_mgmt(fc))
  694. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  695. if (ieee80211_is_probe_resp(fc) &&
  696. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  697. tx_flags |= TX_CMD_FLG_TSF_MSK;
  698. } else {
  699. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  700. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  701. }
  702. tx->sta_id = std_id;
  703. if (ieee80211_has_morefrags(fc))
  704. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  705. if (ieee80211_is_data_qos(fc)) {
  706. u8 *qc = ieee80211_get_qos_ctl(hdr);
  707. tx->tid_tspec = qc[0] & 0xf;
  708. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  709. } else {
  710. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  711. }
  712. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  713. tx_flags |= TX_CMD_FLG_RTS_MSK;
  714. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  715. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  716. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  717. tx_flags |= TX_CMD_FLG_CTS_MSK;
  718. }
  719. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  720. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  721. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  722. if (ieee80211_is_mgmt(fc)) {
  723. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  724. tx->timeout.pm_frame_timeout = cpu_to_le16(3);
  725. else
  726. tx->timeout.pm_frame_timeout = cpu_to_le16(2);
  727. } else {
  728. tx->timeout.pm_frame_timeout = 0;
  729. #ifdef CONFIG_IWLWIFI_LEDS
  730. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  731. #endif
  732. }
  733. tx->driver_txop = 0;
  734. tx->tx_flags = tx_flags;
  735. tx->next_frame_len = 0;
  736. }
  737. /**
  738. * iwl3945_get_sta_id - Find station's index within station table
  739. */
  740. static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  741. {
  742. int sta_id;
  743. u16 fc = le16_to_cpu(hdr->frame_control);
  744. /* If this frame is broadcast or management, use broadcast station id */
  745. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  746. is_multicast_ether_addr(hdr->addr1))
  747. return priv->hw_params.bcast_sta_id;
  748. switch (priv->iw_mode) {
  749. /* If we are a client station in a BSS network, use the special
  750. * AP station entry (that's the only station we communicate with) */
  751. case NL80211_IFTYPE_STATION:
  752. return IWL_AP_ID;
  753. /* If we are an AP, then find the station, or use BCAST */
  754. case NL80211_IFTYPE_AP:
  755. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  756. if (sta_id != IWL_INVALID_STATION)
  757. return sta_id;
  758. return priv->hw_params.bcast_sta_id;
  759. /* If this frame is going out to an IBSS network, find the station,
  760. * or create a new station table entry */
  761. case NL80211_IFTYPE_ADHOC: {
  762. /* Create new station table entry */
  763. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  764. if (sta_id != IWL_INVALID_STATION)
  765. return sta_id;
  766. sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  767. if (sta_id != IWL_INVALID_STATION)
  768. return sta_id;
  769. IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
  770. "Defaulting to broadcast...\n",
  771. hdr->addr1);
  772. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  773. return priv->hw_params.bcast_sta_id;
  774. }
  775. /* If we are in monitor mode, use BCAST. This is required for
  776. * packet injection. */
  777. case NL80211_IFTYPE_MONITOR:
  778. return priv->hw_params.bcast_sta_id;
  779. default:
  780. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  781. priv->iw_mode);
  782. return priv->hw_params.bcast_sta_id;
  783. }
  784. }
  785. /*
  786. * start REPLY_TX command process
  787. */
  788. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  789. {
  790. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  791. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  792. struct iwl3945_tx_cmd *tx;
  793. struct iwl_tx_queue *txq = NULL;
  794. struct iwl_queue *q = NULL;
  795. struct iwl_cmd *out_cmd = NULL;
  796. dma_addr_t phys_addr;
  797. dma_addr_t txcmd_phys;
  798. int txq_id = skb_get_queue_mapping(skb);
  799. u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
  800. u8 id;
  801. u8 unicast;
  802. u8 sta_id;
  803. u8 tid = 0;
  804. u16 seq_number = 0;
  805. __le16 fc;
  806. u8 wait_write_ptr = 0;
  807. u8 *qc = NULL;
  808. unsigned long flags;
  809. int rc;
  810. spin_lock_irqsave(&priv->lock, flags);
  811. if (iwl_is_rfkill(priv)) {
  812. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  813. goto drop_unlock;
  814. }
  815. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  816. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  817. goto drop_unlock;
  818. }
  819. unicast = !is_multicast_ether_addr(hdr->addr1);
  820. id = 0;
  821. fc = hdr->frame_control;
  822. #ifdef CONFIG_IWLWIFI_DEBUG
  823. if (ieee80211_is_auth(fc))
  824. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  825. else if (ieee80211_is_assoc_req(fc))
  826. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  827. else if (ieee80211_is_reassoc_req(fc))
  828. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  829. #endif
  830. /* drop all data frame if we are not associated */
  831. if (ieee80211_is_data(fc) &&
  832. (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
  833. (!iwl_is_associated(priv) ||
  834. ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
  835. IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
  836. goto drop_unlock;
  837. }
  838. spin_unlock_irqrestore(&priv->lock, flags);
  839. hdr_len = ieee80211_hdrlen(fc);
  840. /* Find (or create) index into station table for destination station */
  841. sta_id = iwl3945_get_sta_id(priv, hdr);
  842. if (sta_id == IWL_INVALID_STATION) {
  843. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  844. hdr->addr1);
  845. goto drop;
  846. }
  847. IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
  848. if (ieee80211_is_data_qos(fc)) {
  849. qc = ieee80211_get_qos_ctl(hdr);
  850. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  851. seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
  852. IEEE80211_SCTL_SEQ;
  853. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  854. (hdr->seq_ctrl &
  855. cpu_to_le16(IEEE80211_SCTL_FRAG));
  856. seq_number += 0x10;
  857. }
  858. /* Descriptor for chosen Tx queue */
  859. txq = &priv->txq[txq_id];
  860. q = &txq->q;
  861. spin_lock_irqsave(&priv->lock, flags);
  862. idx = get_cmd_index(q, q->write_ptr, 0);
  863. /* Set up driver data for this TFD */
  864. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  865. txq->txb[q->write_ptr].skb[0] = skb;
  866. /* Init first empty entry in queue's array of Tx/cmd buffers */
  867. out_cmd = txq->cmd[idx];
  868. tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  869. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  870. memset(tx, 0, sizeof(*tx));
  871. /*
  872. * Set up the Tx-command (not MAC!) header.
  873. * Store the chosen Tx queue and TFD index within the sequence field;
  874. * after Tx, uCode's Tx response will return this value so driver can
  875. * locate the frame within the tx queue and do post-tx processing.
  876. */
  877. out_cmd->hdr.cmd = REPLY_TX;
  878. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  879. INDEX_TO_SEQ(q->write_ptr)));
  880. /* Copy MAC header from skb into command buffer */
  881. memcpy(tx->hdr, hdr, hdr_len);
  882. if (info->control.hw_key)
  883. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
  884. /* TODO need this for burst mode later on */
  885. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  886. /* set is_hcca to 0; it probably will never be implemented */
  887. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  888. /* Total # bytes to be transmitted */
  889. len = (u16)skb->len;
  890. tx->len = cpu_to_le16(len);
  891. tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  892. tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  893. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  894. txq->need_update = 1;
  895. if (qc)
  896. priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
  897. } else {
  898. wait_write_ptr = 1;
  899. txq->need_update = 0;
  900. }
  901. IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
  902. le16_to_cpu(out_cmd->hdr.sequence));
  903. IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx->tx_flags));
  904. iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
  905. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
  906. ieee80211_hdrlen(fc));
  907. /*
  908. * Use the first empty entry in this queue's command buffer array
  909. * to contain the Tx command and MAC header concatenated together
  910. * (payload data will be in another buffer).
  911. * Size of this varies, due to varying MAC header length.
  912. * If end is not dword aligned, we'll have 2 extra bytes at the end
  913. * of the MAC header (device reads on dword boundaries).
  914. * We'll tell device about this padding later.
  915. */
  916. len = sizeof(struct iwl3945_tx_cmd) +
  917. sizeof(struct iwl_cmd_header) + hdr_len;
  918. len_org = len;
  919. len = (len + 3) & ~3;
  920. if (len_org != len)
  921. len_org = 1;
  922. else
  923. len_org = 0;
  924. /* Physical address of this Tx command's header (not MAC header!),
  925. * within command buffer array. */
  926. txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  927. len, PCI_DMA_TODEVICE);
  928. /* we do not map meta data ... so we can safely access address to
  929. * provide to unmap command*/
  930. pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
  931. pci_unmap_len_set(&out_cmd->meta, len, len);
  932. /* Add buffer containing Tx command and MAC(!) header to TFD's
  933. * first entry */
  934. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  935. txcmd_phys, len, 1, 0);
  936. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  937. * if any (802.11 null frames have no payload). */
  938. len = skb->len - hdr_len;
  939. if (len) {
  940. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  941. len, PCI_DMA_TODEVICE);
  942. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  943. phys_addr, len,
  944. 0, U32_PAD(len));
  945. }
  946. /* Tell device the write index *just past* this latest filled TFD */
  947. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  948. rc = iwl_txq_update_write_ptr(priv, txq);
  949. spin_unlock_irqrestore(&priv->lock, flags);
  950. if (rc)
  951. return rc;
  952. if ((iwl_queue_space(q) < q->high_mark)
  953. && priv->mac80211_registered) {
  954. if (wait_write_ptr) {
  955. spin_lock_irqsave(&priv->lock, flags);
  956. txq->need_update = 1;
  957. iwl_txq_update_write_ptr(priv, txq);
  958. spin_unlock_irqrestore(&priv->lock, flags);
  959. }
  960. iwl_stop_queue(priv, skb_get_queue_mapping(skb));
  961. }
  962. return 0;
  963. drop_unlock:
  964. spin_unlock_irqrestore(&priv->lock, flags);
  965. drop:
  966. return -1;
  967. }
  968. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  969. #include "iwl-spectrum.h"
  970. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  971. #define BEACON_TIME_MASK_HIGH 0xFF000000
  972. #define TIME_UNIT 1024
  973. /*
  974. * extended beacon time format
  975. * time in usec will be changed into a 32-bit value in 8:24 format
  976. * the high 1 byte is the beacon counts
  977. * the lower 3 bytes is the time in usec within one beacon interval
  978. */
  979. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  980. {
  981. u32 quot;
  982. u32 rem;
  983. u32 interval = beacon_interval * 1024;
  984. if (!interval || !usec)
  985. return 0;
  986. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  987. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  988. return (quot << 24) + rem;
  989. }
  990. /* base is usually what we get from ucode with each received frame,
  991. * the same as HW timer counter counting down
  992. */
  993. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  994. {
  995. u32 base_low = base & BEACON_TIME_MASK_LOW;
  996. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  997. u32 interval = beacon_interval * TIME_UNIT;
  998. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  999. (addon & BEACON_TIME_MASK_HIGH);
  1000. if (base_low > addon_low)
  1001. res += base_low - addon_low;
  1002. else if (base_low < addon_low) {
  1003. res += interval + base_low - addon_low;
  1004. res += (1 << 24);
  1005. } else
  1006. res += (1 << 24);
  1007. return cpu_to_le32(res);
  1008. }
  1009. static int iwl3945_get_measurement(struct iwl_priv *priv,
  1010. struct ieee80211_measurement_params *params,
  1011. u8 type)
  1012. {
  1013. struct iwl_spectrum_cmd spectrum;
  1014. struct iwl_rx_packet *res;
  1015. struct iwl_host_cmd cmd = {
  1016. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  1017. .data = (void *)&spectrum,
  1018. .meta.flags = CMD_WANT_SKB,
  1019. };
  1020. u32 add_time = le64_to_cpu(params->start_time);
  1021. int rc;
  1022. int spectrum_resp_status;
  1023. int duration = le16_to_cpu(params->duration);
  1024. if (iwl_is_associated(priv))
  1025. add_time =
  1026. iwl3945_usecs_to_beacons(
  1027. le64_to_cpu(params->start_time) - priv->last_tsf,
  1028. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1029. memset(&spectrum, 0, sizeof(spectrum));
  1030. spectrum.channel_count = cpu_to_le16(1);
  1031. spectrum.flags =
  1032. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  1033. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  1034. cmd.len = sizeof(spectrum);
  1035. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  1036. if (iwl_is_associated(priv))
  1037. spectrum.start_time =
  1038. iwl3945_add_beacon_time(priv->last_beacon_time,
  1039. add_time,
  1040. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1041. else
  1042. spectrum.start_time = 0;
  1043. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  1044. spectrum.channels[0].channel = params->channel;
  1045. spectrum.channels[0].type = type;
  1046. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  1047. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  1048. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  1049. rc = iwl_send_cmd_sync(priv, &cmd);
  1050. if (rc)
  1051. return rc;
  1052. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  1053. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1054. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  1055. rc = -EIO;
  1056. }
  1057. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  1058. switch (spectrum_resp_status) {
  1059. case 0: /* Command will be handled */
  1060. if (res->u.spectrum.id != 0xff) {
  1061. IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
  1062. res->u.spectrum.id);
  1063. priv->measurement_status &= ~MEASUREMENT_READY;
  1064. }
  1065. priv->measurement_status |= MEASUREMENT_ACTIVE;
  1066. rc = 0;
  1067. break;
  1068. case 1: /* Command will not be handled */
  1069. rc = -EAGAIN;
  1070. break;
  1071. }
  1072. dev_kfree_skb_any(cmd.meta.u.skb);
  1073. return rc;
  1074. }
  1075. #endif
  1076. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  1077. struct iwl_rx_mem_buffer *rxb)
  1078. {
  1079. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1080. struct iwl_alive_resp *palive;
  1081. struct delayed_work *pwork;
  1082. palive = &pkt->u.alive_frame;
  1083. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  1084. "0x%01X 0x%01X\n",
  1085. palive->is_valid, palive->ver_type,
  1086. palive->ver_subtype);
  1087. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  1088. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  1089. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  1090. sizeof(struct iwl_alive_resp));
  1091. pwork = &priv->init_alive_start;
  1092. } else {
  1093. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1094. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  1095. sizeof(struct iwl_alive_resp));
  1096. pwork = &priv->alive_start;
  1097. iwl3945_disable_events(priv);
  1098. }
  1099. /* We delay the ALIVE response by 5ms to
  1100. * give the HW RF Kill time to activate... */
  1101. if (palive->is_valid == UCODE_VALID_OK)
  1102. queue_delayed_work(priv->workqueue, pwork,
  1103. msecs_to_jiffies(5));
  1104. else
  1105. IWL_WARN(priv, "uCode did not respond OK.\n");
  1106. }
  1107. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  1108. struct iwl_rx_mem_buffer *rxb)
  1109. {
  1110. #ifdef CONFIG_IWLWIFI_DEBUG
  1111. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1112. #endif
  1113. IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  1114. return;
  1115. }
  1116. static void iwl3945_bg_beacon_update(struct work_struct *work)
  1117. {
  1118. struct iwl_priv *priv =
  1119. container_of(work, struct iwl_priv, beacon_update);
  1120. struct sk_buff *beacon;
  1121. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  1122. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  1123. if (!beacon) {
  1124. IWL_ERR(priv, "update beacon failed\n");
  1125. return;
  1126. }
  1127. mutex_lock(&priv->mutex);
  1128. /* new beacon skb is allocated every time; dispose previous.*/
  1129. if (priv->ibss_beacon)
  1130. dev_kfree_skb(priv->ibss_beacon);
  1131. priv->ibss_beacon = beacon;
  1132. mutex_unlock(&priv->mutex);
  1133. iwl3945_send_beacon_cmd(priv);
  1134. }
  1135. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  1136. struct iwl_rx_mem_buffer *rxb)
  1137. {
  1138. #ifdef CONFIG_IWLWIFI_DEBUG
  1139. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1140. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  1141. u8 rate = beacon->beacon_notify_hdr.rate;
  1142. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  1143. "tsf %d %d rate %d\n",
  1144. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  1145. beacon->beacon_notify_hdr.failure_frame,
  1146. le32_to_cpu(beacon->ibss_mgr_status),
  1147. le32_to_cpu(beacon->high_tsf),
  1148. le32_to_cpu(beacon->low_tsf), rate);
  1149. #endif
  1150. if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
  1151. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  1152. queue_work(priv->workqueue, &priv->beacon_update);
  1153. }
  1154. /* Handle notification from uCode that card's power state is changing
  1155. * due to software, hardware, or critical temperature RFKILL */
  1156. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  1157. struct iwl_rx_mem_buffer *rxb)
  1158. {
  1159. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  1160. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  1161. unsigned long status = priv->status;
  1162. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
  1163. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  1164. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  1165. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1166. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1167. if (flags & HW_CARD_DISABLED)
  1168. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1169. else
  1170. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1171. if (flags & SW_CARD_DISABLED)
  1172. set_bit(STATUS_RF_KILL_SW, &priv->status);
  1173. else
  1174. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  1175. iwl_scan_cancel(priv);
  1176. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  1177. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  1178. (test_bit(STATUS_RF_KILL_SW, &status) !=
  1179. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  1180. queue_work(priv->workqueue, &priv->rf_kill);
  1181. else
  1182. wake_up_interruptible(&priv->wait_command_queue);
  1183. }
  1184. /**
  1185. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  1186. *
  1187. * Setup the RX handlers for each of the reply types sent from the uCode
  1188. * to the host.
  1189. *
  1190. * This function chains into the hardware specific files for them to setup
  1191. * any hardware specific handlers as well.
  1192. */
  1193. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  1194. {
  1195. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  1196. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  1197. priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
  1198. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
  1199. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
  1200. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  1201. iwl_rx_pm_debug_statistics_notif;
  1202. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  1203. /*
  1204. * The same handler is used for both the REPLY to a discrete
  1205. * statistics request from the host as well as for the periodic
  1206. * statistics notifications (after received beacons) from the uCode.
  1207. */
  1208. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  1209. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  1210. iwl_setup_spectrum_handlers(priv);
  1211. iwl_setup_rx_scan_handlers(priv);
  1212. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  1213. /* Set up hardware specific Rx handlers */
  1214. iwl3945_hw_rx_handler_setup(priv);
  1215. }
  1216. /************************** RX-FUNCTIONS ****************************/
  1217. /*
  1218. * Rx theory of operation
  1219. *
  1220. * The host allocates 32 DMA target addresses and passes the host address
  1221. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  1222. * 0 to 31
  1223. *
  1224. * Rx Queue Indexes
  1225. * The host/firmware share two index registers for managing the Rx buffers.
  1226. *
  1227. * The READ index maps to the first position that the firmware may be writing
  1228. * to -- the driver can read up to (but not including) this position and get
  1229. * good data.
  1230. * The READ index is managed by the firmware once the card is enabled.
  1231. *
  1232. * The WRITE index maps to the last position the driver has read from -- the
  1233. * position preceding WRITE is the last slot the firmware can place a packet.
  1234. *
  1235. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  1236. * WRITE = READ.
  1237. *
  1238. * During initialization, the host sets up the READ queue position to the first
  1239. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  1240. *
  1241. * When the firmware places a packet in a buffer, it will advance the READ index
  1242. * and fire the RX interrupt. The driver can then query the READ index and
  1243. * process as many packets as possible, moving the WRITE index forward as it
  1244. * resets the Rx queue buffers with new memory.
  1245. *
  1246. * The management in the driver is as follows:
  1247. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  1248. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  1249. * to replenish the iwl->rxq->rx_free.
  1250. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  1251. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  1252. * 'processed' and 'read' driver indexes as well)
  1253. * + A received packet is processed and handed to the kernel network stack,
  1254. * detached from the iwl->rxq. The driver 'processed' index is updated.
  1255. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  1256. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  1257. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  1258. * were enough free buffers and RX_STALLED is set it is cleared.
  1259. *
  1260. *
  1261. * Driver sequence:
  1262. *
  1263. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  1264. * iwl3945_rx_queue_restock
  1265. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  1266. * queue, updates firmware pointers, and updates
  1267. * the WRITE index. If insufficient rx_free buffers
  1268. * are available, schedules iwl3945_rx_replenish
  1269. *
  1270. * -- enable interrupts --
  1271. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  1272. * READ INDEX, detaching the SKB from the pool.
  1273. * Moves the packet buffer from queue to rx_used.
  1274. * Calls iwl3945_rx_queue_restock to refill any empty
  1275. * slots.
  1276. * ...
  1277. *
  1278. */
  1279. /**
  1280. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  1281. */
  1282. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  1283. dma_addr_t dma_addr)
  1284. {
  1285. return cpu_to_le32((u32)dma_addr);
  1286. }
  1287. /**
  1288. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  1289. *
  1290. * If there are slots in the RX queue that need to be restocked,
  1291. * and we have free pre-allocated buffers, fill the ranks as much
  1292. * as we can, pulling from rx_free.
  1293. *
  1294. * This moves the 'write' index forward to catch up with 'processed', and
  1295. * also updates the memory address in the firmware to reference the new
  1296. * target buffer.
  1297. */
  1298. static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
  1299. {
  1300. struct iwl_rx_queue *rxq = &priv->rxq;
  1301. struct list_head *element;
  1302. struct iwl_rx_mem_buffer *rxb;
  1303. unsigned long flags;
  1304. int write, rc;
  1305. spin_lock_irqsave(&rxq->lock, flags);
  1306. write = rxq->write & ~0x7;
  1307. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  1308. /* Get next free Rx buffer, remove from free list */
  1309. element = rxq->rx_free.next;
  1310. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1311. list_del(element);
  1312. /* Point to Rx buffer via next RBD in circular buffer */
  1313. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
  1314. rxq->queue[rxq->write] = rxb;
  1315. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  1316. rxq->free_count--;
  1317. }
  1318. spin_unlock_irqrestore(&rxq->lock, flags);
  1319. /* If the pre-allocated buffer pool is dropping low, schedule to
  1320. * refill it */
  1321. if (rxq->free_count <= RX_LOW_WATERMARK)
  1322. queue_work(priv->workqueue, &priv->rx_replenish);
  1323. /* If we've added more space for the firmware to place data, tell it.
  1324. * Increment device's write pointer in multiples of 8. */
  1325. if ((write != (rxq->write & ~0x7))
  1326. || (abs(rxq->write - rxq->read) > 7)) {
  1327. spin_lock_irqsave(&rxq->lock, flags);
  1328. rxq->need_update = 1;
  1329. spin_unlock_irqrestore(&rxq->lock, flags);
  1330. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  1331. if (rc)
  1332. return rc;
  1333. }
  1334. return 0;
  1335. }
  1336. /**
  1337. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  1338. *
  1339. * When moving to rx_free an SKB is allocated for the slot.
  1340. *
  1341. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  1342. * This is called as a scheduled work item (except for during initialization)
  1343. */
  1344. static void iwl3945_rx_allocate(struct iwl_priv *priv)
  1345. {
  1346. struct iwl_rx_queue *rxq = &priv->rxq;
  1347. struct list_head *element;
  1348. struct iwl_rx_mem_buffer *rxb;
  1349. unsigned long flags;
  1350. spin_lock_irqsave(&rxq->lock, flags);
  1351. while (!list_empty(&rxq->rx_used)) {
  1352. element = rxq->rx_used.next;
  1353. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1354. /* Alloc a new receive buffer */
  1355. rxb->skb =
  1356. alloc_skb(priv->hw_params.rx_buf_size,
  1357. __GFP_NOWARN | GFP_ATOMIC);
  1358. if (!rxb->skb) {
  1359. if (net_ratelimit())
  1360. IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
  1361. /* We don't reschedule replenish work here -- we will
  1362. * call the restock method and if it still needs
  1363. * more buffers it will schedule replenish */
  1364. break;
  1365. }
  1366. /* If radiotap head is required, reserve some headroom here.
  1367. * The physical head count is a variable rx_stats->phy_count.
  1368. * We reserve 4 bytes here. Plus these extra bytes, the
  1369. * headroom of the physical head should be enough for the
  1370. * radiotap head that iwl3945 supported. See iwl3945_rt.
  1371. */
  1372. skb_reserve(rxb->skb, 4);
  1373. priv->alloc_rxb_skb++;
  1374. list_del(element);
  1375. /* Get physical address of RB/SKB */
  1376. rxb->real_dma_addr = pci_map_single(priv->pci_dev,
  1377. rxb->skb->data,
  1378. priv->hw_params.rx_buf_size,
  1379. PCI_DMA_FROMDEVICE);
  1380. list_add_tail(&rxb->list, &rxq->rx_free);
  1381. rxq->free_count++;
  1382. }
  1383. spin_unlock_irqrestore(&rxq->lock, flags);
  1384. }
  1385. void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  1386. {
  1387. unsigned long flags;
  1388. int i;
  1389. spin_lock_irqsave(&rxq->lock, flags);
  1390. INIT_LIST_HEAD(&rxq->rx_free);
  1391. INIT_LIST_HEAD(&rxq->rx_used);
  1392. /* Fill the rx_used queue with _all_ of the Rx buffers */
  1393. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  1394. /* In the reset function, these buffers may have been allocated
  1395. * to an SKB, so we need to unmap and free potential storage */
  1396. if (rxq->pool[i].skb != NULL) {
  1397. pci_unmap_single(priv->pci_dev,
  1398. rxq->pool[i].real_dma_addr,
  1399. priv->hw_params.rx_buf_size,
  1400. PCI_DMA_FROMDEVICE);
  1401. priv->alloc_rxb_skb--;
  1402. dev_kfree_skb(rxq->pool[i].skb);
  1403. rxq->pool[i].skb = NULL;
  1404. }
  1405. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  1406. }
  1407. /* Set us so that we have processed and used all buffers, but have
  1408. * not restocked the Rx queue with fresh buffers */
  1409. rxq->read = rxq->write = 0;
  1410. rxq->free_count = 0;
  1411. spin_unlock_irqrestore(&rxq->lock, flags);
  1412. }
  1413. /*
  1414. * this should be called while priv->lock is locked
  1415. */
  1416. static void __iwl3945_rx_replenish(void *data)
  1417. {
  1418. struct iwl_priv *priv = data;
  1419. iwl3945_rx_allocate(priv);
  1420. iwl3945_rx_queue_restock(priv);
  1421. }
  1422. void iwl3945_rx_replenish(void *data)
  1423. {
  1424. struct iwl_priv *priv = data;
  1425. unsigned long flags;
  1426. iwl3945_rx_allocate(priv);
  1427. spin_lock_irqsave(&priv->lock, flags);
  1428. iwl3945_rx_queue_restock(priv);
  1429. spin_unlock_irqrestore(&priv->lock, flags);
  1430. }
  1431. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  1432. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  1433. * This free routine walks the list of POOL entries and if SKB is set to
  1434. * non NULL it is unmapped and freed
  1435. */
  1436. static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  1437. {
  1438. int i;
  1439. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  1440. if (rxq->pool[i].skb != NULL) {
  1441. pci_unmap_single(priv->pci_dev,
  1442. rxq->pool[i].real_dma_addr,
  1443. priv->hw_params.rx_buf_size,
  1444. PCI_DMA_FROMDEVICE);
  1445. dev_kfree_skb(rxq->pool[i].skb);
  1446. }
  1447. }
  1448. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  1449. rxq->dma_addr);
  1450. pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
  1451. rxq->rb_stts, rxq->rb_stts_dma);
  1452. rxq->bd = NULL;
  1453. rxq->rb_stts = NULL;
  1454. }
  1455. /* Convert linear signal-to-noise ratio into dB */
  1456. static u8 ratio2dB[100] = {
  1457. /* 0 1 2 3 4 5 6 7 8 9 */
  1458. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  1459. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  1460. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  1461. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  1462. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  1463. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  1464. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  1465. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  1466. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  1467. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  1468. };
  1469. /* Calculates a relative dB value from a ratio of linear
  1470. * (i.e. not dB) signal levels.
  1471. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  1472. int iwl3945_calc_db_from_ratio(int sig_ratio)
  1473. {
  1474. /* 1000:1 or higher just report as 60 dB */
  1475. if (sig_ratio >= 1000)
  1476. return 60;
  1477. /* 100:1 or higher, divide by 10 and use table,
  1478. * add 20 dB to make up for divide by 10 */
  1479. if (sig_ratio >= 100)
  1480. return 20 + (int)ratio2dB[sig_ratio/10];
  1481. /* We shouldn't see this */
  1482. if (sig_ratio < 1)
  1483. return 0;
  1484. /* Use table for ratios 1:1 - 99:1 */
  1485. return (int)ratio2dB[sig_ratio];
  1486. }
  1487. #define PERFECT_RSSI (-20) /* dBm */
  1488. #define WORST_RSSI (-95) /* dBm */
  1489. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  1490. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  1491. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  1492. * about formulas used below. */
  1493. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  1494. {
  1495. int sig_qual;
  1496. int degradation = PERFECT_RSSI - rssi_dbm;
  1497. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  1498. * as indicator; formula is (signal dbm - noise dbm).
  1499. * SNR at or above 40 is a great signal (100%).
  1500. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  1501. * Weakest usable signal is usually 10 - 15 dB SNR. */
  1502. if (noise_dbm) {
  1503. if (rssi_dbm - noise_dbm >= 40)
  1504. return 100;
  1505. else if (rssi_dbm < noise_dbm)
  1506. return 0;
  1507. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  1508. /* Else use just the signal level.
  1509. * This formula is a least squares fit of data points collected and
  1510. * compared with a reference system that had a percentage (%) display
  1511. * for signal quality. */
  1512. } else
  1513. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  1514. (15 * RSSI_RANGE + 62 * degradation)) /
  1515. (RSSI_RANGE * RSSI_RANGE);
  1516. if (sig_qual > 100)
  1517. sig_qual = 100;
  1518. else if (sig_qual < 1)
  1519. sig_qual = 0;
  1520. return sig_qual;
  1521. }
  1522. /**
  1523. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1524. *
  1525. * Uses the priv->rx_handlers callback function array to invoke
  1526. * the appropriate handlers, including command responses,
  1527. * frame-received notifications, and other notifications.
  1528. */
  1529. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1530. {
  1531. struct iwl_rx_mem_buffer *rxb;
  1532. struct iwl_rx_packet *pkt;
  1533. struct iwl_rx_queue *rxq = &priv->rxq;
  1534. u32 r, i;
  1535. int reclaim;
  1536. unsigned long flags;
  1537. u8 fill_rx = 0;
  1538. u32 count = 8;
  1539. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1540. * buffer that the driver may process (last buffer filled by ucode). */
  1541. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1542. i = rxq->read;
  1543. if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  1544. fill_rx = 1;
  1545. /* Rx interrupt, but nothing sent from uCode */
  1546. if (i == r)
  1547. IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  1548. while (i != r) {
  1549. rxb = rxq->queue[i];
  1550. /* If an RXB doesn't have a Rx queue slot associated with it,
  1551. * then a bug has been introduced in the queue refilling
  1552. * routines -- catch it here */
  1553. BUG_ON(rxb == NULL);
  1554. rxq->queue[i] = NULL;
  1555. pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
  1556. priv->hw_params.rx_buf_size,
  1557. PCI_DMA_FROMDEVICE);
  1558. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1559. /* Reclaim a command buffer only if this packet is a response
  1560. * to a (driver-originated) command.
  1561. * If the packet (e.g. Rx frame) originated from uCode,
  1562. * there is no command buffer to reclaim.
  1563. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1564. * but apparently a few don't get set; catch them here. */
  1565. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1566. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1567. (pkt->hdr.cmd != REPLY_TX);
  1568. /* Based on type of command response or notification,
  1569. * handle those that need handling via function in
  1570. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1571. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1572. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1573. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1574. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1575. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1576. } else {
  1577. /* No handling needed */
  1578. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1579. "r %d i %d No handler needed for %s, 0x%02x\n",
  1580. r, i, get_cmd_string(pkt->hdr.cmd),
  1581. pkt->hdr.cmd);
  1582. }
  1583. if (reclaim) {
  1584. /* Invoke any callbacks, transfer the skb to caller, and
  1585. * fire off the (possibly) blocking iwl_send_cmd()
  1586. * as we reclaim the driver command queue */
  1587. if (rxb && rxb->skb)
  1588. iwl_tx_cmd_complete(priv, rxb);
  1589. else
  1590. IWL_WARN(priv, "Claim null rxb?\n");
  1591. }
  1592. /* For now we just don't re-use anything. We can tweak this
  1593. * later to try and re-use notification packets and SKBs that
  1594. * fail to Rx correctly */
  1595. if (rxb->skb != NULL) {
  1596. priv->alloc_rxb_skb--;
  1597. dev_kfree_skb_any(rxb->skb);
  1598. rxb->skb = NULL;
  1599. }
  1600. spin_lock_irqsave(&rxq->lock, flags);
  1601. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  1602. spin_unlock_irqrestore(&rxq->lock, flags);
  1603. i = (i + 1) & RX_QUEUE_MASK;
  1604. /* If there are a lot of unused frames,
  1605. * restock the Rx queue so ucode won't assert. */
  1606. if (fill_rx) {
  1607. count++;
  1608. if (count >= 8) {
  1609. priv->rxq.read = i;
  1610. __iwl3945_rx_replenish(priv);
  1611. count = 0;
  1612. }
  1613. }
  1614. }
  1615. /* Backtrack one entry */
  1616. priv->rxq.read = i;
  1617. iwl3945_rx_queue_restock(priv);
  1618. }
  1619. /* call this function to flush any scheduled tasklet */
  1620. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  1621. {
  1622. /* wait to make sure we flush pending tasklet*/
  1623. synchronize_irq(priv->pci_dev->irq);
  1624. tasklet_kill(&priv->irq_tasklet);
  1625. }
  1626. static const char *desc_lookup(int i)
  1627. {
  1628. switch (i) {
  1629. case 1:
  1630. return "FAIL";
  1631. case 2:
  1632. return "BAD_PARAM";
  1633. case 3:
  1634. return "BAD_CHECKSUM";
  1635. case 4:
  1636. return "NMI_INTERRUPT";
  1637. case 5:
  1638. return "SYSASSERT";
  1639. case 6:
  1640. return "FATAL_ERROR";
  1641. }
  1642. return "UNKNOWN";
  1643. }
  1644. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1645. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1646. static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  1647. {
  1648. u32 i;
  1649. u32 desc, time, count, base, data1;
  1650. u32 blink1, blink2, ilink1, ilink2;
  1651. int rc;
  1652. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1653. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1654. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  1655. return;
  1656. }
  1657. rc = iwl_grab_nic_access(priv);
  1658. if (rc) {
  1659. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1660. return;
  1661. }
  1662. count = iwl_read_targ_mem(priv, base);
  1663. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1664. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1665. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1666. priv->status, count);
  1667. }
  1668. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  1669. "ilink1 nmiPC Line\n");
  1670. for (i = ERROR_START_OFFSET;
  1671. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1672. i += ERROR_ELEM_SIZE) {
  1673. desc = iwl_read_targ_mem(priv, base + i);
  1674. time =
  1675. iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  1676. blink1 =
  1677. iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  1678. blink2 =
  1679. iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  1680. ilink1 =
  1681. iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  1682. ilink2 =
  1683. iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  1684. data1 =
  1685. iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  1686. IWL_ERR(priv,
  1687. "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1688. desc_lookup(desc), desc, time, blink1, blink2,
  1689. ilink1, ilink2, data1);
  1690. }
  1691. iwl_release_nic_access(priv);
  1692. }
  1693. #define EVENT_START_OFFSET (6 * sizeof(u32))
  1694. /**
  1695. * iwl3945_print_event_log - Dump error event log to syslog
  1696. *
  1697. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  1698. */
  1699. static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
  1700. u32 num_events, u32 mode)
  1701. {
  1702. u32 i;
  1703. u32 base; /* SRAM byte address of event log header */
  1704. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  1705. u32 ptr; /* SRAM byte address of log data */
  1706. u32 ev, time, data; /* event log data */
  1707. if (num_events == 0)
  1708. return;
  1709. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1710. if (mode == 0)
  1711. event_size = 2 * sizeof(u32);
  1712. else
  1713. event_size = 3 * sizeof(u32);
  1714. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  1715. /* "time" is actually "data" for mode 0 (no timestamp).
  1716. * place event id # at far right for easier visual parsing. */
  1717. for (i = 0; i < num_events; i++) {
  1718. ev = iwl_read_targ_mem(priv, ptr);
  1719. ptr += sizeof(u32);
  1720. time = iwl_read_targ_mem(priv, ptr);
  1721. ptr += sizeof(u32);
  1722. if (mode == 0) {
  1723. /* data, ev */
  1724. IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
  1725. } else {
  1726. data = iwl_read_targ_mem(priv, ptr);
  1727. ptr += sizeof(u32);
  1728. IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
  1729. }
  1730. }
  1731. }
  1732. static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
  1733. {
  1734. int rc;
  1735. u32 base; /* SRAM byte address of event log header */
  1736. u32 capacity; /* event log capacity in # entries */
  1737. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  1738. u32 num_wraps; /* # times uCode wrapped to top of log */
  1739. u32 next_entry; /* index of next entry to be written by uCode */
  1740. u32 size; /* # entries that we'll print */
  1741. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1742. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1743. IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
  1744. return;
  1745. }
  1746. rc = iwl_grab_nic_access(priv);
  1747. if (rc) {
  1748. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1749. return;
  1750. }
  1751. /* event log header */
  1752. capacity = iwl_read_targ_mem(priv, base);
  1753. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  1754. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  1755. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  1756. size = num_wraps ? capacity : next_entry;
  1757. /* bail out if nothing in log */
  1758. if (size == 0) {
  1759. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  1760. iwl_release_nic_access(priv);
  1761. return;
  1762. }
  1763. IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
  1764. size, num_wraps);
  1765. /* if uCode has wrapped back to top of log, start at the oldest entry,
  1766. * i.e the next one that uCode would fill. */
  1767. if (num_wraps)
  1768. iwl3945_print_event_log(priv, next_entry,
  1769. capacity - next_entry, mode);
  1770. /* (then/else) start at top of log */
  1771. iwl3945_print_event_log(priv, 0, next_entry, mode);
  1772. iwl_release_nic_access(priv);
  1773. }
  1774. static void iwl3945_error_recovery(struct iwl_priv *priv)
  1775. {
  1776. unsigned long flags;
  1777. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  1778. sizeof(priv->staging_rxon));
  1779. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1780. iwl3945_commit_rxon(priv);
  1781. iwl3945_add_station(priv, priv->bssid, 1, 0);
  1782. spin_lock_irqsave(&priv->lock, flags);
  1783. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  1784. priv->error_recovering = 0;
  1785. spin_unlock_irqrestore(&priv->lock, flags);
  1786. }
  1787. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  1788. {
  1789. u32 inta, handled = 0;
  1790. u32 inta_fh;
  1791. unsigned long flags;
  1792. #ifdef CONFIG_IWLWIFI_DEBUG
  1793. u32 inta_mask;
  1794. #endif
  1795. spin_lock_irqsave(&priv->lock, flags);
  1796. /* Ack/clear/reset pending uCode interrupts.
  1797. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1798. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1799. inta = iwl_read32(priv, CSR_INT);
  1800. iwl_write32(priv, CSR_INT, inta);
  1801. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1802. * Any new interrupts that happen after this, either while we're
  1803. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1804. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1805. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  1806. #ifdef CONFIG_IWLWIFI_DEBUG
  1807. if (priv->debug_level & IWL_DL_ISR) {
  1808. /* just for debug */
  1809. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1810. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1811. inta, inta_mask, inta_fh);
  1812. }
  1813. #endif
  1814. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1815. * atomic, make sure that inta covers all the interrupts that
  1816. * we've discovered, even if FH interrupt came in just after
  1817. * reading CSR_INT. */
  1818. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1819. inta |= CSR_INT_BIT_FH_RX;
  1820. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1821. inta |= CSR_INT_BIT_FH_TX;
  1822. /* Now service all interrupt bits discovered above. */
  1823. if (inta & CSR_INT_BIT_HW_ERR) {
  1824. IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
  1825. /* Tell the device to stop sending interrupts */
  1826. iwl_disable_interrupts(priv);
  1827. iwl_irq_handle_error(priv);
  1828. handled |= CSR_INT_BIT_HW_ERR;
  1829. spin_unlock_irqrestore(&priv->lock, flags);
  1830. return;
  1831. }
  1832. #ifdef CONFIG_IWLWIFI_DEBUG
  1833. if (priv->debug_level & (IWL_DL_ISR)) {
  1834. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1835. if (inta & CSR_INT_BIT_SCD)
  1836. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  1837. "the frame/frames.\n");
  1838. /* Alive notification via Rx interrupt will do the real work */
  1839. if (inta & CSR_INT_BIT_ALIVE)
  1840. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  1841. }
  1842. #endif
  1843. /* Safely ignore these bits for debug checks below */
  1844. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1845. /* Error detected by uCode */
  1846. if (inta & CSR_INT_BIT_SW_ERR) {
  1847. IWL_ERR(priv, "Microcode SW error detected. "
  1848. "Restarting 0x%X.\n", inta);
  1849. iwl_irq_handle_error(priv);
  1850. handled |= CSR_INT_BIT_SW_ERR;
  1851. }
  1852. /* uCode wakes up after power-down sleep */
  1853. if (inta & CSR_INT_BIT_WAKEUP) {
  1854. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  1855. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  1856. iwl_txq_update_write_ptr(priv, &priv->txq[0]);
  1857. iwl_txq_update_write_ptr(priv, &priv->txq[1]);
  1858. iwl_txq_update_write_ptr(priv, &priv->txq[2]);
  1859. iwl_txq_update_write_ptr(priv, &priv->txq[3]);
  1860. iwl_txq_update_write_ptr(priv, &priv->txq[4]);
  1861. iwl_txq_update_write_ptr(priv, &priv->txq[5]);
  1862. handled |= CSR_INT_BIT_WAKEUP;
  1863. }
  1864. /* All uCode command responses, including Tx command responses,
  1865. * Rx "responses" (frame-received notification), and other
  1866. * notifications from uCode come through here*/
  1867. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1868. iwl3945_rx_handle(priv);
  1869. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1870. }
  1871. if (inta & CSR_INT_BIT_FH_TX) {
  1872. IWL_DEBUG_ISR(priv, "Tx interrupt\n");
  1873. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  1874. if (!iwl_grab_nic_access(priv)) {
  1875. iwl_write_direct32(priv, FH39_TCSR_CREDIT
  1876. (FH39_SRVC_CHNL), 0x0);
  1877. iwl_release_nic_access(priv);
  1878. }
  1879. handled |= CSR_INT_BIT_FH_TX;
  1880. }
  1881. if (inta & ~handled)
  1882. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1883. if (inta & ~CSR_INI_SET_MASK) {
  1884. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  1885. inta & ~CSR_INI_SET_MASK);
  1886. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  1887. }
  1888. /* Re-enable all interrupts */
  1889. /* only Re-enable if disabled by irq */
  1890. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1891. iwl_enable_interrupts(priv);
  1892. #ifdef CONFIG_IWLWIFI_DEBUG
  1893. if (priv->debug_level & (IWL_DL_ISR)) {
  1894. inta = iwl_read32(priv, CSR_INT);
  1895. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1896. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1897. IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1898. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1899. }
  1900. #endif
  1901. spin_unlock_irqrestore(&priv->lock, flags);
  1902. }
  1903. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  1904. enum ieee80211_band band,
  1905. u8 is_active, u8 n_probes,
  1906. struct iwl3945_scan_channel *scan_ch)
  1907. {
  1908. const struct ieee80211_channel *channels = NULL;
  1909. const struct ieee80211_supported_band *sband;
  1910. const struct iwl_channel_info *ch_info;
  1911. u16 passive_dwell = 0;
  1912. u16 active_dwell = 0;
  1913. int added, i;
  1914. sband = iwl_get_hw_mode(priv, band);
  1915. if (!sband)
  1916. return 0;
  1917. channels = sband->channels;
  1918. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  1919. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  1920. if (passive_dwell <= active_dwell)
  1921. passive_dwell = active_dwell + 1;
  1922. for (i = 0, added = 0; i < sband->n_channels; i++) {
  1923. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  1924. continue;
  1925. scan_ch->channel = channels[i].hw_value;
  1926. ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
  1927. if (!is_channel_valid(ch_info)) {
  1928. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  1929. scan_ch->channel);
  1930. continue;
  1931. }
  1932. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1933. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1934. /* If passive , set up for auto-switch
  1935. * and use long active_dwell time.
  1936. */
  1937. if (!is_active || is_channel_passive(ch_info) ||
  1938. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1939. scan_ch->type = 0; /* passive */
  1940. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  1941. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  1942. } else {
  1943. scan_ch->type = 1; /* active */
  1944. }
  1945. /* Set direct probe bits. These may be used both for active
  1946. * scan channels (probes gets sent right away),
  1947. * or for passive channels (probes get se sent only after
  1948. * hearing clear Rx packet).*/
  1949. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  1950. if (n_probes)
  1951. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1952. } else {
  1953. /* uCode v1 does not allow setting direct probe bits on
  1954. * passive channel. */
  1955. if ((scan_ch->type & 1) && n_probes)
  1956. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1957. }
  1958. /* Set txpower levels to defaults */
  1959. scan_ch->tpc.dsp_atten = 110;
  1960. /* scan_pwr_info->tpc.dsp_atten; */
  1961. /*scan_pwr_info->tpc.tx_gain; */
  1962. if (band == IEEE80211_BAND_5GHZ)
  1963. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1964. else {
  1965. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  1966. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1967. * power level:
  1968. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1969. */
  1970. }
  1971. IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
  1972. scan_ch->channel,
  1973. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  1974. (scan_ch->type & 1) ?
  1975. active_dwell : passive_dwell);
  1976. scan_ch++;
  1977. added++;
  1978. }
  1979. IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
  1980. return added;
  1981. }
  1982. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  1983. struct ieee80211_rate *rates)
  1984. {
  1985. int i;
  1986. for (i = 0; i < IWL_RATE_COUNT; i++) {
  1987. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  1988. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  1989. rates[i].hw_value_short = i;
  1990. rates[i].flags = 0;
  1991. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  1992. /*
  1993. * If CCK != 1M then set short preamble rate flag.
  1994. */
  1995. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  1996. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1997. }
  1998. }
  1999. }
  2000. /******************************************************************************
  2001. *
  2002. * uCode download functions
  2003. *
  2004. ******************************************************************************/
  2005. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  2006. {
  2007. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  2008. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  2009. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2010. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  2011. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2012. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2013. }
  2014. /**
  2015. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  2016. * looking at all data.
  2017. */
  2018. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  2019. {
  2020. u32 val;
  2021. u32 save_len = len;
  2022. int rc = 0;
  2023. u32 errcnt;
  2024. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  2025. rc = iwl_grab_nic_access(priv);
  2026. if (rc)
  2027. return rc;
  2028. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  2029. IWL39_RTC_INST_LOWER_BOUND);
  2030. errcnt = 0;
  2031. for (; len > 0; len -= sizeof(u32), image++) {
  2032. /* read data comes through single port, auto-incr addr */
  2033. /* NOTE: Use the debugless read so we don't flood kernel log
  2034. * if IWL_DL_IO is set */
  2035. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  2036. if (val != le32_to_cpu(*image)) {
  2037. IWL_ERR(priv, "uCode INST section is invalid at "
  2038. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  2039. save_len - len, val, le32_to_cpu(*image));
  2040. rc = -EIO;
  2041. errcnt++;
  2042. if (errcnt >= 20)
  2043. break;
  2044. }
  2045. }
  2046. iwl_release_nic_access(priv);
  2047. if (!errcnt)
  2048. IWL_DEBUG_INFO(priv,
  2049. "ucode image in INSTRUCTION memory is good\n");
  2050. return rc;
  2051. }
  2052. /**
  2053. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  2054. * using sample data 100 bytes apart. If these sample points are good,
  2055. * it's a pretty good bet that everything between them is good, too.
  2056. */
  2057. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  2058. {
  2059. u32 val;
  2060. int rc = 0;
  2061. u32 errcnt = 0;
  2062. u32 i;
  2063. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  2064. rc = iwl_grab_nic_access(priv);
  2065. if (rc)
  2066. return rc;
  2067. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  2068. /* read data comes through single port, auto-incr addr */
  2069. /* NOTE: Use the debugless read so we don't flood kernel log
  2070. * if IWL_DL_IO is set */
  2071. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  2072. i + IWL39_RTC_INST_LOWER_BOUND);
  2073. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  2074. if (val != le32_to_cpu(*image)) {
  2075. #if 0 /* Enable this if you want to see details */
  2076. IWL_ERR(priv, "uCode INST section is invalid at "
  2077. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  2078. i, val, *image);
  2079. #endif
  2080. rc = -EIO;
  2081. errcnt++;
  2082. if (errcnt >= 3)
  2083. break;
  2084. }
  2085. }
  2086. iwl_release_nic_access(priv);
  2087. return rc;
  2088. }
  2089. /**
  2090. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  2091. * and verify its contents
  2092. */
  2093. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  2094. {
  2095. __le32 *image;
  2096. u32 len;
  2097. int rc = 0;
  2098. /* Try bootstrap */
  2099. image = (__le32 *)priv->ucode_boot.v_addr;
  2100. len = priv->ucode_boot.len;
  2101. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2102. if (rc == 0) {
  2103. IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
  2104. return 0;
  2105. }
  2106. /* Try initialize */
  2107. image = (__le32 *)priv->ucode_init.v_addr;
  2108. len = priv->ucode_init.len;
  2109. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2110. if (rc == 0) {
  2111. IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
  2112. return 0;
  2113. }
  2114. /* Try runtime/protocol */
  2115. image = (__le32 *)priv->ucode_code.v_addr;
  2116. len = priv->ucode_code.len;
  2117. rc = iwl3945_verify_inst_sparse(priv, image, len);
  2118. if (rc == 0) {
  2119. IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
  2120. return 0;
  2121. }
  2122. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  2123. /* Since nothing seems to match, show first several data entries in
  2124. * instruction SRAM, so maybe visual inspection will give a clue.
  2125. * Selection of bootstrap image (vs. other images) is arbitrary. */
  2126. image = (__le32 *)priv->ucode_boot.v_addr;
  2127. len = priv->ucode_boot.len;
  2128. rc = iwl3945_verify_inst_full(priv, image, len);
  2129. return rc;
  2130. }
  2131. static void iwl3945_nic_start(struct iwl_priv *priv)
  2132. {
  2133. /* Remove all resets to allow NIC to operate */
  2134. iwl_write32(priv, CSR_RESET, 0);
  2135. }
  2136. /**
  2137. * iwl3945_read_ucode - Read uCode images from disk file.
  2138. *
  2139. * Copy into buffers for card to fetch via bus-mastering
  2140. */
  2141. static int iwl3945_read_ucode(struct iwl_priv *priv)
  2142. {
  2143. struct iwl_ucode *ucode;
  2144. int ret = -EINVAL, index;
  2145. const struct firmware *ucode_raw;
  2146. /* firmware file name contains uCode/driver compatibility version */
  2147. const char *name_pre = priv->cfg->fw_name_pre;
  2148. const unsigned int api_max = priv->cfg->ucode_api_max;
  2149. const unsigned int api_min = priv->cfg->ucode_api_min;
  2150. char buf[25];
  2151. u8 *src;
  2152. size_t len;
  2153. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  2154. /* Ask kernel firmware_class module to get the boot firmware off disk.
  2155. * request_firmware() is synchronous, file is in memory on return. */
  2156. for (index = api_max; index >= api_min; index--) {
  2157. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  2158. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  2159. if (ret < 0) {
  2160. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  2161. buf, ret);
  2162. if (ret == -ENOENT)
  2163. continue;
  2164. else
  2165. goto error;
  2166. } else {
  2167. if (index < api_max)
  2168. IWL_ERR(priv, "Loaded firmware %s, "
  2169. "which is deprecated. "
  2170. " Please use API v%u instead.\n",
  2171. buf, api_max);
  2172. IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
  2173. "(%zd bytes) from disk\n",
  2174. buf, ucode_raw->size);
  2175. break;
  2176. }
  2177. }
  2178. if (ret < 0)
  2179. goto error;
  2180. /* Make sure that we got at least our header! */
  2181. if (ucode_raw->size < sizeof(*ucode)) {
  2182. IWL_ERR(priv, "File size way too small!\n");
  2183. ret = -EINVAL;
  2184. goto err_release;
  2185. }
  2186. /* Data from ucode file: header followed by uCode images */
  2187. ucode = (void *)ucode_raw->data;
  2188. priv->ucode_ver = le32_to_cpu(ucode->ver);
  2189. api_ver = IWL_UCODE_API(priv->ucode_ver);
  2190. inst_size = le32_to_cpu(ucode->inst_size);
  2191. data_size = le32_to_cpu(ucode->data_size);
  2192. init_size = le32_to_cpu(ucode->init_size);
  2193. init_data_size = le32_to_cpu(ucode->init_data_size);
  2194. boot_size = le32_to_cpu(ucode->boot_size);
  2195. /* api_ver should match the api version forming part of the
  2196. * firmware filename ... but we don't check for that and only rely
  2197. * on the API version read from firmware header from here on forward */
  2198. if (api_ver < api_min || api_ver > api_max) {
  2199. IWL_ERR(priv, "Driver unable to support your firmware API. "
  2200. "Driver supports v%u, firmware is v%u.\n",
  2201. api_max, api_ver);
  2202. priv->ucode_ver = 0;
  2203. ret = -EINVAL;
  2204. goto err_release;
  2205. }
  2206. if (api_ver != api_max)
  2207. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  2208. "got %u. New firmware can be obtained "
  2209. "from http://www.intellinuxwireless.org.\n",
  2210. api_max, api_ver);
  2211. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  2212. IWL_UCODE_MAJOR(priv->ucode_ver),
  2213. IWL_UCODE_MINOR(priv->ucode_ver),
  2214. IWL_UCODE_API(priv->ucode_ver),
  2215. IWL_UCODE_SERIAL(priv->ucode_ver));
  2216. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  2217. priv->ucode_ver);
  2218. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
  2219. inst_size);
  2220. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
  2221. data_size);
  2222. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
  2223. init_size);
  2224. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
  2225. init_data_size);
  2226. IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
  2227. boot_size);
  2228. /* Verify size of file vs. image size info in file's header */
  2229. if (ucode_raw->size < sizeof(*ucode) +
  2230. inst_size + data_size + init_size +
  2231. init_data_size + boot_size) {
  2232. IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
  2233. ucode_raw->size);
  2234. ret = -EINVAL;
  2235. goto err_release;
  2236. }
  2237. /* Verify that uCode images will fit in card's SRAM */
  2238. if (inst_size > IWL39_MAX_INST_SIZE) {
  2239. IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
  2240. inst_size);
  2241. ret = -EINVAL;
  2242. goto err_release;
  2243. }
  2244. if (data_size > IWL39_MAX_DATA_SIZE) {
  2245. IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
  2246. data_size);
  2247. ret = -EINVAL;
  2248. goto err_release;
  2249. }
  2250. if (init_size > IWL39_MAX_INST_SIZE) {
  2251. IWL_DEBUG_INFO(priv,
  2252. "uCode init instr len %d too large to fit in\n",
  2253. init_size);
  2254. ret = -EINVAL;
  2255. goto err_release;
  2256. }
  2257. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  2258. IWL_DEBUG_INFO(priv,
  2259. "uCode init data len %d too large to fit in\n",
  2260. init_data_size);
  2261. ret = -EINVAL;
  2262. goto err_release;
  2263. }
  2264. if (boot_size > IWL39_MAX_BSM_SIZE) {
  2265. IWL_DEBUG_INFO(priv,
  2266. "uCode boot instr len %d too large to fit in\n",
  2267. boot_size);
  2268. ret = -EINVAL;
  2269. goto err_release;
  2270. }
  2271. /* Allocate ucode buffers for card's bus-master loading ... */
  2272. /* Runtime instructions and 2 copies of data:
  2273. * 1) unmodified from disk
  2274. * 2) backup cache for save/restore during power-downs */
  2275. priv->ucode_code.len = inst_size;
  2276. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  2277. priv->ucode_data.len = data_size;
  2278. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  2279. priv->ucode_data_backup.len = data_size;
  2280. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2281. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  2282. !priv->ucode_data_backup.v_addr)
  2283. goto err_pci_alloc;
  2284. /* Initialization instructions and data */
  2285. if (init_size && init_data_size) {
  2286. priv->ucode_init.len = init_size;
  2287. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  2288. priv->ucode_init_data.len = init_data_size;
  2289. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2290. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  2291. goto err_pci_alloc;
  2292. }
  2293. /* Bootstrap (instructions only, no data) */
  2294. if (boot_size) {
  2295. priv->ucode_boot.len = boot_size;
  2296. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2297. if (!priv->ucode_boot.v_addr)
  2298. goto err_pci_alloc;
  2299. }
  2300. /* Copy images into buffers for card's bus-master reads ... */
  2301. /* Runtime instructions (first block of data in file) */
  2302. src = &ucode->data[0];
  2303. len = priv->ucode_code.len;
  2304. IWL_DEBUG_INFO(priv,
  2305. "Copying (but not loading) uCode instr len %zd\n", len);
  2306. memcpy(priv->ucode_code.v_addr, src, len);
  2307. IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  2308. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  2309. /* Runtime data (2nd block)
  2310. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  2311. src = &ucode->data[inst_size];
  2312. len = priv->ucode_data.len;
  2313. IWL_DEBUG_INFO(priv,
  2314. "Copying (but not loading) uCode data len %zd\n", len);
  2315. memcpy(priv->ucode_data.v_addr, src, len);
  2316. memcpy(priv->ucode_data_backup.v_addr, src, len);
  2317. /* Initialization instructions (3rd block) */
  2318. if (init_size) {
  2319. src = &ucode->data[inst_size + data_size];
  2320. len = priv->ucode_init.len;
  2321. IWL_DEBUG_INFO(priv,
  2322. "Copying (but not loading) init instr len %zd\n", len);
  2323. memcpy(priv->ucode_init.v_addr, src, len);
  2324. }
  2325. /* Initialization data (4th block) */
  2326. if (init_data_size) {
  2327. src = &ucode->data[inst_size + data_size + init_size];
  2328. len = priv->ucode_init_data.len;
  2329. IWL_DEBUG_INFO(priv,
  2330. "Copying (but not loading) init data len %zd\n", len);
  2331. memcpy(priv->ucode_init_data.v_addr, src, len);
  2332. }
  2333. /* Bootstrap instructions (5th block) */
  2334. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  2335. len = priv->ucode_boot.len;
  2336. IWL_DEBUG_INFO(priv,
  2337. "Copying (but not loading) boot instr len %zd\n", len);
  2338. memcpy(priv->ucode_boot.v_addr, src, len);
  2339. /* We have our copies now, allow OS release its copies */
  2340. release_firmware(ucode_raw);
  2341. return 0;
  2342. err_pci_alloc:
  2343. IWL_ERR(priv, "failed to allocate pci memory\n");
  2344. ret = -ENOMEM;
  2345. iwl3945_dealloc_ucode_pci(priv);
  2346. err_release:
  2347. release_firmware(ucode_raw);
  2348. error:
  2349. return ret;
  2350. }
  2351. /**
  2352. * iwl3945_set_ucode_ptrs - Set uCode address location
  2353. *
  2354. * Tell initialization uCode where to find runtime uCode.
  2355. *
  2356. * BSM registers initially contain pointers to initialization uCode.
  2357. * We need to replace them to load runtime uCode inst and data,
  2358. * and to save runtime data when powering down.
  2359. */
  2360. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  2361. {
  2362. dma_addr_t pinst;
  2363. dma_addr_t pdata;
  2364. int rc = 0;
  2365. unsigned long flags;
  2366. /* bits 31:0 for 3945 */
  2367. pinst = priv->ucode_code.p_addr;
  2368. pdata = priv->ucode_data_backup.p_addr;
  2369. spin_lock_irqsave(&priv->lock, flags);
  2370. rc = iwl_grab_nic_access(priv);
  2371. if (rc) {
  2372. spin_unlock_irqrestore(&priv->lock, flags);
  2373. return rc;
  2374. }
  2375. /* Tell bootstrap uCode where to find image to load */
  2376. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  2377. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  2378. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  2379. priv->ucode_data.len);
  2380. /* Inst byte count must be last to set up, bit 31 signals uCode
  2381. * that all new ptr/size info is in place */
  2382. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  2383. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  2384. iwl_release_nic_access(priv);
  2385. spin_unlock_irqrestore(&priv->lock, flags);
  2386. IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
  2387. return rc;
  2388. }
  2389. /**
  2390. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  2391. *
  2392. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  2393. *
  2394. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  2395. */
  2396. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  2397. {
  2398. /* Check alive response for "valid" sign from uCode */
  2399. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  2400. /* We had an error bringing up the hardware, so take it
  2401. * all the way back down so we can try again */
  2402. IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
  2403. goto restart;
  2404. }
  2405. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  2406. * This is a paranoid check, because we would not have gotten the
  2407. * "initialize" alive if code weren't properly loaded. */
  2408. if (iwl3945_verify_ucode(priv)) {
  2409. /* Runtime instruction load was bad;
  2410. * take it all the way back down so we can try again */
  2411. IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
  2412. goto restart;
  2413. }
  2414. /* Send pointers to protocol/runtime uCode image ... init code will
  2415. * load and launch runtime uCode, which will send us another "Alive"
  2416. * notification. */
  2417. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  2418. if (iwl3945_set_ucode_ptrs(priv)) {
  2419. /* Runtime instruction load won't happen;
  2420. * take it all the way back down so we can try again */
  2421. IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
  2422. goto restart;
  2423. }
  2424. return;
  2425. restart:
  2426. queue_work(priv->workqueue, &priv->restart);
  2427. }
  2428. /* temporary */
  2429. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
  2430. struct sk_buff *skb);
  2431. /**
  2432. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  2433. * from protocol/runtime uCode (initialization uCode's
  2434. * Alive gets handled by iwl3945_init_alive_start()).
  2435. */
  2436. static void iwl3945_alive_start(struct iwl_priv *priv)
  2437. {
  2438. int rc = 0;
  2439. int thermal_spin = 0;
  2440. u32 rfkill;
  2441. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  2442. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  2443. /* We had an error bringing up the hardware, so take it
  2444. * all the way back down so we can try again */
  2445. IWL_DEBUG_INFO(priv, "Alive failed.\n");
  2446. goto restart;
  2447. }
  2448. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  2449. * This is a paranoid check, because we would not have gotten the
  2450. * "runtime" alive if code weren't properly loaded. */
  2451. if (iwl3945_verify_ucode(priv)) {
  2452. /* Runtime instruction load was bad;
  2453. * take it all the way back down so we can try again */
  2454. IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
  2455. goto restart;
  2456. }
  2457. iwl3945_clear_stations_table(priv);
  2458. rc = iwl_grab_nic_access(priv);
  2459. if (rc) {
  2460. IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
  2461. return;
  2462. }
  2463. rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
  2464. IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
  2465. iwl_release_nic_access(priv);
  2466. if (rfkill & 0x1) {
  2467. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2468. /* if RFKILL is not on, then wait for thermal
  2469. * sensor in adapter to kick in */
  2470. while (iwl3945_hw_get_temperature(priv) == 0) {
  2471. thermal_spin++;
  2472. udelay(10);
  2473. }
  2474. if (thermal_spin)
  2475. IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
  2476. thermal_spin * 10);
  2477. } else
  2478. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2479. /* After the ALIVE response, we can send commands to 3945 uCode */
  2480. set_bit(STATUS_ALIVE, &priv->status);
  2481. /* Clear out the uCode error bit if it is set */
  2482. clear_bit(STATUS_FW_ERROR, &priv->status);
  2483. if (iwl_is_rfkill(priv))
  2484. return;
  2485. ieee80211_wake_queues(priv->hw);
  2486. priv->active_rate = priv->rates_mask;
  2487. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  2488. iwl_power_update_mode(priv, false);
  2489. if (iwl_is_associated(priv)) {
  2490. struct iwl3945_rxon_cmd *active_rxon =
  2491. (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
  2492. memcpy(&priv->staging_rxon, &priv->active_rxon,
  2493. sizeof(priv->staging_rxon));
  2494. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2495. } else {
  2496. /* Initialize our rx_config data */
  2497. iwl_connection_init_rx_config(priv, priv->iw_mode);
  2498. }
  2499. /* Configure Bluetooth device coexistence support */
  2500. iwl_send_bt_config(priv);
  2501. /* Configure the adapter for unassociated operation */
  2502. iwl3945_commit_rxon(priv);
  2503. iwl3945_reg_txpower_periodic(priv);
  2504. iwl3945_led_register(priv);
  2505. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  2506. set_bit(STATUS_READY, &priv->status);
  2507. wake_up_interruptible(&priv->wait_command_queue);
  2508. if (priv->error_recovering)
  2509. iwl3945_error_recovery(priv);
  2510. /* reassociate for ADHOC mode */
  2511. if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
  2512. struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
  2513. priv->vif);
  2514. if (beacon)
  2515. iwl3945_mac_beacon_update(priv->hw, beacon);
  2516. }
  2517. return;
  2518. restart:
  2519. queue_work(priv->workqueue, &priv->restart);
  2520. }
  2521. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  2522. static void __iwl3945_down(struct iwl_priv *priv)
  2523. {
  2524. unsigned long flags;
  2525. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  2526. struct ieee80211_conf *conf = NULL;
  2527. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  2528. conf = ieee80211_get_hw_conf(priv->hw);
  2529. if (!exit_pending)
  2530. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2531. iwl3945_led_unregister(priv);
  2532. iwl3945_clear_stations_table(priv);
  2533. /* Unblock any waiting calls */
  2534. wake_up_interruptible_all(&priv->wait_command_queue);
  2535. /* Wipe out the EXIT_PENDING status bit if we are not actually
  2536. * exiting the module */
  2537. if (!exit_pending)
  2538. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2539. /* stop and reset the on-board processor */
  2540. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2541. /* tell the device to stop sending interrupts */
  2542. spin_lock_irqsave(&priv->lock, flags);
  2543. iwl_disable_interrupts(priv);
  2544. spin_unlock_irqrestore(&priv->lock, flags);
  2545. iwl_synchronize_irq(priv);
  2546. if (priv->mac80211_registered)
  2547. ieee80211_stop_queues(priv->hw);
  2548. /* If we have not previously called iwl3945_init() then
  2549. * clear all bits but the RF Kill and SUSPEND bits and return */
  2550. if (!iwl_is_init(priv)) {
  2551. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2552. STATUS_RF_KILL_HW |
  2553. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2554. STATUS_RF_KILL_SW |
  2555. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2556. STATUS_GEO_CONFIGURED |
  2557. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  2558. STATUS_IN_SUSPEND |
  2559. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2560. STATUS_EXIT_PENDING;
  2561. goto exit;
  2562. }
  2563. /* ...otherwise clear out all the status bits but the RF Kill and
  2564. * SUSPEND bits and continue taking the NIC down. */
  2565. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2566. STATUS_RF_KILL_HW |
  2567. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2568. STATUS_RF_KILL_SW |
  2569. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2570. STATUS_GEO_CONFIGURED |
  2571. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  2572. STATUS_IN_SUSPEND |
  2573. test_bit(STATUS_FW_ERROR, &priv->status) <<
  2574. STATUS_FW_ERROR |
  2575. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2576. STATUS_EXIT_PENDING;
  2577. priv->cfg->ops->lib->apm_ops.reset(priv);
  2578. spin_lock_irqsave(&priv->lock, flags);
  2579. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2580. spin_unlock_irqrestore(&priv->lock, flags);
  2581. iwl3945_hw_txq_ctx_stop(priv);
  2582. iwl3945_hw_rxq_stop(priv);
  2583. spin_lock_irqsave(&priv->lock, flags);
  2584. if (!iwl_grab_nic_access(priv)) {
  2585. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  2586. APMG_CLK_VAL_DMA_CLK_RQT);
  2587. iwl_release_nic_access(priv);
  2588. }
  2589. spin_unlock_irqrestore(&priv->lock, flags);
  2590. udelay(5);
  2591. if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
  2592. priv->cfg->ops->lib->apm_ops.stop(priv);
  2593. else
  2594. priv->cfg->ops->lib->apm_ops.reset(priv);
  2595. exit:
  2596. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  2597. if (priv->ibss_beacon)
  2598. dev_kfree_skb(priv->ibss_beacon);
  2599. priv->ibss_beacon = NULL;
  2600. /* clear out any free frames */
  2601. iwl3945_clear_free_frames(priv);
  2602. }
  2603. static void iwl3945_down(struct iwl_priv *priv)
  2604. {
  2605. mutex_lock(&priv->mutex);
  2606. __iwl3945_down(priv);
  2607. mutex_unlock(&priv->mutex);
  2608. iwl3945_cancel_deferred_work(priv);
  2609. }
  2610. #define MAX_HW_RESTARTS 5
  2611. static int __iwl3945_up(struct iwl_priv *priv)
  2612. {
  2613. int rc, i;
  2614. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2615. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  2616. return -EIO;
  2617. }
  2618. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  2619. IWL_WARN(priv, "Radio disabled by SW RF kill (module "
  2620. "parameter)\n");
  2621. return -ENODEV;
  2622. }
  2623. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  2624. IWL_ERR(priv, "ucode not available for device bring up\n");
  2625. return -EIO;
  2626. }
  2627. /* If platform's RF_KILL switch is NOT set to KILL */
  2628. if (iwl_read32(priv, CSR_GP_CNTRL) &
  2629. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2630. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2631. else {
  2632. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2633. if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
  2634. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  2635. return -ENODEV;
  2636. }
  2637. }
  2638. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2639. rc = iwl3945_hw_nic_init(priv);
  2640. if (rc) {
  2641. IWL_ERR(priv, "Unable to int nic\n");
  2642. return rc;
  2643. }
  2644. /* make sure rfkill handshake bits are cleared */
  2645. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2646. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2647. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2648. /* clear (again), then enable host interrupts */
  2649. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2650. iwl_enable_interrupts(priv);
  2651. /* really make sure rfkill handshake bits are cleared */
  2652. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2653. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2654. /* Copy original ucode data image from disk into backup cache.
  2655. * This will be used to initialize the on-board processor's
  2656. * data SRAM for a clean start when the runtime program first loads. */
  2657. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  2658. priv->ucode_data.len);
  2659. /* We return success when we resume from suspend and rf_kill is on. */
  2660. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  2661. return 0;
  2662. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  2663. iwl3945_clear_stations_table(priv);
  2664. /* load bootstrap state machine,
  2665. * load bootstrap program into processor's memory,
  2666. * prepare to load the "initialize" uCode */
  2667. priv->cfg->ops->lib->load_ucode(priv);
  2668. if (rc) {
  2669. IWL_ERR(priv,
  2670. "Unable to set up bootstrap uCode: %d\n", rc);
  2671. continue;
  2672. }
  2673. /* start card; "initialize" will load runtime ucode */
  2674. iwl3945_nic_start(priv);
  2675. IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
  2676. return 0;
  2677. }
  2678. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2679. __iwl3945_down(priv);
  2680. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2681. /* tried to restart and config the device for as long as our
  2682. * patience could withstand */
  2683. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  2684. return -EIO;
  2685. }
  2686. /*****************************************************************************
  2687. *
  2688. * Workqueue callbacks
  2689. *
  2690. *****************************************************************************/
  2691. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  2692. {
  2693. struct iwl_priv *priv =
  2694. container_of(data, struct iwl_priv, init_alive_start.work);
  2695. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2696. return;
  2697. mutex_lock(&priv->mutex);
  2698. iwl3945_init_alive_start(priv);
  2699. mutex_unlock(&priv->mutex);
  2700. }
  2701. static void iwl3945_bg_alive_start(struct work_struct *data)
  2702. {
  2703. struct iwl_priv *priv =
  2704. container_of(data, struct iwl_priv, alive_start.work);
  2705. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2706. return;
  2707. mutex_lock(&priv->mutex);
  2708. iwl3945_alive_start(priv);
  2709. mutex_unlock(&priv->mutex);
  2710. }
  2711. static void iwl3945_rfkill_poll(struct work_struct *data)
  2712. {
  2713. struct iwl_priv *priv =
  2714. container_of(data, struct iwl_priv, rfkill_poll.work);
  2715. unsigned long status = priv->status;
  2716. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2717. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2718. else
  2719. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2720. if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
  2721. queue_work(priv->workqueue, &priv->rf_kill);
  2722. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  2723. round_jiffies_relative(2 * HZ));
  2724. }
  2725. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  2726. static void iwl3945_bg_request_scan(struct work_struct *data)
  2727. {
  2728. struct iwl_priv *priv =
  2729. container_of(data, struct iwl_priv, request_scan);
  2730. struct iwl_host_cmd cmd = {
  2731. .id = REPLY_SCAN_CMD,
  2732. .len = sizeof(struct iwl3945_scan_cmd),
  2733. .meta.flags = CMD_SIZE_HUGE,
  2734. };
  2735. int rc = 0;
  2736. struct iwl3945_scan_cmd *scan;
  2737. struct ieee80211_conf *conf = NULL;
  2738. u8 n_probes = 2;
  2739. enum ieee80211_band band;
  2740. DECLARE_SSID_BUF(ssid);
  2741. conf = ieee80211_get_hw_conf(priv->hw);
  2742. mutex_lock(&priv->mutex);
  2743. cancel_delayed_work(&priv->scan_check);
  2744. if (!iwl_is_ready(priv)) {
  2745. IWL_WARN(priv, "request scan called when driver not ready.\n");
  2746. goto done;
  2747. }
  2748. /* Make sure the scan wasn't canceled before this queued work
  2749. * was given the chance to run... */
  2750. if (!test_bit(STATUS_SCANNING, &priv->status))
  2751. goto done;
  2752. /* This should never be called or scheduled if there is currently
  2753. * a scan active in the hardware. */
  2754. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  2755. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
  2756. "Ignoring second request.\n");
  2757. rc = -EIO;
  2758. goto done;
  2759. }
  2760. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2761. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  2762. goto done;
  2763. }
  2764. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2765. IWL_DEBUG_HC(priv,
  2766. "Scan request while abort pending. Queuing.\n");
  2767. goto done;
  2768. }
  2769. if (iwl_is_rfkill(priv)) {
  2770. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  2771. goto done;
  2772. }
  2773. if (!test_bit(STATUS_READY, &priv->status)) {
  2774. IWL_DEBUG_HC(priv,
  2775. "Scan request while uninitialized. Queuing.\n");
  2776. goto done;
  2777. }
  2778. if (!priv->scan_bands) {
  2779. IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
  2780. goto done;
  2781. }
  2782. if (!priv->scan) {
  2783. priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  2784. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  2785. if (!priv->scan) {
  2786. rc = -ENOMEM;
  2787. goto done;
  2788. }
  2789. }
  2790. scan = priv->scan;
  2791. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  2792. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  2793. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  2794. if (iwl_is_associated(priv)) {
  2795. u16 interval = 0;
  2796. u32 extra;
  2797. u32 suspend_time = 100;
  2798. u32 scan_suspend_time = 100;
  2799. unsigned long flags;
  2800. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  2801. spin_lock_irqsave(&priv->lock, flags);
  2802. interval = priv->beacon_int;
  2803. spin_unlock_irqrestore(&priv->lock, flags);
  2804. scan->suspend_time = 0;
  2805. scan->max_out_time = cpu_to_le32(200 * 1024);
  2806. if (!interval)
  2807. interval = suspend_time;
  2808. /*
  2809. * suspend time format:
  2810. * 0-19: beacon interval in usec (time before exec.)
  2811. * 20-23: 0
  2812. * 24-31: number of beacons (suspend between channels)
  2813. */
  2814. extra = (suspend_time / interval) << 24;
  2815. scan_suspend_time = 0xFF0FFFFF &
  2816. (extra | ((suspend_time % interval) * 1024));
  2817. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  2818. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  2819. scan_suspend_time, interval);
  2820. }
  2821. /* We should add the ability for user to lock to PASSIVE ONLY */
  2822. if (priv->one_direct_scan) {
  2823. IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
  2824. print_ssid(ssid, priv->direct_ssid,
  2825. priv->direct_ssid_len));
  2826. scan->direct_scan[0].id = WLAN_EID_SSID;
  2827. scan->direct_scan[0].len = priv->direct_ssid_len;
  2828. memcpy(scan->direct_scan[0].ssid,
  2829. priv->direct_ssid, priv->direct_ssid_len);
  2830. n_probes++;
  2831. } else
  2832. IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
  2833. /* We don't build a direct scan probe request; the uCode will do
  2834. * that based on the direct_mask added to each channel entry */
  2835. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  2836. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  2837. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2838. /* flags + rate selection */
  2839. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  2840. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  2841. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  2842. scan->good_CRC_th = 0;
  2843. band = IEEE80211_BAND_2GHZ;
  2844. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  2845. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  2846. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  2847. band = IEEE80211_BAND_5GHZ;
  2848. } else {
  2849. IWL_WARN(priv, "Invalid scan band count\n");
  2850. goto done;
  2851. }
  2852. scan->tx_cmd.len = cpu_to_le16(
  2853. iwl_fill_probe_req(priv, band,
  2854. (struct ieee80211_mgmt *)scan->data,
  2855. IWL_MAX_SCAN_SIZE - sizeof(*scan)));
  2856. /* select Rx antennas */
  2857. scan->flags |= iwl3945_get_antenna_flags(priv);
  2858. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  2859. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  2860. scan->channel_count =
  2861. iwl3945_get_channels_for_scan(priv, band, 1, /* active */
  2862. n_probes,
  2863. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  2864. if (scan->channel_count == 0) {
  2865. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  2866. goto done;
  2867. }
  2868. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  2869. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  2870. cmd.data = scan;
  2871. scan->len = cpu_to_le16(cmd.len);
  2872. set_bit(STATUS_SCAN_HW, &priv->status);
  2873. rc = iwl_send_cmd_sync(priv, &cmd);
  2874. if (rc)
  2875. goto done;
  2876. queue_delayed_work(priv->workqueue, &priv->scan_check,
  2877. IWL_SCAN_CHECK_WATCHDOG);
  2878. mutex_unlock(&priv->mutex);
  2879. return;
  2880. done:
  2881. /* can not perform scan make sure we clear scanning
  2882. * bits from status so next scan request can be performed.
  2883. * if we dont clear scanning status bit here all next scan
  2884. * will fail
  2885. */
  2886. clear_bit(STATUS_SCAN_HW, &priv->status);
  2887. clear_bit(STATUS_SCANNING, &priv->status);
  2888. /* inform mac80211 scan aborted */
  2889. queue_work(priv->workqueue, &priv->scan_completed);
  2890. mutex_unlock(&priv->mutex);
  2891. }
  2892. static void iwl3945_bg_up(struct work_struct *data)
  2893. {
  2894. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  2895. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2896. return;
  2897. mutex_lock(&priv->mutex);
  2898. __iwl3945_up(priv);
  2899. mutex_unlock(&priv->mutex);
  2900. iwl_rfkill_set_hw_state(priv);
  2901. }
  2902. static void iwl3945_bg_restart(struct work_struct *data)
  2903. {
  2904. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  2905. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2906. return;
  2907. iwl3945_down(priv);
  2908. queue_work(priv->workqueue, &priv->up);
  2909. }
  2910. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  2911. {
  2912. struct iwl_priv *priv =
  2913. container_of(data, struct iwl_priv, rx_replenish);
  2914. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2915. return;
  2916. mutex_lock(&priv->mutex);
  2917. iwl3945_rx_replenish(priv);
  2918. mutex_unlock(&priv->mutex);
  2919. }
  2920. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  2921. static void iwl3945_post_associate(struct iwl_priv *priv)
  2922. {
  2923. int rc = 0;
  2924. struct ieee80211_conf *conf = NULL;
  2925. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  2926. IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
  2927. return;
  2928. }
  2929. IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
  2930. priv->assoc_id, priv->active_rxon.bssid_addr);
  2931. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2932. return;
  2933. if (!priv->vif || !priv->is_open)
  2934. return;
  2935. iwl_scan_cancel_timeout(priv, 200);
  2936. conf = ieee80211_get_hw_conf(priv->hw);
  2937. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2938. iwl3945_commit_rxon(priv);
  2939. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  2940. iwl3945_setup_rxon_timing(priv);
  2941. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  2942. sizeof(priv->rxon_timing), &priv->rxon_timing);
  2943. if (rc)
  2944. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2945. "Attempting to continue.\n");
  2946. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2947. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  2948. IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
  2949. priv->assoc_id, priv->beacon_int);
  2950. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2951. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2952. else
  2953. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2954. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  2955. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  2956. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2957. else
  2958. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2959. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  2960. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2961. }
  2962. iwl3945_commit_rxon(priv);
  2963. switch (priv->iw_mode) {
  2964. case NL80211_IFTYPE_STATION:
  2965. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  2966. break;
  2967. case NL80211_IFTYPE_ADHOC:
  2968. priv->assoc_id = 1;
  2969. iwl3945_add_station(priv, priv->bssid, 0, 0);
  2970. iwl3945_sync_sta(priv, IWL_STA_ID,
  2971. (priv->band == IEEE80211_BAND_5GHZ) ?
  2972. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  2973. CMD_ASYNC);
  2974. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  2975. iwl3945_send_beacon_cmd(priv);
  2976. break;
  2977. default:
  2978. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  2979. __func__, priv->iw_mode);
  2980. break;
  2981. }
  2982. iwl_activate_qos(priv, 0);
  2983. /* we have just associated, don't start scan too early */
  2984. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  2985. }
  2986. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
  2987. /*****************************************************************************
  2988. *
  2989. * mac80211 entry point functions
  2990. *
  2991. *****************************************************************************/
  2992. #define UCODE_READY_TIMEOUT (2 * HZ)
  2993. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  2994. {
  2995. struct iwl_priv *priv = hw->priv;
  2996. int ret;
  2997. IWL_DEBUG_MAC80211(priv, "enter\n");
  2998. /* we should be verifying the device is ready to be opened */
  2999. mutex_lock(&priv->mutex);
  3000. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  3001. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  3002. * ucode filename and max sizes are card-specific. */
  3003. if (!priv->ucode_code.len) {
  3004. ret = iwl3945_read_ucode(priv);
  3005. if (ret) {
  3006. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  3007. mutex_unlock(&priv->mutex);
  3008. goto out_release_irq;
  3009. }
  3010. }
  3011. ret = __iwl3945_up(priv);
  3012. mutex_unlock(&priv->mutex);
  3013. iwl_rfkill_set_hw_state(priv);
  3014. if (ret)
  3015. goto out_release_irq;
  3016. IWL_DEBUG_INFO(priv, "Start UP work.\n");
  3017. if (test_bit(STATUS_IN_SUSPEND, &priv->status))
  3018. return 0;
  3019. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  3020. * mac80211 will not be run successfully. */
  3021. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  3022. test_bit(STATUS_READY, &priv->status),
  3023. UCODE_READY_TIMEOUT);
  3024. if (!ret) {
  3025. if (!test_bit(STATUS_READY, &priv->status)) {
  3026. IWL_ERR(priv,
  3027. "Wait for START_ALIVE timeout after %dms.\n",
  3028. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  3029. ret = -ETIMEDOUT;
  3030. goto out_release_irq;
  3031. }
  3032. }
  3033. /* ucode is running and will send rfkill notifications,
  3034. * no need to poll the killswitch state anymore */
  3035. cancel_delayed_work(&priv->rfkill_poll);
  3036. priv->is_open = 1;
  3037. IWL_DEBUG_MAC80211(priv, "leave\n");
  3038. return 0;
  3039. out_release_irq:
  3040. priv->is_open = 0;
  3041. IWL_DEBUG_MAC80211(priv, "leave - failed\n");
  3042. return ret;
  3043. }
  3044. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  3045. {
  3046. struct iwl_priv *priv = hw->priv;
  3047. IWL_DEBUG_MAC80211(priv, "enter\n");
  3048. if (!priv->is_open) {
  3049. IWL_DEBUG_MAC80211(priv, "leave - skip\n");
  3050. return;
  3051. }
  3052. priv->is_open = 0;
  3053. if (iwl_is_ready_rf(priv)) {
  3054. /* stop mac, cancel any scan request and clear
  3055. * RXON_FILTER_ASSOC_MSK BIT
  3056. */
  3057. mutex_lock(&priv->mutex);
  3058. iwl_scan_cancel_timeout(priv, 100);
  3059. mutex_unlock(&priv->mutex);
  3060. }
  3061. iwl3945_down(priv);
  3062. flush_workqueue(priv->workqueue);
  3063. /* start polling the killswitch state again */
  3064. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  3065. round_jiffies_relative(2 * HZ));
  3066. IWL_DEBUG_MAC80211(priv, "leave\n");
  3067. }
  3068. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  3069. {
  3070. struct iwl_priv *priv = hw->priv;
  3071. IWL_DEBUG_MAC80211(priv, "enter\n");
  3072. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  3073. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  3074. if (iwl3945_tx_skb(priv, skb))
  3075. dev_kfree_skb_any(skb);
  3076. IWL_DEBUG_MAC80211(priv, "leave\n");
  3077. return NETDEV_TX_OK;
  3078. }
  3079. static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
  3080. struct ieee80211_if_init_conf *conf)
  3081. {
  3082. struct iwl_priv *priv = hw->priv;
  3083. unsigned long flags;
  3084. IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
  3085. if (priv->vif) {
  3086. IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
  3087. return -EOPNOTSUPP;
  3088. }
  3089. spin_lock_irqsave(&priv->lock, flags);
  3090. priv->vif = conf->vif;
  3091. priv->iw_mode = conf->type;
  3092. spin_unlock_irqrestore(&priv->lock, flags);
  3093. mutex_lock(&priv->mutex);
  3094. if (conf->mac_addr) {
  3095. IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
  3096. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  3097. }
  3098. if (iwl_is_ready(priv))
  3099. iwl3945_set_mode(priv, conf->type);
  3100. mutex_unlock(&priv->mutex);
  3101. IWL_DEBUG_MAC80211(priv, "leave\n");
  3102. return 0;
  3103. }
  3104. /**
  3105. * iwl3945_mac_config - mac80211 config callback
  3106. *
  3107. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  3108. * be set inappropriately and the driver currently sets the hardware up to
  3109. * use it whenever needed.
  3110. */
  3111. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
  3112. {
  3113. struct iwl_priv *priv = hw->priv;
  3114. const struct iwl_channel_info *ch_info;
  3115. struct ieee80211_conf *conf = &hw->conf;
  3116. unsigned long flags;
  3117. int ret = 0;
  3118. mutex_lock(&priv->mutex);
  3119. IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
  3120. conf->channel->hw_value);
  3121. if (!iwl_is_ready(priv)) {
  3122. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  3123. ret = -EIO;
  3124. goto out;
  3125. }
  3126. if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
  3127. test_bit(STATUS_SCANNING, &priv->status))) {
  3128. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  3129. set_bit(STATUS_CONF_PENDING, &priv->status);
  3130. mutex_unlock(&priv->mutex);
  3131. return 0;
  3132. }
  3133. spin_lock_irqsave(&priv->lock, flags);
  3134. ch_info = iwl_get_channel_info(priv, conf->channel->band,
  3135. conf->channel->hw_value);
  3136. if (!is_channel_valid(ch_info)) {
  3137. IWL_DEBUG_SCAN(priv,
  3138. "Channel %d [%d] is INVALID for this band.\n",
  3139. conf->channel->hw_value, conf->channel->band);
  3140. IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
  3141. spin_unlock_irqrestore(&priv->lock, flags);
  3142. ret = -EINVAL;
  3143. goto out;
  3144. }
  3145. iwl_set_rxon_channel(priv, conf->channel);
  3146. iwl_set_flags_for_band(priv, conf->channel->band);
  3147. /* The list of supported rates and rate mask can be different
  3148. * for each phymode; since the phymode may have changed, reset
  3149. * the rate mask to what mac80211 lists */
  3150. iwl_set_rate(priv);
  3151. spin_unlock_irqrestore(&priv->lock, flags);
  3152. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  3153. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  3154. iwl3945_hw_channel_switch(priv, conf->channel);
  3155. goto out;
  3156. }
  3157. #endif
  3158. if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
  3159. if (conf->radio_enabled &&
  3160. iwl_radio_kill_sw_enable_radio(priv)) {
  3161. IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
  3162. "waiting for uCode\n");
  3163. goto out;
  3164. }
  3165. if (!conf->radio_enabled) {
  3166. iwl_radio_kill_sw_disable_radio(priv);
  3167. IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
  3168. goto out;
  3169. }
  3170. }
  3171. if (iwl_is_rfkill(priv)) {
  3172. IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
  3173. ret = -EIO;
  3174. goto out;
  3175. }
  3176. iwl_set_rate(priv);
  3177. if (memcmp(&priv->active_rxon,
  3178. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  3179. iwl3945_commit_rxon(priv);
  3180. else
  3181. IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
  3182. IWL_DEBUG_MAC80211(priv, "leave\n");
  3183. out:
  3184. clear_bit(STATUS_CONF_PENDING, &priv->status);
  3185. mutex_unlock(&priv->mutex);
  3186. return ret;
  3187. }
  3188. static void iwl3945_config_ap(struct iwl_priv *priv)
  3189. {
  3190. int rc = 0;
  3191. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  3192. return;
  3193. /* The following should be done only at AP bring up */
  3194. if (!(iwl_is_associated(priv))) {
  3195. /* RXON - unassoc (to set timing command) */
  3196. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3197. iwl3945_commit_rxon(priv);
  3198. /* RXON Timing */
  3199. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  3200. iwl3945_setup_rxon_timing(priv);
  3201. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  3202. sizeof(priv->rxon_timing),
  3203. &priv->rxon_timing);
  3204. if (rc)
  3205. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  3206. "Attempting to continue.\n");
  3207. /* FIXME: what should be the assoc_id for AP? */
  3208. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  3209. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  3210. priv->staging_rxon.flags |=
  3211. RXON_FLG_SHORT_PREAMBLE_MSK;
  3212. else
  3213. priv->staging_rxon.flags &=
  3214. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3215. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  3216. if (priv->assoc_capability &
  3217. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  3218. priv->staging_rxon.flags |=
  3219. RXON_FLG_SHORT_SLOT_MSK;
  3220. else
  3221. priv->staging_rxon.flags &=
  3222. ~RXON_FLG_SHORT_SLOT_MSK;
  3223. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  3224. priv->staging_rxon.flags &=
  3225. ~RXON_FLG_SHORT_SLOT_MSK;
  3226. }
  3227. /* restore RXON assoc */
  3228. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  3229. iwl3945_commit_rxon(priv);
  3230. iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
  3231. }
  3232. iwl3945_send_beacon_cmd(priv);
  3233. /* FIXME - we need to add code here to detect a totally new
  3234. * configuration, reset the AP, unassoc, rxon timing, assoc,
  3235. * clear sta table, add BCAST sta... */
  3236. }
  3237. static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
  3238. struct ieee80211_vif *vif,
  3239. struct ieee80211_if_conf *conf)
  3240. {
  3241. struct iwl_priv *priv = hw->priv;
  3242. int rc;
  3243. if (conf == NULL)
  3244. return -EIO;
  3245. if (priv->vif != vif) {
  3246. IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
  3247. return 0;
  3248. }
  3249. /* handle this temporarily here */
  3250. if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
  3251. conf->changed & IEEE80211_IFCC_BEACON) {
  3252. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  3253. if (!beacon)
  3254. return -ENOMEM;
  3255. mutex_lock(&priv->mutex);
  3256. rc = iwl3945_mac_beacon_update(hw, beacon);
  3257. mutex_unlock(&priv->mutex);
  3258. if (rc)
  3259. return rc;
  3260. }
  3261. if (!iwl_is_alive(priv))
  3262. return -EAGAIN;
  3263. mutex_lock(&priv->mutex);
  3264. if (conf->bssid)
  3265. IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
  3266. /*
  3267. * very dubious code was here; the probe filtering flag is never set:
  3268. *
  3269. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  3270. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  3271. */
  3272. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  3273. if (!conf->bssid) {
  3274. conf->bssid = priv->mac_addr;
  3275. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  3276. IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
  3277. conf->bssid);
  3278. }
  3279. if (priv->ibss_beacon)
  3280. dev_kfree_skb(priv->ibss_beacon);
  3281. priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
  3282. }
  3283. if (iwl_is_rfkill(priv))
  3284. goto done;
  3285. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  3286. !is_multicast_ether_addr(conf->bssid)) {
  3287. /* If there is currently a HW scan going on in the background
  3288. * then we need to cancel it else the RXON below will fail. */
  3289. if (iwl_scan_cancel_timeout(priv, 100)) {
  3290. IWL_WARN(priv, "Aborted scan still in progress "
  3291. "after 100ms\n");
  3292. IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
  3293. mutex_unlock(&priv->mutex);
  3294. return -EAGAIN;
  3295. }
  3296. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  3297. /* TODO: Audit driver for usage of these members and see
  3298. * if mac80211 deprecates them (priv->bssid looks like it
  3299. * shouldn't be there, but I haven't scanned the IBSS code
  3300. * to verify) - jpk */
  3301. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  3302. if (priv->iw_mode == NL80211_IFTYPE_AP)
  3303. iwl3945_config_ap(priv);
  3304. else {
  3305. rc = iwl3945_commit_rxon(priv);
  3306. if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
  3307. iwl3945_add_station(priv,
  3308. priv->active_rxon.bssid_addr, 1, 0);
  3309. }
  3310. } else {
  3311. iwl_scan_cancel_timeout(priv, 100);
  3312. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3313. iwl3945_commit_rxon(priv);
  3314. }
  3315. done:
  3316. IWL_DEBUG_MAC80211(priv, "leave\n");
  3317. mutex_unlock(&priv->mutex);
  3318. return 0;
  3319. }
  3320. static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
  3321. struct ieee80211_if_init_conf *conf)
  3322. {
  3323. struct iwl_priv *priv = hw->priv;
  3324. IWL_DEBUG_MAC80211(priv, "enter\n");
  3325. mutex_lock(&priv->mutex);
  3326. if (iwl_is_ready_rf(priv)) {
  3327. iwl_scan_cancel_timeout(priv, 100);
  3328. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3329. iwl3945_commit_rxon(priv);
  3330. }
  3331. if (priv->vif == conf->vif) {
  3332. priv->vif = NULL;
  3333. memset(priv->bssid, 0, ETH_ALEN);
  3334. }
  3335. mutex_unlock(&priv->mutex);
  3336. IWL_DEBUG_MAC80211(priv, "leave\n");
  3337. }
  3338. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  3339. static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
  3340. struct ieee80211_vif *vif,
  3341. struct ieee80211_bss_conf *bss_conf,
  3342. u32 changes)
  3343. {
  3344. struct iwl_priv *priv = hw->priv;
  3345. IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
  3346. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  3347. IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
  3348. bss_conf->use_short_preamble);
  3349. if (bss_conf->use_short_preamble)
  3350. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  3351. else
  3352. priv->staging_rxon.flags &=
  3353. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  3354. }
  3355. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  3356. IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n",
  3357. bss_conf->use_cts_prot);
  3358. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  3359. priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
  3360. else
  3361. priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  3362. }
  3363. if (changes & BSS_CHANGED_ASSOC) {
  3364. IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
  3365. /* This should never happen as this function should
  3366. * never be called from interrupt context. */
  3367. if (WARN_ON_ONCE(in_interrupt()))
  3368. return;
  3369. if (bss_conf->assoc) {
  3370. priv->assoc_id = bss_conf->aid;
  3371. priv->beacon_int = bss_conf->beacon_int;
  3372. priv->timestamp = bss_conf->timestamp;
  3373. priv->assoc_capability = bss_conf->assoc_capability;
  3374. priv->power_data.dtim_period = bss_conf->dtim_period;
  3375. priv->next_scan_jiffies = jiffies +
  3376. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  3377. mutex_lock(&priv->mutex);
  3378. iwl3945_post_associate(priv);
  3379. mutex_unlock(&priv->mutex);
  3380. } else {
  3381. priv->assoc_id = 0;
  3382. IWL_DEBUG_MAC80211(priv,
  3383. "DISASSOC %d\n", bss_conf->assoc);
  3384. }
  3385. } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
  3386. IWL_DEBUG_MAC80211(priv,
  3387. "Associated Changes %d\n", changes);
  3388. iwl3945_send_rxon_assoc(priv);
  3389. }
  3390. }
  3391. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  3392. struct ieee80211_vif *vif,
  3393. struct ieee80211_sta *sta,
  3394. struct ieee80211_key_conf *key)
  3395. {
  3396. struct iwl_priv *priv = hw->priv;
  3397. const u8 *addr;
  3398. int ret = 0;
  3399. u8 sta_id = IWL_INVALID_STATION;
  3400. u8 static_key;
  3401. IWL_DEBUG_MAC80211(priv, "enter\n");
  3402. if (iwl3945_mod_params.sw_crypto) {
  3403. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  3404. return -EOPNOTSUPP;
  3405. }
  3406. addr = sta ? sta->addr : iwl_bcast_addr;
  3407. static_key = !iwl_is_associated(priv);
  3408. if (!static_key) {
  3409. sta_id = iwl3945_hw_find_station(priv, addr);
  3410. if (sta_id == IWL_INVALID_STATION) {
  3411. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  3412. addr);
  3413. return -EINVAL;
  3414. }
  3415. }
  3416. mutex_lock(&priv->mutex);
  3417. iwl_scan_cancel_timeout(priv, 100);
  3418. mutex_unlock(&priv->mutex);
  3419. switch (cmd) {
  3420. case SET_KEY:
  3421. if (static_key)
  3422. ret = iwl3945_set_static_key(priv, key);
  3423. else
  3424. ret = iwl3945_set_dynamic_key(priv, key, sta_id);
  3425. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  3426. break;
  3427. case DISABLE_KEY:
  3428. if (static_key)
  3429. ret = iwl3945_remove_static_key(priv);
  3430. else
  3431. ret = iwl3945_clear_sta_key_info(priv, sta_id);
  3432. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  3433. break;
  3434. default:
  3435. ret = -EINVAL;
  3436. }
  3437. IWL_DEBUG_MAC80211(priv, "leave\n");
  3438. return ret;
  3439. }
  3440. static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  3441. const struct ieee80211_tx_queue_params *params)
  3442. {
  3443. struct iwl_priv *priv = hw->priv;
  3444. unsigned long flags;
  3445. int q;
  3446. IWL_DEBUG_MAC80211(priv, "enter\n");
  3447. if (!iwl_is_ready_rf(priv)) {
  3448. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  3449. return -EIO;
  3450. }
  3451. if (queue >= AC_NUM) {
  3452. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  3453. return 0;
  3454. }
  3455. q = AC_NUM - 1 - queue;
  3456. spin_lock_irqsave(&priv->lock, flags);
  3457. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  3458. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  3459. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  3460. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  3461. cpu_to_le16((params->txop * 32));
  3462. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  3463. priv->qos_data.qos_active = 1;
  3464. spin_unlock_irqrestore(&priv->lock, flags);
  3465. mutex_lock(&priv->mutex);
  3466. if (priv->iw_mode == NL80211_IFTYPE_AP)
  3467. iwl_activate_qos(priv, 1);
  3468. else if (priv->assoc_id && iwl_is_associated(priv))
  3469. iwl_activate_qos(priv, 0);
  3470. mutex_unlock(&priv->mutex);
  3471. IWL_DEBUG_MAC80211(priv, "leave\n");
  3472. return 0;
  3473. }
  3474. static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
  3475. struct ieee80211_tx_queue_stats *stats)
  3476. {
  3477. struct iwl_priv *priv = hw->priv;
  3478. int i, avail;
  3479. struct iwl_tx_queue *txq;
  3480. struct iwl_queue *q;
  3481. unsigned long flags;
  3482. IWL_DEBUG_MAC80211(priv, "enter\n");
  3483. if (!iwl_is_ready_rf(priv)) {
  3484. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  3485. return -EIO;
  3486. }
  3487. spin_lock_irqsave(&priv->lock, flags);
  3488. for (i = 0; i < AC_NUM; i++) {
  3489. txq = &priv->txq[i];
  3490. q = &txq->q;
  3491. avail = iwl_queue_space(q);
  3492. stats[i].len = q->n_window - avail;
  3493. stats[i].limit = q->n_window - q->high_mark;
  3494. stats[i].count = q->n_window;
  3495. }
  3496. spin_unlock_irqrestore(&priv->lock, flags);
  3497. IWL_DEBUG_MAC80211(priv, "leave\n");
  3498. return 0;
  3499. }
  3500. static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
  3501. {
  3502. struct iwl_priv *priv = hw->priv;
  3503. unsigned long flags;
  3504. mutex_lock(&priv->mutex);
  3505. IWL_DEBUG_MAC80211(priv, "enter\n");
  3506. iwl_reset_qos(priv);
  3507. spin_lock_irqsave(&priv->lock, flags);
  3508. priv->assoc_id = 0;
  3509. priv->assoc_capability = 0;
  3510. /* new association get rid of ibss beacon skb */
  3511. if (priv->ibss_beacon)
  3512. dev_kfree_skb(priv->ibss_beacon);
  3513. priv->ibss_beacon = NULL;
  3514. priv->beacon_int = priv->hw->conf.beacon_int;
  3515. priv->timestamp = 0;
  3516. if ((priv->iw_mode == NL80211_IFTYPE_STATION))
  3517. priv->beacon_int = 0;
  3518. spin_unlock_irqrestore(&priv->lock, flags);
  3519. if (!iwl_is_ready_rf(priv)) {
  3520. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  3521. mutex_unlock(&priv->mutex);
  3522. return;
  3523. }
  3524. /* we are restarting association process
  3525. * clear RXON_FILTER_ASSOC_MSK bit
  3526. */
  3527. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  3528. iwl_scan_cancel_timeout(priv, 100);
  3529. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3530. iwl3945_commit_rxon(priv);
  3531. }
  3532. /* Per mac80211.h: This is only used in IBSS mode... */
  3533. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  3534. IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
  3535. mutex_unlock(&priv->mutex);
  3536. return;
  3537. }
  3538. iwl_set_rate(priv);
  3539. mutex_unlock(&priv->mutex);
  3540. IWL_DEBUG_MAC80211(priv, "leave\n");
  3541. }
  3542. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
  3543. {
  3544. struct iwl_priv *priv = hw->priv;
  3545. unsigned long flags;
  3546. __le64 timestamp;
  3547. IWL_DEBUG_MAC80211(priv, "enter\n");
  3548. if (!iwl_is_ready_rf(priv)) {
  3549. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  3550. return -EIO;
  3551. }
  3552. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  3553. IWL_DEBUG_MAC80211(priv, "leave - not IBSS\n");
  3554. return -EIO;
  3555. }
  3556. spin_lock_irqsave(&priv->lock, flags);
  3557. if (priv->ibss_beacon)
  3558. dev_kfree_skb(priv->ibss_beacon);
  3559. priv->ibss_beacon = skb;
  3560. priv->assoc_id = 0;
  3561. timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  3562. priv->timestamp = le64_to_cpu(timestamp);
  3563. IWL_DEBUG_MAC80211(priv, "leave\n");
  3564. spin_unlock_irqrestore(&priv->lock, flags);
  3565. iwl_reset_qos(priv);
  3566. iwl3945_post_associate(priv);
  3567. return 0;
  3568. }
  3569. /*****************************************************************************
  3570. *
  3571. * sysfs attributes
  3572. *
  3573. *****************************************************************************/
  3574. #ifdef CONFIG_IWLWIFI_DEBUG
  3575. /*
  3576. * The following adds a new attribute to the sysfs representation
  3577. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  3578. * used for controlling the debug level.
  3579. *
  3580. * See the level definitions in iwl for details.
  3581. */
  3582. static ssize_t show_debug_level(struct device *d,
  3583. struct device_attribute *attr, char *buf)
  3584. {
  3585. struct iwl_priv *priv = d->driver_data;
  3586. return sprintf(buf, "0x%08X\n", priv->debug_level);
  3587. }
  3588. static ssize_t store_debug_level(struct device *d,
  3589. struct device_attribute *attr,
  3590. const char *buf, size_t count)
  3591. {
  3592. struct iwl_priv *priv = d->driver_data;
  3593. unsigned long val;
  3594. int ret;
  3595. ret = strict_strtoul(buf, 0, &val);
  3596. if (ret)
  3597. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  3598. else
  3599. priv->debug_level = val;
  3600. return strnlen(buf, count);
  3601. }
  3602. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  3603. show_debug_level, store_debug_level);
  3604. #endif /* CONFIG_IWLWIFI_DEBUG */
  3605. static ssize_t show_temperature(struct device *d,
  3606. struct device_attribute *attr, char *buf)
  3607. {
  3608. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3609. if (!iwl_is_alive(priv))
  3610. return -EAGAIN;
  3611. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  3612. }
  3613. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  3614. static ssize_t show_tx_power(struct device *d,
  3615. struct device_attribute *attr, char *buf)
  3616. {
  3617. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3618. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  3619. }
  3620. static ssize_t store_tx_power(struct device *d,
  3621. struct device_attribute *attr,
  3622. const char *buf, size_t count)
  3623. {
  3624. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3625. char *p = (char *)buf;
  3626. u32 val;
  3627. val = simple_strtoul(p, &p, 10);
  3628. if (p == buf)
  3629. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  3630. else
  3631. iwl3945_hw_reg_set_txpower(priv, val);
  3632. return count;
  3633. }
  3634. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  3635. static ssize_t show_flags(struct device *d,
  3636. struct device_attribute *attr, char *buf)
  3637. {
  3638. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3639. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  3640. }
  3641. static ssize_t store_flags(struct device *d,
  3642. struct device_attribute *attr,
  3643. const char *buf, size_t count)
  3644. {
  3645. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3646. u32 flags = simple_strtoul(buf, NULL, 0);
  3647. mutex_lock(&priv->mutex);
  3648. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  3649. /* Cancel any currently running scans... */
  3650. if (iwl_scan_cancel_timeout(priv, 100))
  3651. IWL_WARN(priv, "Could not cancel scan.\n");
  3652. else {
  3653. IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
  3654. flags);
  3655. priv->staging_rxon.flags = cpu_to_le32(flags);
  3656. iwl3945_commit_rxon(priv);
  3657. }
  3658. }
  3659. mutex_unlock(&priv->mutex);
  3660. return count;
  3661. }
  3662. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  3663. static ssize_t show_filter_flags(struct device *d,
  3664. struct device_attribute *attr, char *buf)
  3665. {
  3666. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3667. return sprintf(buf, "0x%04X\n",
  3668. le32_to_cpu(priv->active_rxon.filter_flags));
  3669. }
  3670. static ssize_t store_filter_flags(struct device *d,
  3671. struct device_attribute *attr,
  3672. const char *buf, size_t count)
  3673. {
  3674. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3675. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  3676. mutex_lock(&priv->mutex);
  3677. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  3678. /* Cancel any currently running scans... */
  3679. if (iwl_scan_cancel_timeout(priv, 100))
  3680. IWL_WARN(priv, "Could not cancel scan.\n");
  3681. else {
  3682. IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
  3683. "0x%04X\n", filter_flags);
  3684. priv->staging_rxon.filter_flags =
  3685. cpu_to_le32(filter_flags);
  3686. iwl3945_commit_rxon(priv);
  3687. }
  3688. }
  3689. mutex_unlock(&priv->mutex);
  3690. return count;
  3691. }
  3692. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  3693. store_filter_flags);
  3694. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3695. static ssize_t show_measurement(struct device *d,
  3696. struct device_attribute *attr, char *buf)
  3697. {
  3698. struct iwl_priv *priv = dev_get_drvdata(d);
  3699. struct iwl_spectrum_notification measure_report;
  3700. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  3701. u8 *data = (u8 *)&measure_report;
  3702. unsigned long flags;
  3703. spin_lock_irqsave(&priv->lock, flags);
  3704. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  3705. spin_unlock_irqrestore(&priv->lock, flags);
  3706. return 0;
  3707. }
  3708. memcpy(&measure_report, &priv->measure_report, size);
  3709. priv->measurement_status = 0;
  3710. spin_unlock_irqrestore(&priv->lock, flags);
  3711. while (size && (PAGE_SIZE - len)) {
  3712. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3713. PAGE_SIZE - len, 1);
  3714. len = strlen(buf);
  3715. if (PAGE_SIZE - len)
  3716. buf[len++] = '\n';
  3717. ofs += 16;
  3718. size -= min(size, 16U);
  3719. }
  3720. return len;
  3721. }
  3722. static ssize_t store_measurement(struct device *d,
  3723. struct device_attribute *attr,
  3724. const char *buf, size_t count)
  3725. {
  3726. struct iwl_priv *priv = dev_get_drvdata(d);
  3727. struct ieee80211_measurement_params params = {
  3728. .channel = le16_to_cpu(priv->active_rxon.channel),
  3729. .start_time = cpu_to_le64(priv->last_tsf),
  3730. .duration = cpu_to_le16(1),
  3731. };
  3732. u8 type = IWL_MEASURE_BASIC;
  3733. u8 buffer[32];
  3734. u8 channel;
  3735. if (count) {
  3736. char *p = buffer;
  3737. strncpy(buffer, buf, min(sizeof(buffer), count));
  3738. channel = simple_strtoul(p, NULL, 0);
  3739. if (channel)
  3740. params.channel = channel;
  3741. p = buffer;
  3742. while (*p && *p != ' ')
  3743. p++;
  3744. if (*p)
  3745. type = simple_strtoul(p + 1, NULL, 0);
  3746. }
  3747. IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
  3748. "channel %d (for '%s')\n", type, params.channel, buf);
  3749. iwl3945_get_measurement(priv, &params, type);
  3750. return count;
  3751. }
  3752. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  3753. show_measurement, store_measurement);
  3754. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  3755. static ssize_t store_retry_rate(struct device *d,
  3756. struct device_attribute *attr,
  3757. const char *buf, size_t count)
  3758. {
  3759. struct iwl_priv *priv = dev_get_drvdata(d);
  3760. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  3761. if (priv->retry_rate <= 0)
  3762. priv->retry_rate = 1;
  3763. return count;
  3764. }
  3765. static ssize_t show_retry_rate(struct device *d,
  3766. struct device_attribute *attr, char *buf)
  3767. {
  3768. struct iwl_priv *priv = dev_get_drvdata(d);
  3769. return sprintf(buf, "%d", priv->retry_rate);
  3770. }
  3771. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  3772. store_retry_rate);
  3773. static ssize_t store_power_level(struct device *d,
  3774. struct device_attribute *attr,
  3775. const char *buf, size_t count)
  3776. {
  3777. struct iwl_priv *priv = dev_get_drvdata(d);
  3778. int ret;
  3779. unsigned long mode;
  3780. mutex_lock(&priv->mutex);
  3781. ret = strict_strtoul(buf, 10, &mode);
  3782. if (ret)
  3783. goto out;
  3784. ret = iwl_power_set_user_mode(priv, mode);
  3785. if (ret) {
  3786. IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
  3787. goto out;
  3788. }
  3789. ret = count;
  3790. out:
  3791. mutex_unlock(&priv->mutex);
  3792. return ret;
  3793. }
  3794. static ssize_t show_power_level(struct device *d,
  3795. struct device_attribute *attr, char *buf)
  3796. {
  3797. struct iwl_priv *priv = dev_get_drvdata(d);
  3798. int mode = priv->power_data.user_power_setting;
  3799. int system = priv->power_data.system_power_setting;
  3800. int level = priv->power_data.power_mode;
  3801. char *p = buf;
  3802. switch (system) {
  3803. case IWL_POWER_SYS_AUTO:
  3804. p += sprintf(p, "SYSTEM:auto");
  3805. break;
  3806. case IWL_POWER_SYS_AC:
  3807. p += sprintf(p, "SYSTEM:ac");
  3808. break;
  3809. case IWL_POWER_SYS_BATTERY:
  3810. p += sprintf(p, "SYSTEM:battery");
  3811. break;
  3812. }
  3813. p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
  3814. "fixed" : "auto");
  3815. p += sprintf(p, "\tINDEX:%d", level);
  3816. p += sprintf(p, "\n");
  3817. return p - buf + 1;
  3818. }
  3819. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
  3820. show_power_level, store_power_level);
  3821. #define MAX_WX_STRING 80
  3822. /* Values are in microsecond */
  3823. static const s32 timeout_duration[] = {
  3824. 350000,
  3825. 250000,
  3826. 75000,
  3827. 37000,
  3828. 25000,
  3829. };
  3830. static const s32 period_duration[] = {
  3831. 400000,
  3832. 700000,
  3833. 1000000,
  3834. 1000000,
  3835. 1000000
  3836. };
  3837. static ssize_t show_channels(struct device *d,
  3838. struct device_attribute *attr, char *buf)
  3839. {
  3840. /* all this shit doesn't belong into sysfs anyway */
  3841. return 0;
  3842. }
  3843. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  3844. static ssize_t show_statistics(struct device *d,
  3845. struct device_attribute *attr, char *buf)
  3846. {
  3847. struct iwl_priv *priv = dev_get_drvdata(d);
  3848. u32 size = sizeof(struct iwl3945_notif_statistics);
  3849. u32 len = 0, ofs = 0;
  3850. u8 *data = (u8 *)&priv->statistics_39;
  3851. int rc = 0;
  3852. if (!iwl_is_alive(priv))
  3853. return -EAGAIN;
  3854. mutex_lock(&priv->mutex);
  3855. rc = iwl_send_statistics_request(priv, 0);
  3856. mutex_unlock(&priv->mutex);
  3857. if (rc) {
  3858. len = sprintf(buf,
  3859. "Error sending statistics request: 0x%08X\n", rc);
  3860. return len;
  3861. }
  3862. while (size && (PAGE_SIZE - len)) {
  3863. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3864. PAGE_SIZE - len, 1);
  3865. len = strlen(buf);
  3866. if (PAGE_SIZE - len)
  3867. buf[len++] = '\n';
  3868. ofs += 16;
  3869. size -= min(size, 16U);
  3870. }
  3871. return len;
  3872. }
  3873. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  3874. static ssize_t show_antenna(struct device *d,
  3875. struct device_attribute *attr, char *buf)
  3876. {
  3877. struct iwl_priv *priv = dev_get_drvdata(d);
  3878. if (!iwl_is_alive(priv))
  3879. return -EAGAIN;
  3880. return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
  3881. }
  3882. static ssize_t store_antenna(struct device *d,
  3883. struct device_attribute *attr,
  3884. const char *buf, size_t count)
  3885. {
  3886. struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
  3887. int ant;
  3888. if (count == 0)
  3889. return 0;
  3890. if (sscanf(buf, "%1i", &ant) != 1) {
  3891. IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
  3892. return count;
  3893. }
  3894. if ((ant >= 0) && (ant <= 2)) {
  3895. IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
  3896. iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
  3897. } else
  3898. IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
  3899. return count;
  3900. }
  3901. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  3902. static ssize_t show_status(struct device *d,
  3903. struct device_attribute *attr, char *buf)
  3904. {
  3905. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  3906. if (!iwl_is_alive(priv))
  3907. return -EAGAIN;
  3908. return sprintf(buf, "0x%08x\n", (int)priv->status);
  3909. }
  3910. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  3911. static ssize_t dump_error_log(struct device *d,
  3912. struct device_attribute *attr,
  3913. const char *buf, size_t count)
  3914. {
  3915. char *p = (char *)buf;
  3916. if (p[0] == '1')
  3917. iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  3918. return strnlen(buf, count);
  3919. }
  3920. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  3921. static ssize_t dump_event_log(struct device *d,
  3922. struct device_attribute *attr,
  3923. const char *buf, size_t count)
  3924. {
  3925. char *p = (char *)buf;
  3926. if (p[0] == '1')
  3927. iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  3928. return strnlen(buf, count);
  3929. }
  3930. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  3931. /*****************************************************************************
  3932. *
  3933. * driver setup and tear down
  3934. *
  3935. *****************************************************************************/
  3936. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  3937. {
  3938. priv->workqueue = create_singlethread_workqueue(DRV_NAME);
  3939. init_waitqueue_head(&priv->wait_command_queue);
  3940. INIT_WORK(&priv->up, iwl3945_bg_up);
  3941. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  3942. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  3943. INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
  3944. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  3945. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  3946. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  3947. INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
  3948. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  3949. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  3950. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  3951. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  3952. iwl3945_hw_setup_deferred_work(priv);
  3953. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  3954. iwl3945_irq_tasklet, (unsigned long)priv);
  3955. }
  3956. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  3957. {
  3958. iwl3945_hw_cancel_deferred_work(priv);
  3959. cancel_delayed_work_sync(&priv->init_alive_start);
  3960. cancel_delayed_work(&priv->scan_check);
  3961. cancel_delayed_work(&priv->alive_start);
  3962. cancel_work_sync(&priv->beacon_update);
  3963. }
  3964. static struct attribute *iwl3945_sysfs_entries[] = {
  3965. &dev_attr_antenna.attr,
  3966. &dev_attr_channels.attr,
  3967. &dev_attr_dump_errors.attr,
  3968. &dev_attr_dump_events.attr,
  3969. &dev_attr_flags.attr,
  3970. &dev_attr_filter_flags.attr,
  3971. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3972. &dev_attr_measurement.attr,
  3973. #endif
  3974. &dev_attr_power_level.attr,
  3975. &dev_attr_retry_rate.attr,
  3976. &dev_attr_statistics.attr,
  3977. &dev_attr_status.attr,
  3978. &dev_attr_temperature.attr,
  3979. &dev_attr_tx_power.attr,
  3980. #ifdef CONFIG_IWLWIFI_DEBUG
  3981. &dev_attr_debug_level.attr,
  3982. #endif
  3983. NULL
  3984. };
  3985. static struct attribute_group iwl3945_attribute_group = {
  3986. .name = NULL, /* put in device directory */
  3987. .attrs = iwl3945_sysfs_entries,
  3988. };
  3989. static struct ieee80211_ops iwl3945_hw_ops = {
  3990. .tx = iwl3945_mac_tx,
  3991. .start = iwl3945_mac_start,
  3992. .stop = iwl3945_mac_stop,
  3993. .add_interface = iwl3945_mac_add_interface,
  3994. .remove_interface = iwl3945_mac_remove_interface,
  3995. .config = iwl3945_mac_config,
  3996. .config_interface = iwl3945_mac_config_interface,
  3997. .configure_filter = iwl_configure_filter,
  3998. .set_key = iwl3945_mac_set_key,
  3999. .get_tx_stats = iwl3945_mac_get_tx_stats,
  4000. .conf_tx = iwl3945_mac_conf_tx,
  4001. .reset_tsf = iwl3945_mac_reset_tsf,
  4002. .bss_info_changed = iwl3945_bss_info_changed,
  4003. .hw_scan = iwl_mac_hw_scan
  4004. };
  4005. static int iwl3945_init_drv(struct iwl_priv *priv)
  4006. {
  4007. int ret;
  4008. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  4009. priv->retry_rate = 1;
  4010. priv->ibss_beacon = NULL;
  4011. spin_lock_init(&priv->lock);
  4012. spin_lock_init(&priv->power_data.lock);
  4013. spin_lock_init(&priv->sta_lock);
  4014. spin_lock_init(&priv->hcmd_lock);
  4015. INIT_LIST_HEAD(&priv->free_frames);
  4016. mutex_init(&priv->mutex);
  4017. /* Clear the driver's (not device's) station table */
  4018. iwl3945_clear_stations_table(priv);
  4019. priv->data_retry_limit = -1;
  4020. priv->ieee_channels = NULL;
  4021. priv->ieee_rates = NULL;
  4022. priv->band = IEEE80211_BAND_2GHZ;
  4023. priv->iw_mode = NL80211_IFTYPE_STATION;
  4024. iwl_reset_qos(priv);
  4025. priv->qos_data.qos_active = 0;
  4026. priv->qos_data.qos_cap.val = 0;
  4027. priv->rates_mask = IWL_RATES_MASK;
  4028. /* If power management is turned on, default to CAM mode */
  4029. priv->power_mode = IWL_POWER_MODE_CAM;
  4030. priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
  4031. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  4032. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  4033. eeprom->version);
  4034. ret = -EINVAL;
  4035. goto err;
  4036. }
  4037. ret = iwl_init_channel_map(priv);
  4038. if (ret) {
  4039. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  4040. goto err;
  4041. }
  4042. /* Set up txpower settings in driver for all channels */
  4043. if (iwl3945_txpower_set_from_eeprom(priv)) {
  4044. ret = -EIO;
  4045. goto err_free_channel_map;
  4046. }
  4047. ret = iwlcore_init_geos(priv);
  4048. if (ret) {
  4049. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  4050. goto err_free_channel_map;
  4051. }
  4052. iwl3945_init_hw_rates(priv, priv->ieee_rates);
  4053. return 0;
  4054. err_free_channel_map:
  4055. iwl_free_channel_map(priv);
  4056. err:
  4057. return ret;
  4058. }
  4059. static int iwl3945_setup_mac(struct iwl_priv *priv)
  4060. {
  4061. int ret;
  4062. struct ieee80211_hw *hw = priv->hw;
  4063. hw->rate_control_algorithm = "iwl-3945-rs";
  4064. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  4065. /* Tell mac80211 our characteristics */
  4066. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  4067. IEEE80211_HW_NOISE_DBM |
  4068. IEEE80211_HW_SPECTRUM_MGMT;
  4069. hw->wiphy->interface_modes =
  4070. BIT(NL80211_IFTYPE_STATION) |
  4071. BIT(NL80211_IFTYPE_ADHOC);
  4072. hw->wiphy->custom_regulatory = true;
  4073. hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
  4074. /* Default value; 4 EDCA QOS priorities */
  4075. hw->queues = 4;
  4076. hw->conf.beacon_int = 100;
  4077. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  4078. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  4079. &priv->bands[IEEE80211_BAND_2GHZ];
  4080. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  4081. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  4082. &priv->bands[IEEE80211_BAND_5GHZ];
  4083. ret = ieee80211_register_hw(priv->hw);
  4084. if (ret) {
  4085. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  4086. return ret;
  4087. }
  4088. priv->mac80211_registered = 1;
  4089. return 0;
  4090. }
  4091. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  4092. {
  4093. int err = 0;
  4094. struct iwl_priv *priv;
  4095. struct ieee80211_hw *hw;
  4096. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  4097. struct iwl3945_eeprom *eeprom;
  4098. unsigned long flags;
  4099. /***********************
  4100. * 1. Allocating HW data
  4101. * ********************/
  4102. /* mac80211 allocates memory for this device instance, including
  4103. * space for this driver's private structure */
  4104. hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
  4105. if (hw == NULL) {
  4106. printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
  4107. err = -ENOMEM;
  4108. goto out;
  4109. }
  4110. priv = hw->priv;
  4111. SET_IEEE80211_DEV(hw, &pdev->dev);
  4112. if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
  4113. (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
  4114. IWL_ERR(priv,
  4115. "invalid queues_num, should be between %d and %d\n",
  4116. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  4117. err = -EINVAL;
  4118. goto out_ieee80211_free_hw;
  4119. }
  4120. /*
  4121. * Disabling hardware scan means that mac80211 will perform scans
  4122. * "the hard way", rather than using device's scan.
  4123. */
  4124. if (iwl3945_mod_params.disable_hw_scan) {
  4125. IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
  4126. iwl3945_hw_ops.hw_scan = NULL;
  4127. }
  4128. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  4129. priv->cfg = cfg;
  4130. priv->pci_dev = pdev;
  4131. #ifdef CONFIG_IWLWIFI_DEBUG
  4132. priv->debug_level = iwl3945_mod_params.debug;
  4133. atomic_set(&priv->restrict_refcnt, 0);
  4134. #endif
  4135. /***************************
  4136. * 2. Initializing PCI bus
  4137. * *************************/
  4138. if (pci_enable_device(pdev)) {
  4139. err = -ENODEV;
  4140. goto out_ieee80211_free_hw;
  4141. }
  4142. pci_set_master(pdev);
  4143. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  4144. if (!err)
  4145. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  4146. if (err) {
  4147. IWL_WARN(priv, "No suitable DMA available.\n");
  4148. goto out_pci_disable_device;
  4149. }
  4150. pci_set_drvdata(pdev, priv);
  4151. err = pci_request_regions(pdev, DRV_NAME);
  4152. if (err)
  4153. goto out_pci_disable_device;
  4154. /***********************
  4155. * 3. Read REV Register
  4156. * ********************/
  4157. priv->hw_base = pci_iomap(pdev, 0, 0);
  4158. if (!priv->hw_base) {
  4159. err = -ENODEV;
  4160. goto out_pci_release_regions;
  4161. }
  4162. IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
  4163. (unsigned long long) pci_resource_len(pdev, 0));
  4164. IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
  4165. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  4166. * PCI Tx retries from interfering with C3 CPU state */
  4167. pci_write_config_byte(pdev, 0x41, 0x00);
  4168. /* amp init */
  4169. err = priv->cfg->ops->lib->apm_ops.init(priv);
  4170. if (err < 0) {
  4171. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  4172. goto out_iounmap;
  4173. }
  4174. /***********************
  4175. * 4. Read EEPROM
  4176. * ********************/
  4177. /* Read the EEPROM */
  4178. err = iwl_eeprom_init(priv);
  4179. if (err) {
  4180. IWL_ERR(priv, "Unable to init EEPROM\n");
  4181. goto out_iounmap;
  4182. }
  4183. /* MAC Address location in EEPROM same for 3945/4965 */
  4184. eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  4185. memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
  4186. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
  4187. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  4188. /***********************
  4189. * 5. Setup HW Constants
  4190. * ********************/
  4191. /* Device-specific setup */
  4192. if (iwl3945_hw_set_hw_params(priv)) {
  4193. IWL_ERR(priv, "failed to set hw settings\n");
  4194. goto out_eeprom_free;
  4195. }
  4196. /***********************
  4197. * 6. Setup priv
  4198. * ********************/
  4199. err = iwl3945_init_drv(priv);
  4200. if (err) {
  4201. IWL_ERR(priv, "initializing driver failed\n");
  4202. goto out_unset_hw_params;
  4203. }
  4204. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  4205. priv->cfg->name);
  4206. /***********************************
  4207. * 7. Initialize Module Parameters
  4208. * **********************************/
  4209. /* Initialize module parameter values here */
  4210. /* Disable radio (SW RF KILL) via parameter when loading driver */
  4211. if (iwl3945_mod_params.disable) {
  4212. set_bit(STATUS_RF_KILL_SW, &priv->status);
  4213. IWL_DEBUG_INFO(priv, "Radio disabled.\n");
  4214. }
  4215. /***********************
  4216. * 8. Setup Services
  4217. * ********************/
  4218. spin_lock_irqsave(&priv->lock, flags);
  4219. iwl_disable_interrupts(priv);
  4220. spin_unlock_irqrestore(&priv->lock, flags);
  4221. pci_enable_msi(priv->pci_dev);
  4222. err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
  4223. DRV_NAME, priv);
  4224. if (err) {
  4225. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  4226. goto out_disable_msi;
  4227. }
  4228. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4229. if (err) {
  4230. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  4231. goto out_release_irq;
  4232. }
  4233. iwl_set_rxon_channel(priv,
  4234. &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
  4235. iwl3945_setup_deferred_work(priv);
  4236. iwl3945_setup_rx_handlers(priv);
  4237. /*********************************
  4238. * 9. Setup and Register mac80211
  4239. * *******************************/
  4240. iwl_enable_interrupts(priv);
  4241. err = iwl3945_setup_mac(priv);
  4242. if (err)
  4243. goto out_remove_sysfs;
  4244. err = iwl_rfkill_init(priv);
  4245. if (err)
  4246. IWL_ERR(priv, "Unable to initialize RFKILL system. "
  4247. "Ignoring error: %d\n", err);
  4248. else
  4249. iwl_rfkill_set_hw_state(priv);
  4250. /* Start monitoring the killswitch */
  4251. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  4252. 2 * HZ);
  4253. return 0;
  4254. out_remove_sysfs:
  4255. destroy_workqueue(priv->workqueue);
  4256. priv->workqueue = NULL;
  4257. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4258. out_release_irq:
  4259. free_irq(priv->pci_dev->irq, priv);
  4260. out_disable_msi:
  4261. pci_disable_msi(priv->pci_dev);
  4262. iwlcore_free_geos(priv);
  4263. iwl_free_channel_map(priv);
  4264. out_unset_hw_params:
  4265. iwl3945_unset_hw_params(priv);
  4266. out_eeprom_free:
  4267. iwl_eeprom_free(priv);
  4268. out_iounmap:
  4269. pci_iounmap(pdev, priv->hw_base);
  4270. out_pci_release_regions:
  4271. pci_release_regions(pdev);
  4272. out_pci_disable_device:
  4273. pci_set_drvdata(pdev, NULL);
  4274. pci_disable_device(pdev);
  4275. out_ieee80211_free_hw:
  4276. ieee80211_free_hw(priv->hw);
  4277. out:
  4278. return err;
  4279. }
  4280. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  4281. {
  4282. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4283. unsigned long flags;
  4284. if (!priv)
  4285. return;
  4286. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  4287. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4288. if (priv->mac80211_registered) {
  4289. ieee80211_unregister_hw(priv->hw);
  4290. priv->mac80211_registered = 0;
  4291. } else {
  4292. iwl3945_down(priv);
  4293. }
  4294. /* make sure we flush any pending irq or
  4295. * tasklet for the driver
  4296. */
  4297. spin_lock_irqsave(&priv->lock, flags);
  4298. iwl_disable_interrupts(priv);
  4299. spin_unlock_irqrestore(&priv->lock, flags);
  4300. iwl_synchronize_irq(priv);
  4301. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  4302. iwl_rfkill_unregister(priv);
  4303. cancel_delayed_work_sync(&priv->rfkill_poll);
  4304. iwl3945_dealloc_ucode_pci(priv);
  4305. if (priv->rxq.bd)
  4306. iwl3945_rx_queue_free(priv, &priv->rxq);
  4307. iwl3945_hw_txq_ctx_free(priv);
  4308. iwl3945_unset_hw_params(priv);
  4309. iwl3945_clear_stations_table(priv);
  4310. /*netif_stop_queue(dev); */
  4311. flush_workqueue(priv->workqueue);
  4312. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  4313. * priv->workqueue... so we can't take down the workqueue
  4314. * until now... */
  4315. destroy_workqueue(priv->workqueue);
  4316. priv->workqueue = NULL;
  4317. free_irq(pdev->irq, priv);
  4318. pci_disable_msi(pdev);
  4319. pci_iounmap(pdev, priv->hw_base);
  4320. pci_release_regions(pdev);
  4321. pci_disable_device(pdev);
  4322. pci_set_drvdata(pdev, NULL);
  4323. iwl_free_channel_map(priv);
  4324. iwlcore_free_geos(priv);
  4325. kfree(priv->scan);
  4326. if (priv->ibss_beacon)
  4327. dev_kfree_skb(priv->ibss_beacon);
  4328. ieee80211_free_hw(priv->hw);
  4329. }
  4330. #ifdef CONFIG_PM
  4331. static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  4332. {
  4333. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4334. if (priv->is_open) {
  4335. set_bit(STATUS_IN_SUSPEND, &priv->status);
  4336. iwl3945_mac_stop(priv->hw);
  4337. priv->is_open = 1;
  4338. }
  4339. pci_save_state(pdev);
  4340. pci_disable_device(pdev);
  4341. pci_set_power_state(pdev, PCI_D3hot);
  4342. return 0;
  4343. }
  4344. static int iwl3945_pci_resume(struct pci_dev *pdev)
  4345. {
  4346. struct iwl_priv *priv = pci_get_drvdata(pdev);
  4347. int ret;
  4348. pci_set_power_state(pdev, PCI_D0);
  4349. ret = pci_enable_device(pdev);
  4350. if (ret)
  4351. return ret;
  4352. pci_restore_state(pdev);
  4353. if (priv->is_open)
  4354. iwl3945_mac_start(priv->hw);
  4355. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  4356. return 0;
  4357. }
  4358. #endif /* CONFIG_PM */
  4359. /*****************************************************************************
  4360. *
  4361. * driver and module entry point
  4362. *
  4363. *****************************************************************************/
  4364. static struct pci_driver iwl3945_driver = {
  4365. .name = DRV_NAME,
  4366. .id_table = iwl3945_hw_card_ids,
  4367. .probe = iwl3945_pci_probe,
  4368. .remove = __devexit_p(iwl3945_pci_remove),
  4369. #ifdef CONFIG_PM
  4370. .suspend = iwl3945_pci_suspend,
  4371. .resume = iwl3945_pci_resume,
  4372. #endif
  4373. };
  4374. static int __init iwl3945_init(void)
  4375. {
  4376. int ret;
  4377. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  4378. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  4379. ret = iwl3945_rate_control_register();
  4380. if (ret) {
  4381. printk(KERN_ERR DRV_NAME
  4382. "Unable to register rate control algorithm: %d\n", ret);
  4383. return ret;
  4384. }
  4385. ret = pci_register_driver(&iwl3945_driver);
  4386. if (ret) {
  4387. printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
  4388. goto error_register;
  4389. }
  4390. return ret;
  4391. error_register:
  4392. iwl3945_rate_control_unregister();
  4393. return ret;
  4394. }
  4395. static void __exit iwl3945_exit(void)
  4396. {
  4397. pci_unregister_driver(&iwl3945_driver);
  4398. iwl3945_rate_control_unregister();
  4399. }
  4400. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  4401. module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
  4402. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  4403. module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
  4404. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  4405. module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
  4406. MODULE_PARM_DESC(swcrypto,
  4407. "using software crypto (default 1 [software])\n");
  4408. module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
  4409. MODULE_PARM_DESC(debug, "debug output mask");
  4410. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
  4411. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  4412. module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
  4413. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  4414. module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
  4415. MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
  4416. module_exit(iwl3945_exit);
  4417. module_init(iwl3945_init);