3945-mac.c 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/slab.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/if_arp.h>
  44. #include <net/ieee80211_radiotap.h>
  45. #include <net/mac80211.h>
  46. #include <asm/div64.h>
  47. #define DRV_NAME "iwl3945"
  48. #include "commands.h"
  49. #include "common.h"
  50. #include "3945.h"
  51. #include "iwl-spectrum.h"
  52. /*
  53. * module name, copyright, version, etc.
  54. */
  55. #define DRV_DESCRIPTION \
  56. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  57. #ifdef CONFIG_IWLEGACY_DEBUG
  58. #define VD "d"
  59. #else
  60. #define VD
  61. #endif
  62. /*
  63. * add "s" to indicate spectrum measurement included.
  64. * we add it here to be consistent with previous releases in which
  65. * this was configurable.
  66. */
  67. #define DRV_VERSION IWLWIFI_VERSION VD "s"
  68. #define DRV_COPYRIGHT "Copyright(c) 2003-2011 Intel Corporation"
  69. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  70. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  71. MODULE_VERSION(DRV_VERSION);
  72. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  73. MODULE_LICENSE("GPL");
  74. /* module parameters */
  75. struct il_mod_params il3945_mod_params = {
  76. .sw_crypto = 1,
  77. .restart_fw = 1,
  78. .disable_hw_scan = 1,
  79. /* the rest are 0 by default */
  80. };
  81. /**
  82. * il3945_get_antenna_flags - Get antenna flags for RXON command
  83. * @il: eeprom and antenna fields are used to determine antenna flags
  84. *
  85. * il->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  86. * il3945_mod_params.antenna specifies the antenna diversity mode:
  87. *
  88. * IL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  89. * IL_ANTENNA_MAIN - Force MAIN antenna
  90. * IL_ANTENNA_AUX - Force AUX antenna
  91. */
  92. __le32
  93. il3945_get_antenna_flags(const struct il_priv *il)
  94. {
  95. struct il3945_eeprom *eeprom = (struct il3945_eeprom *)il->eeprom;
  96. switch (il3945_mod_params.antenna) {
  97. case IL_ANTENNA_DIVERSITY:
  98. return 0;
  99. case IL_ANTENNA_MAIN:
  100. if (eeprom->antenna_switch_type)
  101. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  102. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  103. case IL_ANTENNA_AUX:
  104. if (eeprom->antenna_switch_type)
  105. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  106. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  107. }
  108. /* bad antenna selector value */
  109. IL_ERR("Bad antenna selector value (0x%x)\n",
  110. il3945_mod_params.antenna);
  111. return 0; /* "diversity" is default if error */
  112. }
  113. static int
  114. il3945_set_ccmp_dynamic_key_info(struct il_priv *il,
  115. struct ieee80211_key_conf *keyconf, u8 sta_id)
  116. {
  117. unsigned long flags;
  118. __le16 key_flags = 0;
  119. int ret;
  120. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  121. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  122. if (sta_id == il->hw_params.bcast_id)
  123. key_flags |= STA_KEY_MULTICAST_MSK;
  124. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  125. keyconf->hw_key_idx = keyconf->keyidx;
  126. key_flags &= ~STA_KEY_FLG_INVALID;
  127. spin_lock_irqsave(&il->sta_lock, flags);
  128. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  129. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  130. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
  131. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
  132. if ((il->stations[sta_id].sta.key.
  133. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  134. il->stations[sta_id].sta.key.key_offset =
  135. il_get_free_ucode_key_idx(il);
  136. /* else, we are overriding an existing key => no need to allocated room
  137. * in uCode. */
  138. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  139. "no space for a new key");
  140. il->stations[sta_id].sta.key.key_flags = key_flags;
  141. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  142. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  143. D_INFO("hwcrypto: modify ucode station key info\n");
  144. ret = il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  145. spin_unlock_irqrestore(&il->sta_lock, flags);
  146. return ret;
  147. }
  148. static int
  149. il3945_set_tkip_dynamic_key_info(struct il_priv *il,
  150. struct ieee80211_key_conf *keyconf, u8 sta_id)
  151. {
  152. return -EOPNOTSUPP;
  153. }
  154. static int
  155. il3945_set_wep_dynamic_key_info(struct il_priv *il,
  156. struct ieee80211_key_conf *keyconf, u8 sta_id)
  157. {
  158. return -EOPNOTSUPP;
  159. }
  160. static int
  161. il3945_clear_sta_key_info(struct il_priv *il, u8 sta_id)
  162. {
  163. unsigned long flags;
  164. struct il_addsta_cmd sta_cmd;
  165. spin_lock_irqsave(&il->sta_lock, flags);
  166. memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
  167. memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
  168. il->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  169. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  170. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  171. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  172. sizeof(struct il_addsta_cmd));
  173. spin_unlock_irqrestore(&il->sta_lock, flags);
  174. D_INFO("hwcrypto: clear ucode station key info\n");
  175. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  176. }
  177. static int
  178. il3945_set_dynamic_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
  179. u8 sta_id)
  180. {
  181. int ret = 0;
  182. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  183. switch (keyconf->cipher) {
  184. case WLAN_CIPHER_SUITE_CCMP:
  185. ret = il3945_set_ccmp_dynamic_key_info(il, keyconf, sta_id);
  186. break;
  187. case WLAN_CIPHER_SUITE_TKIP:
  188. ret = il3945_set_tkip_dynamic_key_info(il, keyconf, sta_id);
  189. break;
  190. case WLAN_CIPHER_SUITE_WEP40:
  191. case WLAN_CIPHER_SUITE_WEP104:
  192. ret = il3945_set_wep_dynamic_key_info(il, keyconf, sta_id);
  193. break;
  194. default:
  195. IL_ERR("Unknown alg: %s alg=%x\n", __func__, keyconf->cipher);
  196. ret = -EINVAL;
  197. }
  198. D_WEP("Set dynamic key: alg=%x len=%d idx=%d sta=%d ret=%d\n",
  199. keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
  200. return ret;
  201. }
  202. static int
  203. il3945_remove_static_key(struct il_priv *il)
  204. {
  205. int ret = -EOPNOTSUPP;
  206. return ret;
  207. }
  208. static int
  209. il3945_set_static_key(struct il_priv *il, struct ieee80211_key_conf *key)
  210. {
  211. if (key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  212. key->cipher == WLAN_CIPHER_SUITE_WEP104)
  213. return -EOPNOTSUPP;
  214. IL_ERR("Static key invalid: cipher %x\n", key->cipher);
  215. return -EINVAL;
  216. }
  217. static void
  218. il3945_clear_free_frames(struct il_priv *il)
  219. {
  220. struct list_head *element;
  221. D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
  222. while (!list_empty(&il->free_frames)) {
  223. element = il->free_frames.next;
  224. list_del(element);
  225. kfree(list_entry(element, struct il3945_frame, list));
  226. il->frames_count--;
  227. }
  228. if (il->frames_count) {
  229. IL_WARN("%d frames still in use. Did we lose one?\n",
  230. il->frames_count);
  231. il->frames_count = 0;
  232. }
  233. }
  234. static struct il3945_frame *
  235. il3945_get_free_frame(struct il_priv *il)
  236. {
  237. struct il3945_frame *frame;
  238. struct list_head *element;
  239. if (list_empty(&il->free_frames)) {
  240. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  241. if (!frame) {
  242. IL_ERR("Could not allocate frame!\n");
  243. return NULL;
  244. }
  245. il->frames_count++;
  246. return frame;
  247. }
  248. element = il->free_frames.next;
  249. list_del(element);
  250. return list_entry(element, struct il3945_frame, list);
  251. }
  252. static void
  253. il3945_free_frame(struct il_priv *il, struct il3945_frame *frame)
  254. {
  255. memset(frame, 0, sizeof(*frame));
  256. list_add(&frame->list, &il->free_frames);
  257. }
  258. unsigned int
  259. il3945_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
  260. int left)
  261. {
  262. if (!il_is_associated(il) || !il->beacon_skb)
  263. return 0;
  264. if (il->beacon_skb->len > left)
  265. return 0;
  266. memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
  267. return il->beacon_skb->len;
  268. }
  269. static int
  270. il3945_send_beacon_cmd(struct il_priv *il)
  271. {
  272. struct il3945_frame *frame;
  273. unsigned int frame_size;
  274. int rc;
  275. u8 rate;
  276. frame = il3945_get_free_frame(il);
  277. if (!frame) {
  278. IL_ERR("Could not obtain free frame buffer for beacon "
  279. "command.\n");
  280. return -ENOMEM;
  281. }
  282. rate = il_get_lowest_plcp(il);
  283. frame_size = il3945_hw_get_beacon_cmd(il, frame, rate);
  284. rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
  285. il3945_free_frame(il, frame);
  286. return rc;
  287. }
  288. static void
  289. il3945_unset_hw_params(struct il_priv *il)
  290. {
  291. if (il->_3945.shared_virt)
  292. dma_free_coherent(&il->pci_dev->dev,
  293. sizeof(struct il3945_shared),
  294. il->_3945.shared_virt, il->_3945.shared_phys);
  295. }
  296. static void
  297. il3945_build_tx_cmd_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
  298. struct il_device_cmd *cmd,
  299. struct sk_buff *skb_frag, int sta_id)
  300. {
  301. struct il3945_tx_cmd *tx_cmd = (struct il3945_tx_cmd *)cmd->cmd.payload;
  302. struct il_hw_key *keyinfo = &il->stations[sta_id].keyinfo;
  303. tx_cmd->sec_ctl = 0;
  304. switch (keyinfo->cipher) {
  305. case WLAN_CIPHER_SUITE_CCMP:
  306. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  307. memcpy(tx_cmd->key, keyinfo->key, keyinfo->keylen);
  308. D_TX("tx_cmd with AES hwcrypto\n");
  309. break;
  310. case WLAN_CIPHER_SUITE_TKIP:
  311. break;
  312. case WLAN_CIPHER_SUITE_WEP104:
  313. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  314. /* fall through */
  315. case WLAN_CIPHER_SUITE_WEP40:
  316. tx_cmd->sec_ctl |=
  317. TX_CMD_SEC_WEP | (info->control.hw_key->
  318. hw_key_idx & TX_CMD_SEC_MSK) <<
  319. TX_CMD_SEC_SHIFT;
  320. memcpy(&tx_cmd->key[3], keyinfo->key, keyinfo->keylen);
  321. D_TX("Configuring packet for WEP encryption " "with key %d\n",
  322. info->control.hw_key->hw_key_idx);
  323. break;
  324. default:
  325. IL_ERR("Unknown encode cipher %x\n", keyinfo->cipher);
  326. break;
  327. }
  328. }
  329. /*
  330. * handle build C_TX command notification.
  331. */
  332. static void
  333. il3945_build_tx_cmd_basic(struct il_priv *il, struct il_device_cmd *cmd,
  334. struct ieee80211_tx_info *info,
  335. struct ieee80211_hdr *hdr, u8 std_id)
  336. {
  337. struct il3945_tx_cmd *tx_cmd = (struct il3945_tx_cmd *)cmd->cmd.payload;
  338. __le32 tx_flags = tx_cmd->tx_flags;
  339. __le16 fc = hdr->frame_control;
  340. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  341. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  342. tx_flags |= TX_CMD_FLG_ACK_MSK;
  343. if (ieee80211_is_mgmt(fc))
  344. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  345. if (ieee80211_is_probe_resp(fc) &&
  346. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  347. tx_flags |= TX_CMD_FLG_TSF_MSK;
  348. } else {
  349. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  350. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  351. }
  352. tx_cmd->sta_id = std_id;
  353. if (ieee80211_has_morefrags(fc))
  354. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  355. if (ieee80211_is_data_qos(fc)) {
  356. u8 *qc = ieee80211_get_qos_ctl(hdr);
  357. tx_cmd->tid_tspec = qc[0] & 0xf;
  358. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  359. } else {
  360. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  361. }
  362. il_tx_cmd_protection(il, info, fc, &tx_flags);
  363. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  364. if (ieee80211_is_mgmt(fc)) {
  365. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  366. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  367. else
  368. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  369. } else {
  370. tx_cmd->timeout.pm_frame_timeout = 0;
  371. }
  372. tx_cmd->driver_txop = 0;
  373. tx_cmd->tx_flags = tx_flags;
  374. tx_cmd->next_frame_len = 0;
  375. }
  376. /*
  377. * start C_TX command process
  378. */
  379. static int
  380. il3945_tx_skb(struct il_priv *il,
  381. struct ieee80211_sta *sta,
  382. struct sk_buff *skb)
  383. {
  384. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  385. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  386. struct il3945_tx_cmd *tx_cmd;
  387. struct il_tx_queue *txq = NULL;
  388. struct il_queue *q = NULL;
  389. struct il_device_cmd *out_cmd;
  390. struct il_cmd_meta *out_meta;
  391. dma_addr_t phys_addr;
  392. dma_addr_t txcmd_phys;
  393. int txq_id = skb_get_queue_mapping(skb);
  394. u16 len, idx, hdr_len;
  395. u16 firstlen, secondlen;
  396. u8 id;
  397. u8 unicast;
  398. u8 sta_id;
  399. u8 tid = 0;
  400. __le16 fc;
  401. u8 wait_write_ptr = 0;
  402. unsigned long flags;
  403. spin_lock_irqsave(&il->lock, flags);
  404. if (il_is_rfkill(il)) {
  405. D_DROP("Dropping - RF KILL\n");
  406. goto drop_unlock;
  407. }
  408. if ((ieee80211_get_tx_rate(il->hw, info)->hw_value & 0xFF) ==
  409. IL_INVALID_RATE) {
  410. IL_ERR("ERROR: No TX rate available.\n");
  411. goto drop_unlock;
  412. }
  413. unicast = !is_multicast_ether_addr(hdr->addr1);
  414. id = 0;
  415. fc = hdr->frame_control;
  416. #ifdef CONFIG_IWLEGACY_DEBUG
  417. if (ieee80211_is_auth(fc))
  418. D_TX("Sending AUTH frame\n");
  419. else if (ieee80211_is_assoc_req(fc))
  420. D_TX("Sending ASSOC frame\n");
  421. else if (ieee80211_is_reassoc_req(fc))
  422. D_TX("Sending REASSOC frame\n");
  423. #endif
  424. spin_unlock_irqrestore(&il->lock, flags);
  425. hdr_len = ieee80211_hdrlen(fc);
  426. /* Find idx into station table for destination station */
  427. sta_id = il_sta_id_or_broadcast(il, sta);
  428. if (sta_id == IL_INVALID_STATION) {
  429. D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
  430. goto drop;
  431. }
  432. D_RATE("station Id %d\n", sta_id);
  433. if (ieee80211_is_data_qos(fc)) {
  434. u8 *qc = ieee80211_get_qos_ctl(hdr);
  435. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  436. if (unlikely(tid >= MAX_TID_COUNT))
  437. goto drop;
  438. }
  439. /* Descriptor for chosen Tx queue */
  440. txq = &il->txq[txq_id];
  441. q = &txq->q;
  442. if ((il_queue_space(q) < q->high_mark))
  443. goto drop;
  444. spin_lock_irqsave(&il->lock, flags);
  445. idx = il_get_cmd_idx(q, q->write_ptr, 0);
  446. txq->skbs[q->write_ptr] = skb;
  447. /* Init first empty entry in queue's array of Tx/cmd buffers */
  448. out_cmd = txq->cmd[idx];
  449. out_meta = &txq->meta[idx];
  450. tx_cmd = (struct il3945_tx_cmd *)out_cmd->cmd.payload;
  451. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  452. memset(tx_cmd, 0, sizeof(*tx_cmd));
  453. /*
  454. * Set up the Tx-command (not MAC!) header.
  455. * Store the chosen Tx queue and TFD idx within the sequence field;
  456. * after Tx, uCode's Tx response will return this value so driver can
  457. * locate the frame within the tx queue and do post-tx processing.
  458. */
  459. out_cmd->hdr.cmd = C_TX;
  460. out_cmd->hdr.sequence =
  461. cpu_to_le16((u16)
  462. (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
  463. /* Copy MAC header from skb into command buffer */
  464. memcpy(tx_cmd->hdr, hdr, hdr_len);
  465. if (info->control.hw_key)
  466. il3945_build_tx_cmd_hwcrypto(il, info, out_cmd, skb, sta_id);
  467. /* TODO need this for burst mode later on */
  468. il3945_build_tx_cmd_basic(il, out_cmd, info, hdr, sta_id);
  469. il3945_hw_build_tx_cmd_rate(il, out_cmd, info, hdr, sta_id);
  470. /* Total # bytes to be transmitted */
  471. tx_cmd->len = cpu_to_le16((u16) skb->len);
  472. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  473. tx_cmd->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  474. /*
  475. * Use the first empty entry in this queue's command buffer array
  476. * to contain the Tx command and MAC header concatenated together
  477. * (payload data will be in another buffer).
  478. * Size of this varies, due to varying MAC header length.
  479. * If end is not dword aligned, we'll have 2 extra bytes at the end
  480. * of the MAC header (device reads on dword boundaries).
  481. * We'll tell device about this padding later.
  482. */
  483. len =
  484. sizeof(struct il3945_tx_cmd) + sizeof(struct il_cmd_header) +
  485. hdr_len;
  486. firstlen = (len + 3) & ~3;
  487. /* Physical address of this Tx command's header (not MAC header!),
  488. * within command buffer array. */
  489. txcmd_phys =
  490. pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
  491. PCI_DMA_TODEVICE);
  492. if (unlikely(pci_dma_mapping_error(il->pci_dev, txcmd_phys)))
  493. goto drop_unlock;
  494. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  495. * if any (802.11 null frames have no payload). */
  496. secondlen = skb->len - hdr_len;
  497. if (secondlen > 0) {
  498. phys_addr =
  499. pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
  500. PCI_DMA_TODEVICE);
  501. if (unlikely(pci_dma_mapping_error(il->pci_dev, phys_addr)))
  502. goto drop_unlock;
  503. }
  504. /* Add buffer containing Tx command and MAC(!) header to TFD's
  505. * first entry */
  506. il->ops->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen, 1, 0);
  507. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  508. dma_unmap_len_set(out_meta, len, firstlen);
  509. if (secondlen > 0)
  510. il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, secondlen, 0,
  511. U32_PAD(secondlen));
  512. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  513. txq->need_update = 1;
  514. } else {
  515. wait_write_ptr = 1;
  516. txq->need_update = 0;
  517. }
  518. il_update_stats(il, true, fc, skb->len);
  519. D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
  520. D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  521. il_print_hex_dump(il, IL_DL_TX, tx_cmd, sizeof(*tx_cmd));
  522. il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr,
  523. ieee80211_hdrlen(fc));
  524. /* Tell device the write idx *just past* this latest filled TFD */
  525. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  526. il_txq_update_write_ptr(il, txq);
  527. spin_unlock_irqrestore(&il->lock, flags);
  528. if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
  529. if (wait_write_ptr) {
  530. spin_lock_irqsave(&il->lock, flags);
  531. txq->need_update = 1;
  532. il_txq_update_write_ptr(il, txq);
  533. spin_unlock_irqrestore(&il->lock, flags);
  534. }
  535. il_stop_queue(il, txq);
  536. }
  537. return 0;
  538. drop_unlock:
  539. spin_unlock_irqrestore(&il->lock, flags);
  540. drop:
  541. return -1;
  542. }
  543. static int
  544. il3945_get_measurement(struct il_priv *il,
  545. struct ieee80211_measurement_params *params, u8 type)
  546. {
  547. struct il_spectrum_cmd spectrum;
  548. struct il_rx_pkt *pkt;
  549. struct il_host_cmd cmd = {
  550. .id = C_SPECTRUM_MEASUREMENT,
  551. .data = (void *)&spectrum,
  552. .flags = CMD_WANT_SKB,
  553. };
  554. u32 add_time = le64_to_cpu(params->start_time);
  555. int rc;
  556. int spectrum_resp_status;
  557. int duration = le16_to_cpu(params->duration);
  558. if (il_is_associated(il))
  559. add_time =
  560. il_usecs_to_beacons(il,
  561. le64_to_cpu(params->start_time) -
  562. il->_3945.last_tsf,
  563. le16_to_cpu(il->timing.beacon_interval));
  564. memset(&spectrum, 0, sizeof(spectrum));
  565. spectrum.channel_count = cpu_to_le16(1);
  566. spectrum.flags =
  567. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  568. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  569. cmd.len = sizeof(spectrum);
  570. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  571. if (il_is_associated(il))
  572. spectrum.start_time =
  573. il_add_beacon_time(il, il->_3945.last_beacon_time, add_time,
  574. le16_to_cpu(il->timing.beacon_interval));
  575. else
  576. spectrum.start_time = 0;
  577. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  578. spectrum.channels[0].channel = params->channel;
  579. spectrum.channels[0].type = type;
  580. if (il->active.flags & RXON_FLG_BAND_24G_MSK)
  581. spectrum.flags |=
  582. RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK |
  583. RXON_FLG_TGG_PROTECT_MSK;
  584. rc = il_send_cmd_sync(il, &cmd);
  585. if (rc)
  586. return rc;
  587. pkt = (struct il_rx_pkt *)cmd.reply_page;
  588. if (pkt->hdr.flags & IL_CMD_FAILED_MSK) {
  589. IL_ERR("Bad return from N_RX_ON_ASSOC command\n");
  590. rc = -EIO;
  591. }
  592. spectrum_resp_status = le16_to_cpu(pkt->u.spectrum.status);
  593. switch (spectrum_resp_status) {
  594. case 0: /* Command will be handled */
  595. if (pkt->u.spectrum.id != 0xff) {
  596. D_INFO("Replaced existing measurement: %d\n",
  597. pkt->u.spectrum.id);
  598. il->measurement_status &= ~MEASUREMENT_READY;
  599. }
  600. il->measurement_status |= MEASUREMENT_ACTIVE;
  601. rc = 0;
  602. break;
  603. case 1: /* Command will not be handled */
  604. rc = -EAGAIN;
  605. break;
  606. }
  607. il_free_pages(il, cmd.reply_page);
  608. return rc;
  609. }
  610. static void
  611. il3945_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
  612. {
  613. struct il_rx_pkt *pkt = rxb_addr(rxb);
  614. struct il_alive_resp *palive;
  615. struct delayed_work *pwork;
  616. palive = &pkt->u.alive_frame;
  617. D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
  618. palive->is_valid, palive->ver_type, palive->ver_subtype);
  619. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  620. D_INFO("Initialization Alive received.\n");
  621. memcpy(&il->card_alive_init, &pkt->u.alive_frame,
  622. sizeof(struct il_alive_resp));
  623. pwork = &il->init_alive_start;
  624. } else {
  625. D_INFO("Runtime Alive received.\n");
  626. memcpy(&il->card_alive, &pkt->u.alive_frame,
  627. sizeof(struct il_alive_resp));
  628. pwork = &il->alive_start;
  629. il3945_disable_events(il);
  630. }
  631. /* We delay the ALIVE response by 5ms to
  632. * give the HW RF Kill time to activate... */
  633. if (palive->is_valid == UCODE_VALID_OK)
  634. queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
  635. else
  636. IL_WARN("uCode did not respond OK.\n");
  637. }
  638. static void
  639. il3945_hdl_add_sta(struct il_priv *il, struct il_rx_buf *rxb)
  640. {
  641. #ifdef CONFIG_IWLEGACY_DEBUG
  642. struct il_rx_pkt *pkt = rxb_addr(rxb);
  643. #endif
  644. D_RX("Received C_ADD_STA: 0x%02X\n", pkt->u.status);
  645. }
  646. static void
  647. il3945_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
  648. {
  649. struct il_rx_pkt *pkt = rxb_addr(rxb);
  650. struct il3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  651. #ifdef CONFIG_IWLEGACY_DEBUG
  652. u8 rate = beacon->beacon_notify_hdr.rate;
  653. D_RX("beacon status %x retries %d iss %d " "tsf %d %d rate %d\n",
  654. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  655. beacon->beacon_notify_hdr.failure_frame,
  656. le32_to_cpu(beacon->ibss_mgr_status),
  657. le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
  658. #endif
  659. il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  660. }
  661. /* Handle notification from uCode that card's power state is changing
  662. * due to software, hardware, or critical temperature RFKILL */
  663. static void
  664. il3945_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
  665. {
  666. struct il_rx_pkt *pkt = rxb_addr(rxb);
  667. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  668. unsigned long status = il->status;
  669. IL_WARN("Card state received: HW:%s SW:%s\n",
  670. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  671. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  672. _il_wr(il, CSR_UCODE_DRV_GP1_SET, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  673. if (flags & HW_CARD_DISABLED)
  674. set_bit(S_RFKILL, &il->status);
  675. else
  676. clear_bit(S_RFKILL, &il->status);
  677. il_scan_cancel(il);
  678. if ((test_bit(S_RFKILL, &status) !=
  679. test_bit(S_RFKILL, &il->status)))
  680. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  681. test_bit(S_RFKILL, &il->status));
  682. else
  683. wake_up(&il->wait_command_queue);
  684. }
  685. /**
  686. * il3945_setup_handlers - Initialize Rx handler callbacks
  687. *
  688. * Setup the RX handlers for each of the reply types sent from the uCode
  689. * to the host.
  690. *
  691. * This function chains into the hardware specific files for them to setup
  692. * any hardware specific handlers as well.
  693. */
  694. static void
  695. il3945_setup_handlers(struct il_priv *il)
  696. {
  697. il->handlers[N_ALIVE] = il3945_hdl_alive;
  698. il->handlers[C_ADD_STA] = il3945_hdl_add_sta;
  699. il->handlers[N_ERROR] = il_hdl_error;
  700. il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
  701. il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
  702. il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
  703. il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
  704. il->handlers[N_BEACON] = il3945_hdl_beacon;
  705. /*
  706. * The same handler is used for both the REPLY to a discrete
  707. * stats request from the host as well as for the periodic
  708. * stats notifications (after received beacons) from the uCode.
  709. */
  710. il->handlers[C_STATS] = il3945_hdl_c_stats;
  711. il->handlers[N_STATS] = il3945_hdl_stats;
  712. il_setup_rx_scan_handlers(il);
  713. il->handlers[N_CARD_STATE] = il3945_hdl_card_state;
  714. /* Set up hardware specific Rx handlers */
  715. il3945_hw_handler_setup(il);
  716. }
  717. /************************** RX-FUNCTIONS ****************************/
  718. /*
  719. * Rx theory of operation
  720. *
  721. * The host allocates 32 DMA target addresses and passes the host address
  722. * to the firmware at register IL_RFDS_TBL_LOWER + N * RFD_SIZE where N is
  723. * 0 to 31
  724. *
  725. * Rx Queue Indexes
  726. * The host/firmware share two idx registers for managing the Rx buffers.
  727. *
  728. * The READ idx maps to the first position that the firmware may be writing
  729. * to -- the driver can read up to (but not including) this position and get
  730. * good data.
  731. * The READ idx is managed by the firmware once the card is enabled.
  732. *
  733. * The WRITE idx maps to the last position the driver has read from -- the
  734. * position preceding WRITE is the last slot the firmware can place a packet.
  735. *
  736. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  737. * WRITE = READ.
  738. *
  739. * During initialization, the host sets up the READ queue position to the first
  740. * IDX position, and WRITE to the last (READ - 1 wrapped)
  741. *
  742. * When the firmware places a packet in a buffer, it will advance the READ idx
  743. * and fire the RX interrupt. The driver can then query the READ idx and
  744. * process as many packets as possible, moving the WRITE idx forward as it
  745. * resets the Rx queue buffers with new memory.
  746. *
  747. * The management in the driver is as follows:
  748. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  749. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  750. * to replenish the iwl->rxq->rx_free.
  751. * + In il3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  752. * iwl->rxq is replenished and the READ IDX is updated (updating the
  753. * 'processed' and 'read' driver idxes as well)
  754. * + A received packet is processed and handed to the kernel network stack,
  755. * detached from the iwl->rxq. The driver 'processed' idx is updated.
  756. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  757. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  758. * IDX is not incremented and iwl->status(RX_STALLED) is set. If there
  759. * were enough free buffers and RX_STALLED is set it is cleared.
  760. *
  761. *
  762. * Driver sequence:
  763. *
  764. * il3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  765. * il3945_rx_queue_restock
  766. * il3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  767. * queue, updates firmware pointers, and updates
  768. * the WRITE idx. If insufficient rx_free buffers
  769. * are available, schedules il3945_rx_replenish
  770. *
  771. * -- enable interrupts --
  772. * ISR - il3945_rx() Detach il_rx_bufs from pool up to the
  773. * READ IDX, detaching the SKB from the pool.
  774. * Moves the packet buffer from queue to rx_used.
  775. * Calls il3945_rx_queue_restock to refill any empty
  776. * slots.
  777. * ...
  778. *
  779. */
  780. /**
  781. * il3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  782. */
  783. static inline __le32
  784. il3945_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
  785. {
  786. return cpu_to_le32((u32) dma_addr);
  787. }
  788. /**
  789. * il3945_rx_queue_restock - refill RX queue from pre-allocated pool
  790. *
  791. * If there are slots in the RX queue that need to be restocked,
  792. * and we have free pre-allocated buffers, fill the ranks as much
  793. * as we can, pulling from rx_free.
  794. *
  795. * This moves the 'write' idx forward to catch up with 'processed', and
  796. * also updates the memory address in the firmware to reference the new
  797. * target buffer.
  798. */
  799. static void
  800. il3945_rx_queue_restock(struct il_priv *il)
  801. {
  802. struct il_rx_queue *rxq = &il->rxq;
  803. struct list_head *element;
  804. struct il_rx_buf *rxb;
  805. unsigned long flags;
  806. int write;
  807. spin_lock_irqsave(&rxq->lock, flags);
  808. write = rxq->write & ~0x7;
  809. while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
  810. /* Get next free Rx buffer, remove from free list */
  811. element = rxq->rx_free.next;
  812. rxb = list_entry(element, struct il_rx_buf, list);
  813. list_del(element);
  814. /* Point to Rx buffer via next RBD in circular buffer */
  815. rxq->bd[rxq->write] =
  816. il3945_dma_addr2rbd_ptr(il, rxb->page_dma);
  817. rxq->queue[rxq->write] = rxb;
  818. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  819. rxq->free_count--;
  820. }
  821. spin_unlock_irqrestore(&rxq->lock, flags);
  822. /* If the pre-allocated buffer pool is dropping low, schedule to
  823. * refill it */
  824. if (rxq->free_count <= RX_LOW_WATERMARK)
  825. queue_work(il->workqueue, &il->rx_replenish);
  826. /* If we've added more space for the firmware to place data, tell it.
  827. * Increment device's write pointer in multiples of 8. */
  828. if (rxq->write_actual != (rxq->write & ~0x7) ||
  829. abs(rxq->write - rxq->read) > 7) {
  830. spin_lock_irqsave(&rxq->lock, flags);
  831. rxq->need_update = 1;
  832. spin_unlock_irqrestore(&rxq->lock, flags);
  833. il_rx_queue_update_write_ptr(il, rxq);
  834. }
  835. }
  836. /**
  837. * il3945_rx_replenish - Move all used packet from rx_used to rx_free
  838. *
  839. * When moving to rx_free an SKB is allocated for the slot.
  840. *
  841. * Also restock the Rx queue via il3945_rx_queue_restock.
  842. * This is called as a scheduled work item (except for during initialization)
  843. */
  844. static void
  845. il3945_rx_allocate(struct il_priv *il, gfp_t priority)
  846. {
  847. struct il_rx_queue *rxq = &il->rxq;
  848. struct list_head *element;
  849. struct il_rx_buf *rxb;
  850. struct page *page;
  851. dma_addr_t page_dma;
  852. unsigned long flags;
  853. gfp_t gfp_mask = priority;
  854. while (1) {
  855. spin_lock_irqsave(&rxq->lock, flags);
  856. if (list_empty(&rxq->rx_used)) {
  857. spin_unlock_irqrestore(&rxq->lock, flags);
  858. return;
  859. }
  860. spin_unlock_irqrestore(&rxq->lock, flags);
  861. if (rxq->free_count > RX_LOW_WATERMARK)
  862. gfp_mask |= __GFP_NOWARN;
  863. if (il->hw_params.rx_page_order > 0)
  864. gfp_mask |= __GFP_COMP;
  865. /* Alloc a new receive buffer */
  866. page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
  867. if (!page) {
  868. if (net_ratelimit())
  869. D_INFO("Failed to allocate SKB buffer.\n");
  870. if (rxq->free_count <= RX_LOW_WATERMARK &&
  871. net_ratelimit())
  872. IL_ERR("Failed to allocate SKB buffer with %0x."
  873. "Only %u free buffers remaining.\n",
  874. priority, rxq->free_count);
  875. /* We don't reschedule replenish work here -- we will
  876. * call the restock method and if it still needs
  877. * more buffers it will schedule replenish */
  878. break;
  879. }
  880. /* Get physical address of RB/SKB */
  881. page_dma =
  882. pci_map_page(il->pci_dev, page, 0,
  883. PAGE_SIZE << il->hw_params.rx_page_order,
  884. PCI_DMA_FROMDEVICE);
  885. if (unlikely(pci_dma_mapping_error(il->pci_dev, page_dma))) {
  886. __free_pages(page, il->hw_params.rx_page_order);
  887. break;
  888. }
  889. spin_lock_irqsave(&rxq->lock, flags);
  890. if (list_empty(&rxq->rx_used)) {
  891. spin_unlock_irqrestore(&rxq->lock, flags);
  892. pci_unmap_page(il->pci_dev, page_dma,
  893. PAGE_SIZE << il->hw_params.rx_page_order,
  894. PCI_DMA_FROMDEVICE);
  895. __free_pages(page, il->hw_params.rx_page_order);
  896. return;
  897. }
  898. element = rxq->rx_used.next;
  899. rxb = list_entry(element, struct il_rx_buf, list);
  900. list_del(element);
  901. rxb->page = page;
  902. rxb->page_dma = page_dma;
  903. list_add_tail(&rxb->list, &rxq->rx_free);
  904. rxq->free_count++;
  905. il->alloc_rxb_page++;
  906. spin_unlock_irqrestore(&rxq->lock, flags);
  907. }
  908. }
  909. void
  910. il3945_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
  911. {
  912. unsigned long flags;
  913. int i;
  914. spin_lock_irqsave(&rxq->lock, flags);
  915. INIT_LIST_HEAD(&rxq->rx_free);
  916. INIT_LIST_HEAD(&rxq->rx_used);
  917. /* Fill the rx_used queue with _all_ of the Rx buffers */
  918. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  919. /* In the reset function, these buffers may have been allocated
  920. * to an SKB, so we need to unmap and free potential storage */
  921. if (rxq->pool[i].page != NULL) {
  922. pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
  923. PAGE_SIZE << il->hw_params.rx_page_order,
  924. PCI_DMA_FROMDEVICE);
  925. __il_free_pages(il, rxq->pool[i].page);
  926. rxq->pool[i].page = NULL;
  927. }
  928. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  929. }
  930. /* Set us so that we have processed and used all buffers, but have
  931. * not restocked the Rx queue with fresh buffers */
  932. rxq->read = rxq->write = 0;
  933. rxq->write_actual = 0;
  934. rxq->free_count = 0;
  935. spin_unlock_irqrestore(&rxq->lock, flags);
  936. }
  937. void
  938. il3945_rx_replenish(void *data)
  939. {
  940. struct il_priv *il = data;
  941. unsigned long flags;
  942. il3945_rx_allocate(il, GFP_KERNEL);
  943. spin_lock_irqsave(&il->lock, flags);
  944. il3945_rx_queue_restock(il);
  945. spin_unlock_irqrestore(&il->lock, flags);
  946. }
  947. static void
  948. il3945_rx_replenish_now(struct il_priv *il)
  949. {
  950. il3945_rx_allocate(il, GFP_ATOMIC);
  951. il3945_rx_queue_restock(il);
  952. }
  953. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  954. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  955. * This free routine walks the list of POOL entries and if SKB is set to
  956. * non NULL it is unmapped and freed
  957. */
  958. static void
  959. il3945_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
  960. {
  961. int i;
  962. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  963. if (rxq->pool[i].page != NULL) {
  964. pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
  965. PAGE_SIZE << il->hw_params.rx_page_order,
  966. PCI_DMA_FROMDEVICE);
  967. __il_free_pages(il, rxq->pool[i].page);
  968. rxq->pool[i].page = NULL;
  969. }
  970. }
  971. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  972. rxq->bd_dma);
  973. dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
  974. rxq->rb_stts, rxq->rb_stts_dma);
  975. rxq->bd = NULL;
  976. rxq->rb_stts = NULL;
  977. }
  978. /* Convert linear signal-to-noise ratio into dB */
  979. static u8 ratio2dB[100] = {
  980. /* 0 1 2 3 4 5 6 7 8 9 */
  981. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  982. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  983. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  984. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  985. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  986. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  987. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  988. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  989. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  990. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  991. };
  992. /* Calculates a relative dB value from a ratio of linear
  993. * (i.e. not dB) signal levels.
  994. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  995. int
  996. il3945_calc_db_from_ratio(int sig_ratio)
  997. {
  998. /* 1000:1 or higher just report as 60 dB */
  999. if (sig_ratio >= 1000)
  1000. return 60;
  1001. /* 100:1 or higher, divide by 10 and use table,
  1002. * add 20 dB to make up for divide by 10 */
  1003. if (sig_ratio >= 100)
  1004. return 20 + (int)ratio2dB[sig_ratio / 10];
  1005. /* We shouldn't see this */
  1006. if (sig_ratio < 1)
  1007. return 0;
  1008. /* Use table for ratios 1:1 - 99:1 */
  1009. return (int)ratio2dB[sig_ratio];
  1010. }
  1011. /**
  1012. * il3945_rx_handle - Main entry function for receiving responses from uCode
  1013. *
  1014. * Uses the il->handlers callback function array to invoke
  1015. * the appropriate handlers, including command responses,
  1016. * frame-received notifications, and other notifications.
  1017. */
  1018. static void
  1019. il3945_rx_handle(struct il_priv *il)
  1020. {
  1021. struct il_rx_buf *rxb;
  1022. struct il_rx_pkt *pkt;
  1023. struct il_rx_queue *rxq = &il->rxq;
  1024. u32 r, i;
  1025. int reclaim;
  1026. unsigned long flags;
  1027. u8 fill_rx = 0;
  1028. u32 count = 8;
  1029. int total_empty = 0;
  1030. /* uCode's read idx (stored in shared DRAM) indicates the last Rx
  1031. * buffer that the driver may process (last buffer filled by ucode). */
  1032. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1033. i = rxq->read;
  1034. /* calculate total frames need to be restock after handling RX */
  1035. total_empty = r - rxq->write_actual;
  1036. if (total_empty < 0)
  1037. total_empty += RX_QUEUE_SIZE;
  1038. if (total_empty > (RX_QUEUE_SIZE / 2))
  1039. fill_rx = 1;
  1040. /* Rx interrupt, but nothing sent from uCode */
  1041. if (i == r)
  1042. D_RX("r = %d, i = %d\n", r, i);
  1043. while (i != r) {
  1044. int len;
  1045. rxb = rxq->queue[i];
  1046. /* If an RXB doesn't have a Rx queue slot associated with it,
  1047. * then a bug has been introduced in the queue refilling
  1048. * routines -- catch it here */
  1049. BUG_ON(rxb == NULL);
  1050. rxq->queue[i] = NULL;
  1051. pci_unmap_page(il->pci_dev, rxb->page_dma,
  1052. PAGE_SIZE << il->hw_params.rx_page_order,
  1053. PCI_DMA_FROMDEVICE);
  1054. pkt = rxb_addr(rxb);
  1055. len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
  1056. len += sizeof(u32); /* account for status word */
  1057. /* Reclaim a command buffer only if this packet is a response
  1058. * to a (driver-originated) command.
  1059. * If the packet (e.g. Rx frame) originated from uCode,
  1060. * there is no command buffer to reclaim.
  1061. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1062. * but apparently a few don't get set; catch them here. */
  1063. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1064. pkt->hdr.cmd != N_STATS && pkt->hdr.cmd != C_TX;
  1065. /* Based on type of command response or notification,
  1066. * handle those that need handling via function in
  1067. * handlers table. See il3945_setup_handlers() */
  1068. if (il->handlers[pkt->hdr.cmd]) {
  1069. D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
  1070. il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1071. il->isr_stats.handlers[pkt->hdr.cmd]++;
  1072. il->handlers[pkt->hdr.cmd] (il, rxb);
  1073. } else {
  1074. /* No handling needed */
  1075. D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
  1076. i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1077. }
  1078. /*
  1079. * XXX: After here, we should always check rxb->page
  1080. * against NULL before touching it or its virtual
  1081. * memory (pkt). Because some handler might have
  1082. * already taken or freed the pages.
  1083. */
  1084. if (reclaim) {
  1085. /* Invoke any callbacks, transfer the buffer to caller,
  1086. * and fire off the (possibly) blocking il_send_cmd()
  1087. * as we reclaim the driver command queue */
  1088. if (rxb->page)
  1089. il_tx_cmd_complete(il, rxb);
  1090. else
  1091. IL_WARN("Claim null rxb?\n");
  1092. }
  1093. /* Reuse the page if possible. For notification packets and
  1094. * SKBs that fail to Rx correctly, add them back into the
  1095. * rx_free list for reuse later. */
  1096. spin_lock_irqsave(&rxq->lock, flags);
  1097. if (rxb->page != NULL) {
  1098. rxb->page_dma =
  1099. pci_map_page(il->pci_dev, rxb->page, 0,
  1100. PAGE_SIZE << il->hw_params.
  1101. rx_page_order, PCI_DMA_FROMDEVICE);
  1102. if (unlikely(pci_dma_mapping_error(il->pci_dev,
  1103. rxb->page_dma))) {
  1104. __il_free_pages(il, rxb->page);
  1105. rxb->page = NULL;
  1106. list_add_tail(&rxb->list, &rxq->rx_used);
  1107. } else {
  1108. list_add_tail(&rxb->list, &rxq->rx_free);
  1109. rxq->free_count++;
  1110. }
  1111. } else
  1112. list_add_tail(&rxb->list, &rxq->rx_used);
  1113. spin_unlock_irqrestore(&rxq->lock, flags);
  1114. i = (i + 1) & RX_QUEUE_MASK;
  1115. /* If there are a lot of unused frames,
  1116. * restock the Rx queue so ucode won't assert. */
  1117. if (fill_rx) {
  1118. count++;
  1119. if (count >= 8) {
  1120. rxq->read = i;
  1121. il3945_rx_replenish_now(il);
  1122. count = 0;
  1123. }
  1124. }
  1125. }
  1126. /* Backtrack one entry */
  1127. rxq->read = i;
  1128. if (fill_rx)
  1129. il3945_rx_replenish_now(il);
  1130. else
  1131. il3945_rx_queue_restock(il);
  1132. }
  1133. /* call this function to flush any scheduled tasklet */
  1134. static inline void
  1135. il3945_synchronize_irq(struct il_priv *il)
  1136. {
  1137. /* wait to make sure we flush pending tasklet */
  1138. synchronize_irq(il->pci_dev->irq);
  1139. tasklet_kill(&il->irq_tasklet);
  1140. }
  1141. static const char *
  1142. il3945_desc_lookup(int i)
  1143. {
  1144. switch (i) {
  1145. case 1:
  1146. return "FAIL";
  1147. case 2:
  1148. return "BAD_PARAM";
  1149. case 3:
  1150. return "BAD_CHECKSUM";
  1151. case 4:
  1152. return "NMI_INTERRUPT";
  1153. case 5:
  1154. return "SYSASSERT";
  1155. case 6:
  1156. return "FATAL_ERROR";
  1157. }
  1158. return "UNKNOWN";
  1159. }
  1160. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1161. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1162. void
  1163. il3945_dump_nic_error_log(struct il_priv *il)
  1164. {
  1165. u32 i;
  1166. u32 desc, time, count, base, data1;
  1167. u32 blink1, blink2, ilink1, ilink2;
  1168. base = le32_to_cpu(il->card_alive.error_event_table_ptr);
  1169. if (!il3945_hw_valid_rtc_data_addr(base)) {
  1170. IL_ERR("Not valid error log pointer 0x%08X\n", base);
  1171. return;
  1172. }
  1173. count = il_read_targ_mem(il, base);
  1174. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1175. IL_ERR("Start IWL Error Log Dump:\n");
  1176. IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
  1177. }
  1178. IL_ERR("Desc Time asrtPC blink2 "
  1179. "ilink1 nmiPC Line\n");
  1180. for (i = ERROR_START_OFFSET;
  1181. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1182. i += ERROR_ELEM_SIZE) {
  1183. desc = il_read_targ_mem(il, base + i);
  1184. time = il_read_targ_mem(il, base + i + 1 * sizeof(u32));
  1185. blink1 = il_read_targ_mem(il, base + i + 2 * sizeof(u32));
  1186. blink2 = il_read_targ_mem(il, base + i + 3 * sizeof(u32));
  1187. ilink1 = il_read_targ_mem(il, base + i + 4 * sizeof(u32));
  1188. ilink2 = il_read_targ_mem(il, base + i + 5 * sizeof(u32));
  1189. data1 = il_read_targ_mem(il, base + i + 6 * sizeof(u32));
  1190. IL_ERR("%-13s (0x%X) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1191. il3945_desc_lookup(desc), desc, time, blink1, blink2,
  1192. ilink1, ilink2, data1);
  1193. }
  1194. }
  1195. static void
  1196. il3945_irq_tasklet(struct il_priv *il)
  1197. {
  1198. u32 inta, handled = 0;
  1199. u32 inta_fh;
  1200. unsigned long flags;
  1201. #ifdef CONFIG_IWLEGACY_DEBUG
  1202. u32 inta_mask;
  1203. #endif
  1204. spin_lock_irqsave(&il->lock, flags);
  1205. /* Ack/clear/reset pending uCode interrupts.
  1206. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1207. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1208. inta = _il_rd(il, CSR_INT);
  1209. _il_wr(il, CSR_INT, inta);
  1210. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1211. * Any new interrupts that happen after this, either while we're
  1212. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1213. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  1214. _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
  1215. #ifdef CONFIG_IWLEGACY_DEBUG
  1216. if (il_get_debug_level(il) & IL_DL_ISR) {
  1217. /* just for debug */
  1218. inta_mask = _il_rd(il, CSR_INT_MASK);
  1219. D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
  1220. inta_mask, inta_fh);
  1221. }
  1222. #endif
  1223. spin_unlock_irqrestore(&il->lock, flags);
  1224. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1225. * atomic, make sure that inta covers all the interrupts that
  1226. * we've discovered, even if FH interrupt came in just after
  1227. * reading CSR_INT. */
  1228. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1229. inta |= CSR_INT_BIT_FH_RX;
  1230. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1231. inta |= CSR_INT_BIT_FH_TX;
  1232. /* Now service all interrupt bits discovered above. */
  1233. if (inta & CSR_INT_BIT_HW_ERR) {
  1234. IL_ERR("Hardware error detected. Restarting.\n");
  1235. /* Tell the device to stop sending interrupts */
  1236. il_disable_interrupts(il);
  1237. il->isr_stats.hw++;
  1238. il_irq_handle_error(il);
  1239. handled |= CSR_INT_BIT_HW_ERR;
  1240. return;
  1241. }
  1242. #ifdef CONFIG_IWLEGACY_DEBUG
  1243. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  1244. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1245. if (inta & CSR_INT_BIT_SCD) {
  1246. D_ISR("Scheduler finished to transmit "
  1247. "the frame/frames.\n");
  1248. il->isr_stats.sch++;
  1249. }
  1250. /* Alive notification via Rx interrupt will do the real work */
  1251. if (inta & CSR_INT_BIT_ALIVE) {
  1252. D_ISR("Alive interrupt\n");
  1253. il->isr_stats.alive++;
  1254. }
  1255. }
  1256. #endif
  1257. /* Safely ignore these bits for debug checks below */
  1258. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1259. /* Error detected by uCode */
  1260. if (inta & CSR_INT_BIT_SW_ERR) {
  1261. IL_ERR("Microcode SW error detected. " "Restarting 0x%X.\n",
  1262. inta);
  1263. il->isr_stats.sw++;
  1264. il_irq_handle_error(il);
  1265. handled |= CSR_INT_BIT_SW_ERR;
  1266. }
  1267. /* uCode wakes up after power-down sleep */
  1268. if (inta & CSR_INT_BIT_WAKEUP) {
  1269. D_ISR("Wakeup interrupt\n");
  1270. il_rx_queue_update_write_ptr(il, &il->rxq);
  1271. il_txq_update_write_ptr(il, &il->txq[0]);
  1272. il_txq_update_write_ptr(il, &il->txq[1]);
  1273. il_txq_update_write_ptr(il, &il->txq[2]);
  1274. il_txq_update_write_ptr(il, &il->txq[3]);
  1275. il_txq_update_write_ptr(il, &il->txq[4]);
  1276. il_txq_update_write_ptr(il, &il->txq[5]);
  1277. il->isr_stats.wakeup++;
  1278. handled |= CSR_INT_BIT_WAKEUP;
  1279. }
  1280. /* All uCode command responses, including Tx command responses,
  1281. * Rx "responses" (frame-received notification), and other
  1282. * notifications from uCode come through here*/
  1283. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1284. il3945_rx_handle(il);
  1285. il->isr_stats.rx++;
  1286. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1287. }
  1288. if (inta & CSR_INT_BIT_FH_TX) {
  1289. D_ISR("Tx interrupt\n");
  1290. il->isr_stats.tx++;
  1291. _il_wr(il, CSR_FH_INT_STATUS, (1 << 6));
  1292. il_wr(il, FH39_TCSR_CREDIT(FH39_SRVC_CHNL), 0x0);
  1293. handled |= CSR_INT_BIT_FH_TX;
  1294. }
  1295. if (inta & ~handled) {
  1296. IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1297. il->isr_stats.unhandled++;
  1298. }
  1299. if (inta & ~il->inta_mask) {
  1300. IL_WARN("Disabled INTA bits 0x%08x were pending\n",
  1301. inta & ~il->inta_mask);
  1302. IL_WARN(" with inta_fh = 0x%08x\n", inta_fh);
  1303. }
  1304. /* Re-enable all interrupts */
  1305. /* only Re-enable if disabled by irq */
  1306. if (test_bit(S_INT_ENABLED, &il->status))
  1307. il_enable_interrupts(il);
  1308. #ifdef CONFIG_IWLEGACY_DEBUG
  1309. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  1310. inta = _il_rd(il, CSR_INT);
  1311. inta_mask = _il_rd(il, CSR_INT_MASK);
  1312. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  1313. D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1314. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1315. }
  1316. #endif
  1317. }
  1318. static int
  1319. il3945_get_channels_for_scan(struct il_priv *il, enum ieee80211_band band,
  1320. u8 is_active, u8 n_probes,
  1321. struct il3945_scan_channel *scan_ch,
  1322. struct ieee80211_vif *vif)
  1323. {
  1324. struct ieee80211_channel *chan;
  1325. const struct ieee80211_supported_band *sband;
  1326. const struct il_channel_info *ch_info;
  1327. u16 passive_dwell = 0;
  1328. u16 active_dwell = 0;
  1329. int added, i;
  1330. sband = il_get_hw_mode(il, band);
  1331. if (!sband)
  1332. return 0;
  1333. active_dwell = il_get_active_dwell_time(il, band, n_probes);
  1334. passive_dwell = il_get_passive_dwell_time(il, band, vif);
  1335. if (passive_dwell <= active_dwell)
  1336. passive_dwell = active_dwell + 1;
  1337. for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
  1338. chan = il->scan_request->channels[i];
  1339. if (chan->band != band)
  1340. continue;
  1341. scan_ch->channel = chan->hw_value;
  1342. ch_info = il_get_channel_info(il, band, scan_ch->channel);
  1343. if (!il_is_channel_valid(ch_info)) {
  1344. D_SCAN("Channel %d is INVALID for this band.\n",
  1345. scan_ch->channel);
  1346. continue;
  1347. }
  1348. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1349. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1350. /* If passive , set up for auto-switch
  1351. * and use long active_dwell time.
  1352. */
  1353. if (!is_active || il_is_channel_passive(ch_info) ||
  1354. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1355. scan_ch->type = 0; /* passive */
  1356. if (IL_UCODE_API(il->ucode_ver) == 1)
  1357. scan_ch->active_dwell =
  1358. cpu_to_le16(passive_dwell - 1);
  1359. } else {
  1360. scan_ch->type = 1; /* active */
  1361. }
  1362. /* Set direct probe bits. These may be used both for active
  1363. * scan channels (probes gets sent right away),
  1364. * or for passive channels (probes get se sent only after
  1365. * hearing clear Rx packet).*/
  1366. if (IL_UCODE_API(il->ucode_ver) >= 2) {
  1367. if (n_probes)
  1368. scan_ch->type |= IL39_SCAN_PROBE_MASK(n_probes);
  1369. } else {
  1370. /* uCode v1 does not allow setting direct probe bits on
  1371. * passive channel. */
  1372. if ((scan_ch->type & 1) && n_probes)
  1373. scan_ch->type |= IL39_SCAN_PROBE_MASK(n_probes);
  1374. }
  1375. /* Set txpower levels to defaults */
  1376. scan_ch->tpc.dsp_atten = 110;
  1377. /* scan_pwr_info->tpc.dsp_atten; */
  1378. /*scan_pwr_info->tpc.tx_gain; */
  1379. if (band == IEEE80211_BAND_5GHZ)
  1380. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1381. else {
  1382. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  1383. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1384. * power level:
  1385. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1386. */
  1387. }
  1388. D_SCAN("Scanning %d [%s %d]\n", scan_ch->channel,
  1389. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  1390. (scan_ch->type & 1) ? active_dwell : passive_dwell);
  1391. scan_ch++;
  1392. added++;
  1393. }
  1394. D_SCAN("total channels to scan %d\n", added);
  1395. return added;
  1396. }
  1397. static void
  1398. il3945_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
  1399. {
  1400. int i;
  1401. for (i = 0; i < RATE_COUNT_LEGACY; i++) {
  1402. rates[i].bitrate = il3945_rates[i].ieee * 5;
  1403. rates[i].hw_value = i; /* Rate scaling will work on idxes */
  1404. rates[i].hw_value_short = i;
  1405. rates[i].flags = 0;
  1406. if (i > IL39_LAST_OFDM_RATE || i < IL_FIRST_OFDM_RATE) {
  1407. /*
  1408. * If CCK != 1M then set short preamble rate flag.
  1409. */
  1410. rates[i].flags |=
  1411. (il3945_rates[i].plcp ==
  1412. 10) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1413. }
  1414. }
  1415. }
  1416. /******************************************************************************
  1417. *
  1418. * uCode download functions
  1419. *
  1420. ******************************************************************************/
  1421. static void
  1422. il3945_dealloc_ucode_pci(struct il_priv *il)
  1423. {
  1424. il_free_fw_desc(il->pci_dev, &il->ucode_code);
  1425. il_free_fw_desc(il->pci_dev, &il->ucode_data);
  1426. il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
  1427. il_free_fw_desc(il->pci_dev, &il->ucode_init);
  1428. il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
  1429. il_free_fw_desc(il->pci_dev, &il->ucode_boot);
  1430. }
  1431. /**
  1432. * il3945_verify_inst_full - verify runtime uCode image in card vs. host,
  1433. * looking at all data.
  1434. */
  1435. static int
  1436. il3945_verify_inst_full(struct il_priv *il, __le32 * image, u32 len)
  1437. {
  1438. u32 val;
  1439. u32 save_len = len;
  1440. int rc = 0;
  1441. u32 errcnt;
  1442. D_INFO("ucode inst image size is %u\n", len);
  1443. il_wr(il, HBUS_TARG_MEM_RADDR, IL39_RTC_INST_LOWER_BOUND);
  1444. errcnt = 0;
  1445. for (; len > 0; len -= sizeof(u32), image++) {
  1446. /* read data comes through single port, auto-incr addr */
  1447. /* NOTE: Use the debugless read so we don't flood kernel log
  1448. * if IL_DL_IO is set */
  1449. val = _il_rd(il, HBUS_TARG_MEM_RDAT);
  1450. if (val != le32_to_cpu(*image)) {
  1451. IL_ERR("uCode INST section is invalid at "
  1452. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1453. save_len - len, val, le32_to_cpu(*image));
  1454. rc = -EIO;
  1455. errcnt++;
  1456. if (errcnt >= 20)
  1457. break;
  1458. }
  1459. }
  1460. if (!errcnt)
  1461. D_INFO("ucode image in INSTRUCTION memory is good\n");
  1462. return rc;
  1463. }
  1464. /**
  1465. * il3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  1466. * using sample data 100 bytes apart. If these sample points are good,
  1467. * it's a pretty good bet that everything between them is good, too.
  1468. */
  1469. static int
  1470. il3945_verify_inst_sparse(struct il_priv *il, __le32 * image, u32 len)
  1471. {
  1472. u32 val;
  1473. int rc = 0;
  1474. u32 errcnt = 0;
  1475. u32 i;
  1476. D_INFO("ucode inst image size is %u\n", len);
  1477. for (i = 0; i < len; i += 100, image += 100 / sizeof(u32)) {
  1478. /* read data comes through single port, auto-incr addr */
  1479. /* NOTE: Use the debugless read so we don't flood kernel log
  1480. * if IL_DL_IO is set */
  1481. il_wr(il, HBUS_TARG_MEM_RADDR, i + IL39_RTC_INST_LOWER_BOUND);
  1482. val = _il_rd(il, HBUS_TARG_MEM_RDAT);
  1483. if (val != le32_to_cpu(*image)) {
  1484. #if 0 /* Enable this if you want to see details */
  1485. IL_ERR("uCode INST section is invalid at "
  1486. "offset 0x%x, is 0x%x, s/b 0x%x\n", i, val,
  1487. *image);
  1488. #endif
  1489. rc = -EIO;
  1490. errcnt++;
  1491. if (errcnt >= 3)
  1492. break;
  1493. }
  1494. }
  1495. return rc;
  1496. }
  1497. /**
  1498. * il3945_verify_ucode - determine which instruction image is in SRAM,
  1499. * and verify its contents
  1500. */
  1501. static int
  1502. il3945_verify_ucode(struct il_priv *il)
  1503. {
  1504. __le32 *image;
  1505. u32 len;
  1506. int rc = 0;
  1507. /* Try bootstrap */
  1508. image = (__le32 *) il->ucode_boot.v_addr;
  1509. len = il->ucode_boot.len;
  1510. rc = il3945_verify_inst_sparse(il, image, len);
  1511. if (rc == 0) {
  1512. D_INFO("Bootstrap uCode is good in inst SRAM\n");
  1513. return 0;
  1514. }
  1515. /* Try initialize */
  1516. image = (__le32 *) il->ucode_init.v_addr;
  1517. len = il->ucode_init.len;
  1518. rc = il3945_verify_inst_sparse(il, image, len);
  1519. if (rc == 0) {
  1520. D_INFO("Initialize uCode is good in inst SRAM\n");
  1521. return 0;
  1522. }
  1523. /* Try runtime/protocol */
  1524. image = (__le32 *) il->ucode_code.v_addr;
  1525. len = il->ucode_code.len;
  1526. rc = il3945_verify_inst_sparse(il, image, len);
  1527. if (rc == 0) {
  1528. D_INFO("Runtime uCode is good in inst SRAM\n");
  1529. return 0;
  1530. }
  1531. IL_ERR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  1532. /* Since nothing seems to match, show first several data entries in
  1533. * instruction SRAM, so maybe visual inspection will give a clue.
  1534. * Selection of bootstrap image (vs. other images) is arbitrary. */
  1535. image = (__le32 *) il->ucode_boot.v_addr;
  1536. len = il->ucode_boot.len;
  1537. rc = il3945_verify_inst_full(il, image, len);
  1538. return rc;
  1539. }
  1540. static void
  1541. il3945_nic_start(struct il_priv *il)
  1542. {
  1543. /* Remove all resets to allow NIC to operate */
  1544. _il_wr(il, CSR_RESET, 0);
  1545. }
  1546. #define IL3945_UCODE_GET(item) \
  1547. static u32 il3945_ucode_get_##item(const struct il_ucode_header *ucode)\
  1548. { \
  1549. return le32_to_cpu(ucode->v1.item); \
  1550. }
  1551. static u32
  1552. il3945_ucode_get_header_size(u32 api_ver)
  1553. {
  1554. return 24;
  1555. }
  1556. static u8 *
  1557. il3945_ucode_get_data(const struct il_ucode_header *ucode)
  1558. {
  1559. return (u8 *) ucode->v1.data;
  1560. }
  1561. IL3945_UCODE_GET(inst_size);
  1562. IL3945_UCODE_GET(data_size);
  1563. IL3945_UCODE_GET(init_size);
  1564. IL3945_UCODE_GET(init_data_size);
  1565. IL3945_UCODE_GET(boot_size);
  1566. /**
  1567. * il3945_read_ucode - Read uCode images from disk file.
  1568. *
  1569. * Copy into buffers for card to fetch via bus-mastering
  1570. */
  1571. static int
  1572. il3945_read_ucode(struct il_priv *il)
  1573. {
  1574. const struct il_ucode_header *ucode;
  1575. int ret = -EINVAL, idx;
  1576. const struct firmware *ucode_raw;
  1577. /* firmware file name contains uCode/driver compatibility version */
  1578. const char *name_pre = il->cfg->fw_name_pre;
  1579. const unsigned int api_max = il->cfg->ucode_api_max;
  1580. const unsigned int api_min = il->cfg->ucode_api_min;
  1581. char buf[25];
  1582. u8 *src;
  1583. size_t len;
  1584. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  1585. /* Ask kernel firmware_class module to get the boot firmware off disk.
  1586. * request_firmware() is synchronous, file is in memory on return. */
  1587. for (idx = api_max; idx >= api_min; idx--) {
  1588. sprintf(buf, "%s%u%s", name_pre, idx, ".ucode");
  1589. ret = request_firmware(&ucode_raw, buf, &il->pci_dev->dev);
  1590. if (ret < 0) {
  1591. IL_ERR("%s firmware file req failed: %d\n", buf, ret);
  1592. if (ret == -ENOENT)
  1593. continue;
  1594. else
  1595. goto error;
  1596. } else {
  1597. if (idx < api_max)
  1598. IL_ERR("Loaded firmware %s, "
  1599. "which is deprecated. "
  1600. " Please use API v%u instead.\n", buf,
  1601. api_max);
  1602. D_INFO("Got firmware '%s' file "
  1603. "(%zd bytes) from disk\n", buf, ucode_raw->size);
  1604. break;
  1605. }
  1606. }
  1607. if (ret < 0)
  1608. goto error;
  1609. /* Make sure that we got at least our header! */
  1610. if (ucode_raw->size < il3945_ucode_get_header_size(1)) {
  1611. IL_ERR("File size way too small!\n");
  1612. ret = -EINVAL;
  1613. goto err_release;
  1614. }
  1615. /* Data from ucode file: header followed by uCode images */
  1616. ucode = (struct il_ucode_header *)ucode_raw->data;
  1617. il->ucode_ver = le32_to_cpu(ucode->ver);
  1618. api_ver = IL_UCODE_API(il->ucode_ver);
  1619. inst_size = il3945_ucode_get_inst_size(ucode);
  1620. data_size = il3945_ucode_get_data_size(ucode);
  1621. init_size = il3945_ucode_get_init_size(ucode);
  1622. init_data_size = il3945_ucode_get_init_data_size(ucode);
  1623. boot_size = il3945_ucode_get_boot_size(ucode);
  1624. src = il3945_ucode_get_data(ucode);
  1625. /* api_ver should match the api version forming part of the
  1626. * firmware filename ... but we don't check for that and only rely
  1627. * on the API version read from firmware header from here on forward */
  1628. if (api_ver < api_min || api_ver > api_max) {
  1629. IL_ERR("Driver unable to support your firmware API. "
  1630. "Driver supports v%u, firmware is v%u.\n", api_max,
  1631. api_ver);
  1632. il->ucode_ver = 0;
  1633. ret = -EINVAL;
  1634. goto err_release;
  1635. }
  1636. if (api_ver != api_max)
  1637. IL_ERR("Firmware has old API version. Expected %u, "
  1638. "got %u. New firmware can be obtained "
  1639. "from http://www.intellinuxwireless.org.\n", api_max,
  1640. api_ver);
  1641. IL_INFO("loaded firmware version %u.%u.%u.%u\n",
  1642. IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
  1643. IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
  1644. snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
  1645. "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
  1646. IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
  1647. IL_UCODE_SERIAL(il->ucode_ver));
  1648. D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
  1649. D_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
  1650. D_INFO("f/w package hdr runtime data size = %u\n", data_size);
  1651. D_INFO("f/w package hdr init inst size = %u\n", init_size);
  1652. D_INFO("f/w package hdr init data size = %u\n", init_data_size);
  1653. D_INFO("f/w package hdr boot inst size = %u\n", boot_size);
  1654. /* Verify size of file vs. image size info in file's header */
  1655. if (ucode_raw->size !=
  1656. il3945_ucode_get_header_size(api_ver) + inst_size + data_size +
  1657. init_size + init_data_size + boot_size) {
  1658. D_INFO("uCode file size %zd does not match expected size\n",
  1659. ucode_raw->size);
  1660. ret = -EINVAL;
  1661. goto err_release;
  1662. }
  1663. /* Verify that uCode images will fit in card's SRAM */
  1664. if (inst_size > IL39_MAX_INST_SIZE) {
  1665. D_INFO("uCode instr len %d too large to fit in\n", inst_size);
  1666. ret = -EINVAL;
  1667. goto err_release;
  1668. }
  1669. if (data_size > IL39_MAX_DATA_SIZE) {
  1670. D_INFO("uCode data len %d too large to fit in\n", data_size);
  1671. ret = -EINVAL;
  1672. goto err_release;
  1673. }
  1674. if (init_size > IL39_MAX_INST_SIZE) {
  1675. D_INFO("uCode init instr len %d too large to fit in\n",
  1676. init_size);
  1677. ret = -EINVAL;
  1678. goto err_release;
  1679. }
  1680. if (init_data_size > IL39_MAX_DATA_SIZE) {
  1681. D_INFO("uCode init data len %d too large to fit in\n",
  1682. init_data_size);
  1683. ret = -EINVAL;
  1684. goto err_release;
  1685. }
  1686. if (boot_size > IL39_MAX_BSM_SIZE) {
  1687. D_INFO("uCode boot instr len %d too large to fit in\n",
  1688. boot_size);
  1689. ret = -EINVAL;
  1690. goto err_release;
  1691. }
  1692. /* Allocate ucode buffers for card's bus-master loading ... */
  1693. /* Runtime instructions and 2 copies of data:
  1694. * 1) unmodified from disk
  1695. * 2) backup cache for save/restore during power-downs */
  1696. il->ucode_code.len = inst_size;
  1697. il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
  1698. il->ucode_data.len = data_size;
  1699. il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
  1700. il->ucode_data_backup.len = data_size;
  1701. il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
  1702. if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
  1703. !il->ucode_data_backup.v_addr)
  1704. goto err_pci_alloc;
  1705. /* Initialization instructions and data */
  1706. if (init_size && init_data_size) {
  1707. il->ucode_init.len = init_size;
  1708. il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
  1709. il->ucode_init_data.len = init_data_size;
  1710. il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
  1711. if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
  1712. goto err_pci_alloc;
  1713. }
  1714. /* Bootstrap (instructions only, no data) */
  1715. if (boot_size) {
  1716. il->ucode_boot.len = boot_size;
  1717. il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
  1718. if (!il->ucode_boot.v_addr)
  1719. goto err_pci_alloc;
  1720. }
  1721. /* Copy images into buffers for card's bus-master reads ... */
  1722. /* Runtime instructions (first block of data in file) */
  1723. len = inst_size;
  1724. D_INFO("Copying (but not loading) uCode instr len %zd\n", len);
  1725. memcpy(il->ucode_code.v_addr, src, len);
  1726. src += len;
  1727. D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  1728. il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
  1729. /* Runtime data (2nd block)
  1730. * NOTE: Copy into backup buffer will be done in il3945_up() */
  1731. len = data_size;
  1732. D_INFO("Copying (but not loading) uCode data len %zd\n", len);
  1733. memcpy(il->ucode_data.v_addr, src, len);
  1734. memcpy(il->ucode_data_backup.v_addr, src, len);
  1735. src += len;
  1736. /* Initialization instructions (3rd block) */
  1737. if (init_size) {
  1738. len = init_size;
  1739. D_INFO("Copying (but not loading) init instr len %zd\n", len);
  1740. memcpy(il->ucode_init.v_addr, src, len);
  1741. src += len;
  1742. }
  1743. /* Initialization data (4th block) */
  1744. if (init_data_size) {
  1745. len = init_data_size;
  1746. D_INFO("Copying (but not loading) init data len %zd\n", len);
  1747. memcpy(il->ucode_init_data.v_addr, src, len);
  1748. src += len;
  1749. }
  1750. /* Bootstrap instructions (5th block) */
  1751. len = boot_size;
  1752. D_INFO("Copying (but not loading) boot instr len %zd\n", len);
  1753. memcpy(il->ucode_boot.v_addr, src, len);
  1754. /* We have our copies now, allow OS release its copies */
  1755. release_firmware(ucode_raw);
  1756. return 0;
  1757. err_pci_alloc:
  1758. IL_ERR("failed to allocate pci memory\n");
  1759. ret = -ENOMEM;
  1760. il3945_dealloc_ucode_pci(il);
  1761. err_release:
  1762. release_firmware(ucode_raw);
  1763. error:
  1764. return ret;
  1765. }
  1766. /**
  1767. * il3945_set_ucode_ptrs - Set uCode address location
  1768. *
  1769. * Tell initialization uCode where to find runtime uCode.
  1770. *
  1771. * BSM registers initially contain pointers to initialization uCode.
  1772. * We need to replace them to load runtime uCode inst and data,
  1773. * and to save runtime data when powering down.
  1774. */
  1775. static int
  1776. il3945_set_ucode_ptrs(struct il_priv *il)
  1777. {
  1778. dma_addr_t pinst;
  1779. dma_addr_t pdata;
  1780. /* bits 31:0 for 3945 */
  1781. pinst = il->ucode_code.p_addr;
  1782. pdata = il->ucode_data_backup.p_addr;
  1783. /* Tell bootstrap uCode where to find image to load */
  1784. il_wr_prph(il, BSM_DRAM_INST_PTR_REG, pinst);
  1785. il_wr_prph(il, BSM_DRAM_DATA_PTR_REG, pdata);
  1786. il_wr_prph(il, BSM_DRAM_DATA_BYTECOUNT_REG, il->ucode_data.len);
  1787. /* Inst byte count must be last to set up, bit 31 signals uCode
  1788. * that all new ptr/size info is in place */
  1789. il_wr_prph(il, BSM_DRAM_INST_BYTECOUNT_REG,
  1790. il->ucode_code.len | BSM_DRAM_INST_LOAD);
  1791. D_INFO("Runtime uCode pointers are set.\n");
  1792. return 0;
  1793. }
  1794. /**
  1795. * il3945_init_alive_start - Called after N_ALIVE notification received
  1796. *
  1797. * Called after N_ALIVE notification received from "initialize" uCode.
  1798. *
  1799. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  1800. */
  1801. static void
  1802. il3945_init_alive_start(struct il_priv *il)
  1803. {
  1804. /* Check alive response for "valid" sign from uCode */
  1805. if (il->card_alive_init.is_valid != UCODE_VALID_OK) {
  1806. /* We had an error bringing up the hardware, so take it
  1807. * all the way back down so we can try again */
  1808. D_INFO("Initialize Alive failed.\n");
  1809. goto restart;
  1810. }
  1811. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  1812. * This is a paranoid check, because we would not have gotten the
  1813. * "initialize" alive if code weren't properly loaded. */
  1814. if (il3945_verify_ucode(il)) {
  1815. /* Runtime instruction load was bad;
  1816. * take it all the way back down so we can try again */
  1817. D_INFO("Bad \"initialize\" uCode load.\n");
  1818. goto restart;
  1819. }
  1820. /* Send pointers to protocol/runtime uCode image ... init code will
  1821. * load and launch runtime uCode, which will send us another "Alive"
  1822. * notification. */
  1823. D_INFO("Initialization Alive received.\n");
  1824. if (il3945_set_ucode_ptrs(il)) {
  1825. /* Runtime instruction load won't happen;
  1826. * take it all the way back down so we can try again */
  1827. D_INFO("Couldn't set up uCode pointers.\n");
  1828. goto restart;
  1829. }
  1830. return;
  1831. restart:
  1832. queue_work(il->workqueue, &il->restart);
  1833. }
  1834. /**
  1835. * il3945_alive_start - called after N_ALIVE notification received
  1836. * from protocol/runtime uCode (initialization uCode's
  1837. * Alive gets handled by il3945_init_alive_start()).
  1838. */
  1839. static void
  1840. il3945_alive_start(struct il_priv *il)
  1841. {
  1842. int thermal_spin = 0;
  1843. u32 rfkill;
  1844. D_INFO("Runtime Alive received.\n");
  1845. if (il->card_alive.is_valid != UCODE_VALID_OK) {
  1846. /* We had an error bringing up the hardware, so take it
  1847. * all the way back down so we can try again */
  1848. D_INFO("Alive failed.\n");
  1849. goto restart;
  1850. }
  1851. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  1852. * This is a paranoid check, because we would not have gotten the
  1853. * "runtime" alive if code weren't properly loaded. */
  1854. if (il3945_verify_ucode(il)) {
  1855. /* Runtime instruction load was bad;
  1856. * take it all the way back down so we can try again */
  1857. D_INFO("Bad runtime uCode load.\n");
  1858. goto restart;
  1859. }
  1860. rfkill = il_rd_prph(il, APMG_RFKILL_REG);
  1861. D_INFO("RFKILL status: 0x%x\n", rfkill);
  1862. if (rfkill & 0x1) {
  1863. clear_bit(S_RFKILL, &il->status);
  1864. /* if RFKILL is not on, then wait for thermal
  1865. * sensor in adapter to kick in */
  1866. while (il3945_hw_get_temperature(il) == 0) {
  1867. thermal_spin++;
  1868. udelay(10);
  1869. }
  1870. if (thermal_spin)
  1871. D_INFO("Thermal calibration took %dus\n",
  1872. thermal_spin * 10);
  1873. } else
  1874. set_bit(S_RFKILL, &il->status);
  1875. /* After the ALIVE response, we can send commands to 3945 uCode */
  1876. set_bit(S_ALIVE, &il->status);
  1877. /* Enable watchdog to monitor the driver tx queues */
  1878. il_setup_watchdog(il);
  1879. if (il_is_rfkill(il))
  1880. return;
  1881. ieee80211_wake_queues(il->hw);
  1882. il->active_rate = RATES_MASK_3945;
  1883. il_power_update_mode(il, true);
  1884. if (il_is_associated(il)) {
  1885. struct il3945_rxon_cmd *active_rxon =
  1886. (struct il3945_rxon_cmd *)(&il->active);
  1887. il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1888. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1889. } else {
  1890. /* Initialize our rx_config data */
  1891. il_connection_init_rx_config(il);
  1892. }
  1893. /* Configure Bluetooth device coexistence support */
  1894. il_send_bt_config(il);
  1895. set_bit(S_READY, &il->status);
  1896. /* Configure the adapter for unassociated operation */
  1897. il3945_commit_rxon(il);
  1898. il3945_reg_txpower_periodic(il);
  1899. D_INFO("ALIVE processing complete.\n");
  1900. wake_up(&il->wait_command_queue);
  1901. return;
  1902. restart:
  1903. queue_work(il->workqueue, &il->restart);
  1904. }
  1905. static void il3945_cancel_deferred_work(struct il_priv *il);
  1906. static void
  1907. __il3945_down(struct il_priv *il)
  1908. {
  1909. unsigned long flags;
  1910. int exit_pending;
  1911. D_INFO(DRV_NAME " is going down\n");
  1912. il_scan_cancel_timeout(il, 200);
  1913. exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
  1914. /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
  1915. * to prevent rearm timer */
  1916. del_timer_sync(&il->watchdog);
  1917. /* Station information will now be cleared in device */
  1918. il_clear_ucode_stations(il);
  1919. il_dealloc_bcast_stations(il);
  1920. il_clear_driver_stations(il);
  1921. /* Unblock any waiting calls */
  1922. wake_up_all(&il->wait_command_queue);
  1923. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1924. * exiting the module */
  1925. if (!exit_pending)
  1926. clear_bit(S_EXIT_PENDING, &il->status);
  1927. /* stop and reset the on-board processor */
  1928. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  1929. /* tell the device to stop sending interrupts */
  1930. spin_lock_irqsave(&il->lock, flags);
  1931. il_disable_interrupts(il);
  1932. spin_unlock_irqrestore(&il->lock, flags);
  1933. il3945_synchronize_irq(il);
  1934. if (il->mac80211_registered)
  1935. ieee80211_stop_queues(il->hw);
  1936. /* If we have not previously called il3945_init() then
  1937. * clear all bits but the RF Kill bits and return */
  1938. if (!il_is_init(il)) {
  1939. il->status =
  1940. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  1941. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  1942. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  1943. goto exit;
  1944. }
  1945. /* ...otherwise clear out all the status bits but the RF Kill
  1946. * bit and continue taking the NIC down. */
  1947. il->status &=
  1948. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  1949. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  1950. test_bit(S_FW_ERROR, &il->status) << S_FW_ERROR |
  1951. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  1952. /*
  1953. * We disabled and synchronized interrupt, and priv->mutex is taken, so
  1954. * here is the only thread which will program device registers, but
  1955. * still have lockdep assertions, so we are taking reg_lock.
  1956. */
  1957. spin_lock_irq(&il->reg_lock);
  1958. /* FIXME: il_grab_nic_access if rfkill is off ? */
  1959. il3945_hw_txq_ctx_stop(il);
  1960. il3945_hw_rxq_stop(il);
  1961. /* Power-down device's busmaster DMA clocks */
  1962. _il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  1963. udelay(5);
  1964. /* Stop the device, and put it in low power state */
  1965. _il_apm_stop(il);
  1966. spin_unlock_irq(&il->reg_lock);
  1967. il3945_hw_txq_ctx_free(il);
  1968. exit:
  1969. memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
  1970. if (il->beacon_skb)
  1971. dev_kfree_skb(il->beacon_skb);
  1972. il->beacon_skb = NULL;
  1973. /* clear out any free frames */
  1974. il3945_clear_free_frames(il);
  1975. }
  1976. static void
  1977. il3945_down(struct il_priv *il)
  1978. {
  1979. mutex_lock(&il->mutex);
  1980. __il3945_down(il);
  1981. mutex_unlock(&il->mutex);
  1982. il3945_cancel_deferred_work(il);
  1983. }
  1984. #define MAX_HW_RESTARTS 5
  1985. static int
  1986. il3945_alloc_bcast_station(struct il_priv *il)
  1987. {
  1988. unsigned long flags;
  1989. u8 sta_id;
  1990. spin_lock_irqsave(&il->sta_lock, flags);
  1991. sta_id = il_prep_station(il, il_bcast_addr, false, NULL);
  1992. if (sta_id == IL_INVALID_STATION) {
  1993. IL_ERR("Unable to prepare broadcast station\n");
  1994. spin_unlock_irqrestore(&il->sta_lock, flags);
  1995. return -EINVAL;
  1996. }
  1997. il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
  1998. il->stations[sta_id].used |= IL_STA_BCAST;
  1999. spin_unlock_irqrestore(&il->sta_lock, flags);
  2000. return 0;
  2001. }
  2002. static int
  2003. __il3945_up(struct il_priv *il)
  2004. {
  2005. int rc, i;
  2006. rc = il3945_alloc_bcast_station(il);
  2007. if (rc)
  2008. return rc;
  2009. if (test_bit(S_EXIT_PENDING, &il->status)) {
  2010. IL_WARN("Exit pending; will not bring the NIC up\n");
  2011. return -EIO;
  2012. }
  2013. if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
  2014. IL_ERR("ucode not available for device bring up\n");
  2015. return -EIO;
  2016. }
  2017. /* If platform's RF_KILL switch is NOT set to KILL */
  2018. if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2019. clear_bit(S_RFKILL, &il->status);
  2020. else {
  2021. set_bit(S_RFKILL, &il->status);
  2022. IL_WARN("Radio disabled by HW RF Kill switch\n");
  2023. return -ENODEV;
  2024. }
  2025. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  2026. rc = il3945_hw_nic_init(il);
  2027. if (rc) {
  2028. IL_ERR("Unable to int nic\n");
  2029. return rc;
  2030. }
  2031. /* make sure rfkill handshake bits are cleared */
  2032. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2033. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2034. /* clear (again), then enable host interrupts */
  2035. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  2036. il_enable_interrupts(il);
  2037. /* really make sure rfkill handshake bits are cleared */
  2038. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2039. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2040. /* Copy original ucode data image from disk into backup cache.
  2041. * This will be used to initialize the on-board processor's
  2042. * data SRAM for a clean start when the runtime program first loads. */
  2043. memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
  2044. il->ucode_data.len);
  2045. /* We return success when we resume from suspend and rf_kill is on. */
  2046. if (test_bit(S_RFKILL, &il->status))
  2047. return 0;
  2048. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  2049. /* load bootstrap state machine,
  2050. * load bootstrap program into processor's memory,
  2051. * prepare to load the "initialize" uCode */
  2052. rc = il->ops->load_ucode(il);
  2053. if (rc) {
  2054. IL_ERR("Unable to set up bootstrap uCode: %d\n", rc);
  2055. continue;
  2056. }
  2057. /* start card; "initialize" will load runtime ucode */
  2058. il3945_nic_start(il);
  2059. D_INFO(DRV_NAME " is coming up\n");
  2060. return 0;
  2061. }
  2062. set_bit(S_EXIT_PENDING, &il->status);
  2063. __il3945_down(il);
  2064. clear_bit(S_EXIT_PENDING, &il->status);
  2065. /* tried to restart and config the device for as long as our
  2066. * patience could withstand */
  2067. IL_ERR("Unable to initialize device after %d attempts.\n", i);
  2068. return -EIO;
  2069. }
  2070. /*****************************************************************************
  2071. *
  2072. * Workqueue callbacks
  2073. *
  2074. *****************************************************************************/
  2075. static void
  2076. il3945_bg_init_alive_start(struct work_struct *data)
  2077. {
  2078. struct il_priv *il =
  2079. container_of(data, struct il_priv, init_alive_start.work);
  2080. mutex_lock(&il->mutex);
  2081. if (test_bit(S_EXIT_PENDING, &il->status))
  2082. goto out;
  2083. il3945_init_alive_start(il);
  2084. out:
  2085. mutex_unlock(&il->mutex);
  2086. }
  2087. static void
  2088. il3945_bg_alive_start(struct work_struct *data)
  2089. {
  2090. struct il_priv *il =
  2091. container_of(data, struct il_priv, alive_start.work);
  2092. mutex_lock(&il->mutex);
  2093. if (test_bit(S_EXIT_PENDING, &il->status) || il->txq == NULL)
  2094. goto out;
  2095. il3945_alive_start(il);
  2096. out:
  2097. mutex_unlock(&il->mutex);
  2098. }
  2099. /*
  2100. * 3945 cannot interrupt driver when hardware rf kill switch toggles;
  2101. * driver must poll CSR_GP_CNTRL_REG register for change. This register
  2102. * *is* readable even when device has been SW_RESET into low power mode
  2103. * (e.g. during RF KILL).
  2104. */
  2105. static void
  2106. il3945_rfkill_poll(struct work_struct *data)
  2107. {
  2108. struct il_priv *il =
  2109. container_of(data, struct il_priv, _3945.rfkill_poll.work);
  2110. bool old_rfkill = test_bit(S_RFKILL, &il->status);
  2111. bool new_rfkill =
  2112. !(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW);
  2113. if (new_rfkill != old_rfkill) {
  2114. if (new_rfkill)
  2115. set_bit(S_RFKILL, &il->status);
  2116. else
  2117. clear_bit(S_RFKILL, &il->status);
  2118. wiphy_rfkill_set_hw_state(il->hw->wiphy, new_rfkill);
  2119. D_RF_KILL("RF_KILL bit toggled to %s.\n",
  2120. new_rfkill ? "disable radio" : "enable radio");
  2121. }
  2122. /* Keep this running, even if radio now enabled. This will be
  2123. * cancelled in mac_start() if system decides to start again */
  2124. queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll,
  2125. round_jiffies_relative(2 * HZ));
  2126. }
  2127. int
  2128. il3945_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
  2129. {
  2130. struct il_host_cmd cmd = {
  2131. .id = C_SCAN,
  2132. .len = sizeof(struct il3945_scan_cmd),
  2133. .flags = CMD_SIZE_HUGE,
  2134. };
  2135. struct il3945_scan_cmd *scan;
  2136. u8 n_probes = 0;
  2137. enum ieee80211_band band;
  2138. bool is_active = false;
  2139. int ret;
  2140. u16 len;
  2141. lockdep_assert_held(&il->mutex);
  2142. if (!il->scan_cmd) {
  2143. il->scan_cmd =
  2144. kmalloc(sizeof(struct il3945_scan_cmd) + IL_MAX_SCAN_SIZE,
  2145. GFP_KERNEL);
  2146. if (!il->scan_cmd) {
  2147. D_SCAN("Fail to allocate scan memory\n");
  2148. return -ENOMEM;
  2149. }
  2150. }
  2151. scan = il->scan_cmd;
  2152. memset(scan, 0, sizeof(struct il3945_scan_cmd) + IL_MAX_SCAN_SIZE);
  2153. scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
  2154. scan->quiet_time = IL_ACTIVE_QUIET_TIME;
  2155. if (il_is_associated(il)) {
  2156. u16 interval;
  2157. u32 extra;
  2158. u32 suspend_time = 100;
  2159. u32 scan_suspend_time = 100;
  2160. D_INFO("Scanning while associated...\n");
  2161. interval = vif->bss_conf.beacon_int;
  2162. scan->suspend_time = 0;
  2163. scan->max_out_time = cpu_to_le32(200 * 1024);
  2164. if (!interval)
  2165. interval = suspend_time;
  2166. /*
  2167. * suspend time format:
  2168. * 0-19: beacon interval in usec (time before exec.)
  2169. * 20-23: 0
  2170. * 24-31: number of beacons (suspend between channels)
  2171. */
  2172. extra = (suspend_time / interval) << 24;
  2173. scan_suspend_time =
  2174. 0xFF0FFFFF & (extra | ((suspend_time % interval) * 1024));
  2175. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  2176. D_SCAN("suspend_time 0x%X beacon interval %d\n",
  2177. scan_suspend_time, interval);
  2178. }
  2179. if (il->scan_request->n_ssids) {
  2180. int i, p = 0;
  2181. D_SCAN("Kicking off active scan\n");
  2182. for (i = 0; i < il->scan_request->n_ssids; i++) {
  2183. /* always does wildcard anyway */
  2184. if (!il->scan_request->ssids[i].ssid_len)
  2185. continue;
  2186. scan->direct_scan[p].id = WLAN_EID_SSID;
  2187. scan->direct_scan[p].len =
  2188. il->scan_request->ssids[i].ssid_len;
  2189. memcpy(scan->direct_scan[p].ssid,
  2190. il->scan_request->ssids[i].ssid,
  2191. il->scan_request->ssids[i].ssid_len);
  2192. n_probes++;
  2193. p++;
  2194. }
  2195. is_active = true;
  2196. } else
  2197. D_SCAN("Kicking off passive scan.\n");
  2198. /* We don't build a direct scan probe request; the uCode will do
  2199. * that based on the direct_mask added to each channel entry */
  2200. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  2201. scan->tx_cmd.sta_id = il->hw_params.bcast_id;
  2202. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2203. /* flags + rate selection */
  2204. switch (il->scan_band) {
  2205. case IEEE80211_BAND_2GHZ:
  2206. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  2207. scan->tx_cmd.rate = RATE_1M_PLCP;
  2208. band = IEEE80211_BAND_2GHZ;
  2209. break;
  2210. case IEEE80211_BAND_5GHZ:
  2211. scan->tx_cmd.rate = RATE_6M_PLCP;
  2212. band = IEEE80211_BAND_5GHZ;
  2213. break;
  2214. default:
  2215. IL_WARN("Invalid scan band\n");
  2216. return -EIO;
  2217. }
  2218. /*
  2219. * If active scaning is requested but a certain channel is marked
  2220. * passive, we can do active scanning if we detect transmissions. For
  2221. * passive only scanning disable switching to active on any channel.
  2222. */
  2223. scan->good_CRC_th =
  2224. is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
  2225. len =
  2226. il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
  2227. vif->addr, il->scan_request->ie,
  2228. il->scan_request->ie_len,
  2229. IL_MAX_SCAN_SIZE - sizeof(*scan));
  2230. scan->tx_cmd.len = cpu_to_le16(len);
  2231. /* select Rx antennas */
  2232. scan->flags |= il3945_get_antenna_flags(il);
  2233. scan->channel_count =
  2234. il3945_get_channels_for_scan(il, band, is_active, n_probes,
  2235. (void *)&scan->data[len], vif);
  2236. if (scan->channel_count == 0) {
  2237. D_SCAN("channel count %d\n", scan->channel_count);
  2238. return -EIO;
  2239. }
  2240. cmd.len +=
  2241. le16_to_cpu(scan->tx_cmd.len) +
  2242. scan->channel_count * sizeof(struct il3945_scan_channel);
  2243. cmd.data = scan;
  2244. scan->len = cpu_to_le16(cmd.len);
  2245. set_bit(S_SCAN_HW, &il->status);
  2246. ret = il_send_cmd_sync(il, &cmd);
  2247. if (ret)
  2248. clear_bit(S_SCAN_HW, &il->status);
  2249. return ret;
  2250. }
  2251. void
  2252. il3945_post_scan(struct il_priv *il)
  2253. {
  2254. /*
  2255. * Since setting the RXON may have been deferred while
  2256. * performing the scan, fire one off if needed
  2257. */
  2258. if (memcmp(&il->staging, &il->active, sizeof(il->staging)))
  2259. il3945_commit_rxon(il);
  2260. }
  2261. static void
  2262. il3945_bg_restart(struct work_struct *data)
  2263. {
  2264. struct il_priv *il = container_of(data, struct il_priv, restart);
  2265. if (test_bit(S_EXIT_PENDING, &il->status))
  2266. return;
  2267. if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
  2268. mutex_lock(&il->mutex);
  2269. il->is_open = 0;
  2270. mutex_unlock(&il->mutex);
  2271. il3945_down(il);
  2272. ieee80211_restart_hw(il->hw);
  2273. } else {
  2274. il3945_down(il);
  2275. mutex_lock(&il->mutex);
  2276. if (test_bit(S_EXIT_PENDING, &il->status)) {
  2277. mutex_unlock(&il->mutex);
  2278. return;
  2279. }
  2280. __il3945_up(il);
  2281. mutex_unlock(&il->mutex);
  2282. }
  2283. }
  2284. static void
  2285. il3945_bg_rx_replenish(struct work_struct *data)
  2286. {
  2287. struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
  2288. mutex_lock(&il->mutex);
  2289. if (test_bit(S_EXIT_PENDING, &il->status))
  2290. goto out;
  2291. il3945_rx_replenish(il);
  2292. out:
  2293. mutex_unlock(&il->mutex);
  2294. }
  2295. void
  2296. il3945_post_associate(struct il_priv *il)
  2297. {
  2298. int rc = 0;
  2299. struct ieee80211_conf *conf = NULL;
  2300. if (!il->vif || !il->is_open)
  2301. return;
  2302. D_ASSOC("Associated as %d to: %pM\n", il->vif->bss_conf.aid,
  2303. il->active.bssid_addr);
  2304. if (test_bit(S_EXIT_PENDING, &il->status))
  2305. return;
  2306. il_scan_cancel_timeout(il, 200);
  2307. conf = &il->hw->conf;
  2308. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2309. il3945_commit_rxon(il);
  2310. rc = il_send_rxon_timing(il);
  2311. if (rc)
  2312. IL_WARN("C_RXON_TIMING failed - " "Attempting to continue.\n");
  2313. il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2314. il->staging.assoc_id = cpu_to_le16(il->vif->bss_conf.aid);
  2315. D_ASSOC("assoc id %d beacon interval %d\n", il->vif->bss_conf.aid,
  2316. il->vif->bss_conf.beacon_int);
  2317. if (il->vif->bss_conf.use_short_preamble)
  2318. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2319. else
  2320. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2321. if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
  2322. if (il->vif->bss_conf.use_short_slot)
  2323. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2324. else
  2325. il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2326. }
  2327. il3945_commit_rxon(il);
  2328. switch (il->vif->type) {
  2329. case NL80211_IFTYPE_STATION:
  2330. il3945_rate_scale_init(il->hw, IL_AP_ID);
  2331. break;
  2332. case NL80211_IFTYPE_ADHOC:
  2333. il3945_send_beacon_cmd(il);
  2334. break;
  2335. default:
  2336. IL_ERR("%s Should not be called in %d mode\n", __func__,
  2337. il->vif->type);
  2338. break;
  2339. }
  2340. }
  2341. /*****************************************************************************
  2342. *
  2343. * mac80211 entry point functions
  2344. *
  2345. *****************************************************************************/
  2346. #define UCODE_READY_TIMEOUT (2 * HZ)
  2347. static int
  2348. il3945_mac_start(struct ieee80211_hw *hw)
  2349. {
  2350. struct il_priv *il = hw->priv;
  2351. int ret;
  2352. /* we should be verifying the device is ready to be opened */
  2353. mutex_lock(&il->mutex);
  2354. D_MAC80211("enter\n");
  2355. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  2356. * ucode filename and max sizes are card-specific. */
  2357. if (!il->ucode_code.len) {
  2358. ret = il3945_read_ucode(il);
  2359. if (ret) {
  2360. IL_ERR("Could not read microcode: %d\n", ret);
  2361. mutex_unlock(&il->mutex);
  2362. goto out_release_irq;
  2363. }
  2364. }
  2365. ret = __il3945_up(il);
  2366. mutex_unlock(&il->mutex);
  2367. if (ret)
  2368. goto out_release_irq;
  2369. D_INFO("Start UP work.\n");
  2370. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  2371. * mac80211 will not be run successfully. */
  2372. ret = wait_event_timeout(il->wait_command_queue,
  2373. test_bit(S_READY, &il->status),
  2374. UCODE_READY_TIMEOUT);
  2375. if (!ret) {
  2376. if (!test_bit(S_READY, &il->status)) {
  2377. IL_ERR("Wait for START_ALIVE timeout after %dms.\n",
  2378. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  2379. ret = -ETIMEDOUT;
  2380. goto out_release_irq;
  2381. }
  2382. }
  2383. /* ucode is running and will send rfkill notifications,
  2384. * no need to poll the killswitch state anymore */
  2385. cancel_delayed_work(&il->_3945.rfkill_poll);
  2386. il->is_open = 1;
  2387. D_MAC80211("leave\n");
  2388. return 0;
  2389. out_release_irq:
  2390. il->is_open = 0;
  2391. D_MAC80211("leave - failed\n");
  2392. return ret;
  2393. }
  2394. static void
  2395. il3945_mac_stop(struct ieee80211_hw *hw)
  2396. {
  2397. struct il_priv *il = hw->priv;
  2398. D_MAC80211("enter\n");
  2399. if (!il->is_open) {
  2400. D_MAC80211("leave - skip\n");
  2401. return;
  2402. }
  2403. il->is_open = 0;
  2404. il3945_down(il);
  2405. flush_workqueue(il->workqueue);
  2406. /* start polling the killswitch state again */
  2407. queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll,
  2408. round_jiffies_relative(2 * HZ));
  2409. D_MAC80211("leave\n");
  2410. }
  2411. static void
  2412. il3945_mac_tx(struct ieee80211_hw *hw,
  2413. struct ieee80211_tx_control *control,
  2414. struct sk_buff *skb)
  2415. {
  2416. struct il_priv *il = hw->priv;
  2417. D_MAC80211("enter\n");
  2418. D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2419. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2420. if (il3945_tx_skb(il, control->sta, skb))
  2421. dev_kfree_skb_any(skb);
  2422. D_MAC80211("leave\n");
  2423. }
  2424. void
  2425. il3945_config_ap(struct il_priv *il)
  2426. {
  2427. struct ieee80211_vif *vif = il->vif;
  2428. int rc = 0;
  2429. if (test_bit(S_EXIT_PENDING, &il->status))
  2430. return;
  2431. /* The following should be done only at AP bring up */
  2432. if (!(il_is_associated(il))) {
  2433. /* RXON - unassoc (to set timing command) */
  2434. il->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2435. il3945_commit_rxon(il);
  2436. /* RXON Timing */
  2437. rc = il_send_rxon_timing(il);
  2438. if (rc)
  2439. IL_WARN("C_RXON_TIMING failed - "
  2440. "Attempting to continue.\n");
  2441. il->staging.assoc_id = 0;
  2442. if (vif->bss_conf.use_short_preamble)
  2443. il->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2444. else
  2445. il->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2446. if (il->staging.flags & RXON_FLG_BAND_24G_MSK) {
  2447. if (vif->bss_conf.use_short_slot)
  2448. il->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2449. else
  2450. il->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2451. }
  2452. /* restore RXON assoc */
  2453. il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2454. il3945_commit_rxon(il);
  2455. }
  2456. il3945_send_beacon_cmd(il);
  2457. }
  2458. static int
  2459. il3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2460. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2461. struct ieee80211_key_conf *key)
  2462. {
  2463. struct il_priv *il = hw->priv;
  2464. int ret = 0;
  2465. u8 sta_id = IL_INVALID_STATION;
  2466. u8 static_key;
  2467. D_MAC80211("enter\n");
  2468. if (il3945_mod_params.sw_crypto) {
  2469. D_MAC80211("leave - hwcrypto disabled\n");
  2470. return -EOPNOTSUPP;
  2471. }
  2472. /*
  2473. * To support IBSS RSN, don't program group keys in IBSS, the
  2474. * hardware will then not attempt to decrypt the frames.
  2475. */
  2476. if (vif->type == NL80211_IFTYPE_ADHOC &&
  2477. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  2478. D_MAC80211("leave - IBSS RSN\n");
  2479. return -EOPNOTSUPP;
  2480. }
  2481. static_key = !il_is_associated(il);
  2482. if (!static_key) {
  2483. sta_id = il_sta_id_or_broadcast(il, sta);
  2484. if (sta_id == IL_INVALID_STATION) {
  2485. D_MAC80211("leave - station not found\n");
  2486. return -EINVAL;
  2487. }
  2488. }
  2489. mutex_lock(&il->mutex);
  2490. il_scan_cancel_timeout(il, 100);
  2491. switch (cmd) {
  2492. case SET_KEY:
  2493. if (static_key)
  2494. ret = il3945_set_static_key(il, key);
  2495. else
  2496. ret = il3945_set_dynamic_key(il, key, sta_id);
  2497. D_MAC80211("enable hwcrypto key\n");
  2498. break;
  2499. case DISABLE_KEY:
  2500. if (static_key)
  2501. ret = il3945_remove_static_key(il);
  2502. else
  2503. ret = il3945_clear_sta_key_info(il, sta_id);
  2504. D_MAC80211("disable hwcrypto key\n");
  2505. break;
  2506. default:
  2507. ret = -EINVAL;
  2508. }
  2509. D_MAC80211("leave ret %d\n", ret);
  2510. mutex_unlock(&il->mutex);
  2511. return ret;
  2512. }
  2513. static int
  2514. il3945_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2515. struct ieee80211_sta *sta)
  2516. {
  2517. struct il_priv *il = hw->priv;
  2518. struct il3945_sta_priv *sta_priv = (void *)sta->drv_priv;
  2519. int ret;
  2520. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  2521. u8 sta_id;
  2522. mutex_lock(&il->mutex);
  2523. D_INFO("station %pM\n", sta->addr);
  2524. sta_priv->common.sta_id = IL_INVALID_STATION;
  2525. ret = il_add_station_common(il, sta->addr, is_ap, sta, &sta_id);
  2526. if (ret) {
  2527. IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
  2528. /* Should we return success if return code is EEXIST ? */
  2529. mutex_unlock(&il->mutex);
  2530. return ret;
  2531. }
  2532. sta_priv->common.sta_id = sta_id;
  2533. /* Initialize rate scaling */
  2534. D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
  2535. il3945_rs_rate_init(il, sta, sta_id);
  2536. mutex_unlock(&il->mutex);
  2537. return 0;
  2538. }
  2539. static void
  2540. il3945_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
  2541. unsigned int *total_flags, u64 multicast)
  2542. {
  2543. struct il_priv *il = hw->priv;
  2544. __le32 filter_or = 0, filter_nand = 0;
  2545. #define CHK(test, flag) do { \
  2546. if (*total_flags & (test)) \
  2547. filter_or |= (flag); \
  2548. else \
  2549. filter_nand |= (flag); \
  2550. } while (0)
  2551. D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
  2552. *total_flags);
  2553. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  2554. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK);
  2555. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  2556. #undef CHK
  2557. mutex_lock(&il->mutex);
  2558. il->staging.filter_flags &= ~filter_nand;
  2559. il->staging.filter_flags |= filter_or;
  2560. /*
  2561. * Not committing directly because hardware can perform a scan,
  2562. * but even if hw is ready, committing here breaks for some reason,
  2563. * we'll eventually commit the filter flags change anyway.
  2564. */
  2565. mutex_unlock(&il->mutex);
  2566. /*
  2567. * Receiving all multicast frames is always enabled by the
  2568. * default flags setup in il_connection_init_rx_config()
  2569. * since we currently do not support programming multicast
  2570. * filters into the device.
  2571. */
  2572. *total_flags &=
  2573. FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  2574. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  2575. }
  2576. /*****************************************************************************
  2577. *
  2578. * sysfs attributes
  2579. *
  2580. *****************************************************************************/
  2581. #ifdef CONFIG_IWLEGACY_DEBUG
  2582. /*
  2583. * The following adds a new attribute to the sysfs representation
  2584. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  2585. * used for controlling the debug level.
  2586. *
  2587. * See the level definitions in iwl for details.
  2588. *
  2589. * The debug_level being managed using sysfs below is a per device debug
  2590. * level that is used instead of the global debug level if it (the per
  2591. * device debug level) is set.
  2592. */
  2593. static ssize_t
  2594. il3945_show_debug_level(struct device *d, struct device_attribute *attr,
  2595. char *buf)
  2596. {
  2597. struct il_priv *il = dev_get_drvdata(d);
  2598. return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
  2599. }
  2600. static ssize_t
  2601. il3945_store_debug_level(struct device *d, struct device_attribute *attr,
  2602. const char *buf, size_t count)
  2603. {
  2604. struct il_priv *il = dev_get_drvdata(d);
  2605. unsigned long val;
  2606. int ret;
  2607. ret = strict_strtoul(buf, 0, &val);
  2608. if (ret)
  2609. IL_INFO("%s is not in hex or decimal form.\n", buf);
  2610. else
  2611. il->debug_level = val;
  2612. return strnlen(buf, count);
  2613. }
  2614. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il3945_show_debug_level,
  2615. il3945_store_debug_level);
  2616. #endif /* CONFIG_IWLEGACY_DEBUG */
  2617. static ssize_t
  2618. il3945_show_temperature(struct device *d, struct device_attribute *attr,
  2619. char *buf)
  2620. {
  2621. struct il_priv *il = dev_get_drvdata(d);
  2622. if (!il_is_alive(il))
  2623. return -EAGAIN;
  2624. return sprintf(buf, "%d\n", il3945_hw_get_temperature(il));
  2625. }
  2626. static DEVICE_ATTR(temperature, S_IRUGO, il3945_show_temperature, NULL);
  2627. static ssize_t
  2628. il3945_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
  2629. {
  2630. struct il_priv *il = dev_get_drvdata(d);
  2631. return sprintf(buf, "%d\n", il->tx_power_user_lmt);
  2632. }
  2633. static ssize_t
  2634. il3945_store_tx_power(struct device *d, struct device_attribute *attr,
  2635. const char *buf, size_t count)
  2636. {
  2637. struct il_priv *il = dev_get_drvdata(d);
  2638. char *p = (char *)buf;
  2639. u32 val;
  2640. val = simple_strtoul(p, &p, 10);
  2641. if (p == buf)
  2642. IL_INFO(": %s is not in decimal form.\n", buf);
  2643. else
  2644. il3945_hw_reg_set_txpower(il, val);
  2645. return count;
  2646. }
  2647. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il3945_show_tx_power,
  2648. il3945_store_tx_power);
  2649. static ssize_t
  2650. il3945_show_flags(struct device *d, struct device_attribute *attr, char *buf)
  2651. {
  2652. struct il_priv *il = dev_get_drvdata(d);
  2653. return sprintf(buf, "0x%04X\n", il->active.flags);
  2654. }
  2655. static ssize_t
  2656. il3945_store_flags(struct device *d, struct device_attribute *attr,
  2657. const char *buf, size_t count)
  2658. {
  2659. struct il_priv *il = dev_get_drvdata(d);
  2660. u32 flags = simple_strtoul(buf, NULL, 0);
  2661. mutex_lock(&il->mutex);
  2662. if (le32_to_cpu(il->staging.flags) != flags) {
  2663. /* Cancel any currently running scans... */
  2664. if (il_scan_cancel_timeout(il, 100))
  2665. IL_WARN("Could not cancel scan.\n");
  2666. else {
  2667. D_INFO("Committing rxon.flags = 0x%04X\n", flags);
  2668. il->staging.flags = cpu_to_le32(flags);
  2669. il3945_commit_rxon(il);
  2670. }
  2671. }
  2672. mutex_unlock(&il->mutex);
  2673. return count;
  2674. }
  2675. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, il3945_show_flags,
  2676. il3945_store_flags);
  2677. static ssize_t
  2678. il3945_show_filter_flags(struct device *d, struct device_attribute *attr,
  2679. char *buf)
  2680. {
  2681. struct il_priv *il = dev_get_drvdata(d);
  2682. return sprintf(buf, "0x%04X\n", le32_to_cpu(il->active.filter_flags));
  2683. }
  2684. static ssize_t
  2685. il3945_store_filter_flags(struct device *d, struct device_attribute *attr,
  2686. const char *buf, size_t count)
  2687. {
  2688. struct il_priv *il = dev_get_drvdata(d);
  2689. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  2690. mutex_lock(&il->mutex);
  2691. if (le32_to_cpu(il->staging.filter_flags) != filter_flags) {
  2692. /* Cancel any currently running scans... */
  2693. if (il_scan_cancel_timeout(il, 100))
  2694. IL_WARN("Could not cancel scan.\n");
  2695. else {
  2696. D_INFO("Committing rxon.filter_flags = " "0x%04X\n",
  2697. filter_flags);
  2698. il->staging.filter_flags = cpu_to_le32(filter_flags);
  2699. il3945_commit_rxon(il);
  2700. }
  2701. }
  2702. mutex_unlock(&il->mutex);
  2703. return count;
  2704. }
  2705. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, il3945_show_filter_flags,
  2706. il3945_store_filter_flags);
  2707. static ssize_t
  2708. il3945_show_measurement(struct device *d, struct device_attribute *attr,
  2709. char *buf)
  2710. {
  2711. struct il_priv *il = dev_get_drvdata(d);
  2712. struct il_spectrum_notification measure_report;
  2713. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  2714. u8 *data = (u8 *) &measure_report;
  2715. unsigned long flags;
  2716. spin_lock_irqsave(&il->lock, flags);
  2717. if (!(il->measurement_status & MEASUREMENT_READY)) {
  2718. spin_unlock_irqrestore(&il->lock, flags);
  2719. return 0;
  2720. }
  2721. memcpy(&measure_report, &il->measure_report, size);
  2722. il->measurement_status = 0;
  2723. spin_unlock_irqrestore(&il->lock, flags);
  2724. while (size && PAGE_SIZE - len) {
  2725. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  2726. PAGE_SIZE - len, 1);
  2727. len = strlen(buf);
  2728. if (PAGE_SIZE - len)
  2729. buf[len++] = '\n';
  2730. ofs += 16;
  2731. size -= min(size, 16U);
  2732. }
  2733. return len;
  2734. }
  2735. static ssize_t
  2736. il3945_store_measurement(struct device *d, struct device_attribute *attr,
  2737. const char *buf, size_t count)
  2738. {
  2739. struct il_priv *il = dev_get_drvdata(d);
  2740. struct ieee80211_measurement_params params = {
  2741. .channel = le16_to_cpu(il->active.channel),
  2742. .start_time = cpu_to_le64(il->_3945.last_tsf),
  2743. .duration = cpu_to_le16(1),
  2744. };
  2745. u8 type = IL_MEASURE_BASIC;
  2746. u8 buffer[32];
  2747. u8 channel;
  2748. if (count) {
  2749. char *p = buffer;
  2750. strlcpy(buffer, buf, sizeof(buffer));
  2751. channel = simple_strtoul(p, NULL, 0);
  2752. if (channel)
  2753. params.channel = channel;
  2754. p = buffer;
  2755. while (*p && *p != ' ')
  2756. p++;
  2757. if (*p)
  2758. type = simple_strtoul(p + 1, NULL, 0);
  2759. }
  2760. D_INFO("Invoking measurement of type %d on " "channel %d (for '%s')\n",
  2761. type, params.channel, buf);
  2762. il3945_get_measurement(il, &params, type);
  2763. return count;
  2764. }
  2765. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR, il3945_show_measurement,
  2766. il3945_store_measurement);
  2767. static ssize_t
  2768. il3945_store_retry_rate(struct device *d, struct device_attribute *attr,
  2769. const char *buf, size_t count)
  2770. {
  2771. struct il_priv *il = dev_get_drvdata(d);
  2772. il->retry_rate = simple_strtoul(buf, NULL, 0);
  2773. if (il->retry_rate <= 0)
  2774. il->retry_rate = 1;
  2775. return count;
  2776. }
  2777. static ssize_t
  2778. il3945_show_retry_rate(struct device *d, struct device_attribute *attr,
  2779. char *buf)
  2780. {
  2781. struct il_priv *il = dev_get_drvdata(d);
  2782. return sprintf(buf, "%d", il->retry_rate);
  2783. }
  2784. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, il3945_show_retry_rate,
  2785. il3945_store_retry_rate);
  2786. static ssize_t
  2787. il3945_show_channels(struct device *d, struct device_attribute *attr, char *buf)
  2788. {
  2789. /* all this shit doesn't belong into sysfs anyway */
  2790. return 0;
  2791. }
  2792. static DEVICE_ATTR(channels, S_IRUSR, il3945_show_channels, NULL);
  2793. static ssize_t
  2794. il3945_show_antenna(struct device *d, struct device_attribute *attr, char *buf)
  2795. {
  2796. struct il_priv *il = dev_get_drvdata(d);
  2797. if (!il_is_alive(il))
  2798. return -EAGAIN;
  2799. return sprintf(buf, "%d\n", il3945_mod_params.antenna);
  2800. }
  2801. static ssize_t
  2802. il3945_store_antenna(struct device *d, struct device_attribute *attr,
  2803. const char *buf, size_t count)
  2804. {
  2805. struct il_priv *il __maybe_unused = dev_get_drvdata(d);
  2806. int ant;
  2807. if (count == 0)
  2808. return 0;
  2809. if (sscanf(buf, "%1i", &ant) != 1) {
  2810. D_INFO("not in hex or decimal form.\n");
  2811. return count;
  2812. }
  2813. if (ant >= 0 && ant <= 2) {
  2814. D_INFO("Setting antenna select to %d.\n", ant);
  2815. il3945_mod_params.antenna = (enum il3945_antenna)ant;
  2816. } else
  2817. D_INFO("Bad antenna select value %d.\n", ant);
  2818. return count;
  2819. }
  2820. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, il3945_show_antenna,
  2821. il3945_store_antenna);
  2822. static ssize_t
  2823. il3945_show_status(struct device *d, struct device_attribute *attr, char *buf)
  2824. {
  2825. struct il_priv *il = dev_get_drvdata(d);
  2826. if (!il_is_alive(il))
  2827. return -EAGAIN;
  2828. return sprintf(buf, "0x%08x\n", (int)il->status);
  2829. }
  2830. static DEVICE_ATTR(status, S_IRUGO, il3945_show_status, NULL);
  2831. static ssize_t
  2832. il3945_dump_error_log(struct device *d, struct device_attribute *attr,
  2833. const char *buf, size_t count)
  2834. {
  2835. struct il_priv *il = dev_get_drvdata(d);
  2836. char *p = (char *)buf;
  2837. if (p[0] == '1')
  2838. il3945_dump_nic_error_log(il);
  2839. return strnlen(buf, count);
  2840. }
  2841. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, il3945_dump_error_log);
  2842. /*****************************************************************************
  2843. *
  2844. * driver setup and tear down
  2845. *
  2846. *****************************************************************************/
  2847. static void
  2848. il3945_setup_deferred_work(struct il_priv *il)
  2849. {
  2850. il->workqueue = create_singlethread_workqueue(DRV_NAME);
  2851. init_waitqueue_head(&il->wait_command_queue);
  2852. INIT_WORK(&il->restart, il3945_bg_restart);
  2853. INIT_WORK(&il->rx_replenish, il3945_bg_rx_replenish);
  2854. INIT_DELAYED_WORK(&il->init_alive_start, il3945_bg_init_alive_start);
  2855. INIT_DELAYED_WORK(&il->alive_start, il3945_bg_alive_start);
  2856. INIT_DELAYED_WORK(&il->_3945.rfkill_poll, il3945_rfkill_poll);
  2857. il_setup_scan_deferred_work(il);
  2858. il3945_hw_setup_deferred_work(il);
  2859. init_timer(&il->watchdog);
  2860. il->watchdog.data = (unsigned long)il;
  2861. il->watchdog.function = il_bg_watchdog;
  2862. tasklet_init(&il->irq_tasklet,
  2863. (void (*)(unsigned long))il3945_irq_tasklet,
  2864. (unsigned long)il);
  2865. }
  2866. static void
  2867. il3945_cancel_deferred_work(struct il_priv *il)
  2868. {
  2869. il3945_hw_cancel_deferred_work(il);
  2870. cancel_delayed_work_sync(&il->init_alive_start);
  2871. cancel_delayed_work(&il->alive_start);
  2872. il_cancel_scan_deferred_work(il);
  2873. }
  2874. static struct attribute *il3945_sysfs_entries[] = {
  2875. &dev_attr_antenna.attr,
  2876. &dev_attr_channels.attr,
  2877. &dev_attr_dump_errors.attr,
  2878. &dev_attr_flags.attr,
  2879. &dev_attr_filter_flags.attr,
  2880. &dev_attr_measurement.attr,
  2881. &dev_attr_retry_rate.attr,
  2882. &dev_attr_status.attr,
  2883. &dev_attr_temperature.attr,
  2884. &dev_attr_tx_power.attr,
  2885. #ifdef CONFIG_IWLEGACY_DEBUG
  2886. &dev_attr_debug_level.attr,
  2887. #endif
  2888. NULL
  2889. };
  2890. static struct attribute_group il3945_attribute_group = {
  2891. .name = NULL, /* put in device directory */
  2892. .attrs = il3945_sysfs_entries,
  2893. };
  2894. struct ieee80211_ops il3945_mac_ops = {
  2895. .tx = il3945_mac_tx,
  2896. .start = il3945_mac_start,
  2897. .stop = il3945_mac_stop,
  2898. .add_interface = il_mac_add_interface,
  2899. .remove_interface = il_mac_remove_interface,
  2900. .change_interface = il_mac_change_interface,
  2901. .config = il_mac_config,
  2902. .configure_filter = il3945_configure_filter,
  2903. .set_key = il3945_mac_set_key,
  2904. .conf_tx = il_mac_conf_tx,
  2905. .reset_tsf = il_mac_reset_tsf,
  2906. .bss_info_changed = il_mac_bss_info_changed,
  2907. .hw_scan = il_mac_hw_scan,
  2908. .sta_add = il3945_mac_sta_add,
  2909. .sta_remove = il_mac_sta_remove,
  2910. .tx_last_beacon = il_mac_tx_last_beacon,
  2911. .flush = il_mac_flush,
  2912. };
  2913. static int
  2914. il3945_init_drv(struct il_priv *il)
  2915. {
  2916. int ret;
  2917. struct il3945_eeprom *eeprom = (struct il3945_eeprom *)il->eeprom;
  2918. il->retry_rate = 1;
  2919. il->beacon_skb = NULL;
  2920. spin_lock_init(&il->sta_lock);
  2921. spin_lock_init(&il->hcmd_lock);
  2922. INIT_LIST_HEAD(&il->free_frames);
  2923. mutex_init(&il->mutex);
  2924. il->ieee_channels = NULL;
  2925. il->ieee_rates = NULL;
  2926. il->band = IEEE80211_BAND_2GHZ;
  2927. il->iw_mode = NL80211_IFTYPE_STATION;
  2928. il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
  2929. /* initialize force reset */
  2930. il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
  2931. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  2932. IL_WARN("Unsupported EEPROM version: 0x%04X\n",
  2933. eeprom->version);
  2934. ret = -EINVAL;
  2935. goto err;
  2936. }
  2937. ret = il_init_channel_map(il);
  2938. if (ret) {
  2939. IL_ERR("initializing regulatory failed: %d\n", ret);
  2940. goto err;
  2941. }
  2942. /* Set up txpower settings in driver for all channels */
  2943. if (il3945_txpower_set_from_eeprom(il)) {
  2944. ret = -EIO;
  2945. goto err_free_channel_map;
  2946. }
  2947. ret = il_init_geos(il);
  2948. if (ret) {
  2949. IL_ERR("initializing geos failed: %d\n", ret);
  2950. goto err_free_channel_map;
  2951. }
  2952. il3945_init_hw_rates(il, il->ieee_rates);
  2953. return 0;
  2954. err_free_channel_map:
  2955. il_free_channel_map(il);
  2956. err:
  2957. return ret;
  2958. }
  2959. #define IL3945_MAX_PROBE_REQUEST 200
  2960. static int
  2961. il3945_setup_mac(struct il_priv *il)
  2962. {
  2963. int ret;
  2964. struct ieee80211_hw *hw = il->hw;
  2965. hw->rate_control_algorithm = "iwl-3945-rs";
  2966. hw->sta_data_size = sizeof(struct il3945_sta_priv);
  2967. hw->vif_data_size = sizeof(struct il_vif_priv);
  2968. /* Tell mac80211 our characteristics */
  2969. hw->flags = IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_SPECTRUM_MGMT |
  2970. IEEE80211_HW_SUPPORTS_PS | IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  2971. hw->wiphy->interface_modes =
  2972. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
  2973. hw->wiphy->flags |=
  2974. WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS |
  2975. WIPHY_FLAG_IBSS_RSN;
  2976. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2977. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
  2978. /* we create the 802.11 header and a zero-length SSID element */
  2979. hw->wiphy->max_scan_ie_len = IL3945_MAX_PROBE_REQUEST - 24 - 2;
  2980. /* Default value; 4 EDCA QOS priorities */
  2981. hw->queues = 4;
  2982. if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
  2983. il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  2984. &il->bands[IEEE80211_BAND_2GHZ];
  2985. if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
  2986. il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  2987. &il->bands[IEEE80211_BAND_5GHZ];
  2988. il_leds_init(il);
  2989. ret = ieee80211_register_hw(il->hw);
  2990. if (ret) {
  2991. IL_ERR("Failed to register hw (error %d)\n", ret);
  2992. return ret;
  2993. }
  2994. il->mac80211_registered = 1;
  2995. return 0;
  2996. }
  2997. static int
  2998. il3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2999. {
  3000. int err = 0;
  3001. struct il_priv *il;
  3002. struct ieee80211_hw *hw;
  3003. struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
  3004. struct il3945_eeprom *eeprom;
  3005. unsigned long flags;
  3006. /***********************
  3007. * 1. Allocating HW data
  3008. * ********************/
  3009. hw = ieee80211_alloc_hw(sizeof(struct il_priv), &il3945_mac_ops);
  3010. if (!hw) {
  3011. err = -ENOMEM;
  3012. goto out;
  3013. }
  3014. il = hw->priv;
  3015. il->hw = hw;
  3016. SET_IEEE80211_DEV(hw, &pdev->dev);
  3017. il->cmd_queue = IL39_CMD_QUEUE_NUM;
  3018. /*
  3019. * Disabling hardware scan means that mac80211 will perform scans
  3020. * "the hard way", rather than using device's scan.
  3021. */
  3022. if (il3945_mod_params.disable_hw_scan) {
  3023. D_INFO("Disabling hw_scan\n");
  3024. il3945_mac_ops.hw_scan = NULL;
  3025. }
  3026. D_INFO("*** LOAD DRIVER ***\n");
  3027. il->cfg = cfg;
  3028. il->ops = &il3945_ops;
  3029. #ifdef CONFIG_IWLEGACY_DEBUGFS
  3030. il->debugfs_ops = &il3945_debugfs_ops;
  3031. #endif
  3032. il->pci_dev = pdev;
  3033. il->inta_mask = CSR_INI_SET_MASK;
  3034. /***************************
  3035. * 2. Initializing PCI bus
  3036. * *************************/
  3037. pci_disable_link_state(pdev,
  3038. PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  3039. PCIE_LINK_STATE_CLKPM);
  3040. if (pci_enable_device(pdev)) {
  3041. err = -ENODEV;
  3042. goto out_ieee80211_free_hw;
  3043. }
  3044. pci_set_master(pdev);
  3045. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3046. if (!err)
  3047. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3048. if (err) {
  3049. IL_WARN("No suitable DMA available.\n");
  3050. goto out_pci_disable_device;
  3051. }
  3052. pci_set_drvdata(pdev, il);
  3053. err = pci_request_regions(pdev, DRV_NAME);
  3054. if (err)
  3055. goto out_pci_disable_device;
  3056. /***********************
  3057. * 3. Read REV Register
  3058. * ********************/
  3059. il->hw_base = pci_ioremap_bar(pdev, 0);
  3060. if (!il->hw_base) {
  3061. err = -ENODEV;
  3062. goto out_pci_release_regions;
  3063. }
  3064. D_INFO("pci_resource_len = 0x%08llx\n",
  3065. (unsigned long long)pci_resource_len(pdev, 0));
  3066. D_INFO("pci_resource_base = %p\n", il->hw_base);
  3067. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  3068. * PCI Tx retries from interfering with C3 CPU state */
  3069. pci_write_config_byte(pdev, 0x41, 0x00);
  3070. /* these spin locks will be used in apm_init and EEPROM access
  3071. * we should init now
  3072. */
  3073. spin_lock_init(&il->reg_lock);
  3074. spin_lock_init(&il->lock);
  3075. /*
  3076. * stop and reset the on-board processor just in case it is in a
  3077. * strange state ... like being left stranded by a primary kernel
  3078. * and this is now the kdump kernel trying to start up
  3079. */
  3080. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  3081. /***********************
  3082. * 4. Read EEPROM
  3083. * ********************/
  3084. /* Read the EEPROM */
  3085. err = il_eeprom_init(il);
  3086. if (err) {
  3087. IL_ERR("Unable to init EEPROM\n");
  3088. goto out_iounmap;
  3089. }
  3090. /* MAC Address location in EEPROM same for 3945/4965 */
  3091. eeprom = (struct il3945_eeprom *)il->eeprom;
  3092. D_INFO("MAC address: %pM\n", eeprom->mac_address);
  3093. SET_IEEE80211_PERM_ADDR(il->hw, eeprom->mac_address);
  3094. /***********************
  3095. * 5. Setup HW Constants
  3096. * ********************/
  3097. /* Device-specific setup */
  3098. if (il3945_hw_set_hw_params(il)) {
  3099. IL_ERR("failed to set hw settings\n");
  3100. goto out_eeprom_free;
  3101. }
  3102. /***********************
  3103. * 6. Setup il
  3104. * ********************/
  3105. err = il3945_init_drv(il);
  3106. if (err) {
  3107. IL_ERR("initializing driver failed\n");
  3108. goto out_unset_hw_params;
  3109. }
  3110. IL_INFO("Detected Intel Wireless WiFi Link %s\n", il->cfg->name);
  3111. /***********************
  3112. * 7. Setup Services
  3113. * ********************/
  3114. spin_lock_irqsave(&il->lock, flags);
  3115. il_disable_interrupts(il);
  3116. spin_unlock_irqrestore(&il->lock, flags);
  3117. pci_enable_msi(il->pci_dev);
  3118. err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
  3119. if (err) {
  3120. IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
  3121. goto out_disable_msi;
  3122. }
  3123. err = sysfs_create_group(&pdev->dev.kobj, &il3945_attribute_group);
  3124. if (err) {
  3125. IL_ERR("failed to create sysfs device attributes\n");
  3126. goto out_release_irq;
  3127. }
  3128. il_set_rxon_channel(il, &il->bands[IEEE80211_BAND_2GHZ].channels[5]);
  3129. il3945_setup_deferred_work(il);
  3130. il3945_setup_handlers(il);
  3131. il_power_initialize(il);
  3132. /*********************************
  3133. * 8. Setup and Register mac80211
  3134. * *******************************/
  3135. il_enable_interrupts(il);
  3136. err = il3945_setup_mac(il);
  3137. if (err)
  3138. goto out_remove_sysfs;
  3139. err = il_dbgfs_register(il, DRV_NAME);
  3140. if (err)
  3141. IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
  3142. err);
  3143. /* Start monitoring the killswitch */
  3144. queue_delayed_work(il->workqueue, &il->_3945.rfkill_poll, 2 * HZ);
  3145. return 0;
  3146. out_remove_sysfs:
  3147. destroy_workqueue(il->workqueue);
  3148. il->workqueue = NULL;
  3149. sysfs_remove_group(&pdev->dev.kobj, &il3945_attribute_group);
  3150. out_release_irq:
  3151. free_irq(il->pci_dev->irq, il);
  3152. out_disable_msi:
  3153. pci_disable_msi(il->pci_dev);
  3154. il_free_geos(il);
  3155. il_free_channel_map(il);
  3156. out_unset_hw_params:
  3157. il3945_unset_hw_params(il);
  3158. out_eeprom_free:
  3159. il_eeprom_free(il);
  3160. out_iounmap:
  3161. iounmap(il->hw_base);
  3162. out_pci_release_regions:
  3163. pci_release_regions(pdev);
  3164. out_pci_disable_device:
  3165. pci_set_drvdata(pdev, NULL);
  3166. pci_disable_device(pdev);
  3167. out_ieee80211_free_hw:
  3168. ieee80211_free_hw(il->hw);
  3169. out:
  3170. return err;
  3171. }
  3172. static void
  3173. il3945_pci_remove(struct pci_dev *pdev)
  3174. {
  3175. struct il_priv *il = pci_get_drvdata(pdev);
  3176. unsigned long flags;
  3177. if (!il)
  3178. return;
  3179. D_INFO("*** UNLOAD DRIVER ***\n");
  3180. il_dbgfs_unregister(il);
  3181. set_bit(S_EXIT_PENDING, &il->status);
  3182. il_leds_exit(il);
  3183. if (il->mac80211_registered) {
  3184. ieee80211_unregister_hw(il->hw);
  3185. il->mac80211_registered = 0;
  3186. } else {
  3187. il3945_down(il);
  3188. }
  3189. /*
  3190. * Make sure device is reset to low power before unloading driver.
  3191. * This may be redundant with il_down(), but there are paths to
  3192. * run il_down() without calling apm_ops.stop(), and there are
  3193. * paths to avoid running il_down() at all before leaving driver.
  3194. * This (inexpensive) call *makes sure* device is reset.
  3195. */
  3196. il_apm_stop(il);
  3197. /* make sure we flush any pending irq or
  3198. * tasklet for the driver
  3199. */
  3200. spin_lock_irqsave(&il->lock, flags);
  3201. il_disable_interrupts(il);
  3202. spin_unlock_irqrestore(&il->lock, flags);
  3203. il3945_synchronize_irq(il);
  3204. sysfs_remove_group(&pdev->dev.kobj, &il3945_attribute_group);
  3205. cancel_delayed_work_sync(&il->_3945.rfkill_poll);
  3206. il3945_dealloc_ucode_pci(il);
  3207. if (il->rxq.bd)
  3208. il3945_rx_queue_free(il, &il->rxq);
  3209. il3945_hw_txq_ctx_free(il);
  3210. il3945_unset_hw_params(il);
  3211. /*netif_stop_queue(dev); */
  3212. flush_workqueue(il->workqueue);
  3213. /* ieee80211_unregister_hw calls il3945_mac_stop, which flushes
  3214. * il->workqueue... so we can't take down the workqueue
  3215. * until now... */
  3216. destroy_workqueue(il->workqueue);
  3217. il->workqueue = NULL;
  3218. free_irq(pdev->irq, il);
  3219. pci_disable_msi(pdev);
  3220. iounmap(il->hw_base);
  3221. pci_release_regions(pdev);
  3222. pci_disable_device(pdev);
  3223. pci_set_drvdata(pdev, NULL);
  3224. il_free_channel_map(il);
  3225. il_free_geos(il);
  3226. kfree(il->scan_cmd);
  3227. if (il->beacon_skb)
  3228. dev_kfree_skb(il->beacon_skb);
  3229. ieee80211_free_hw(il->hw);
  3230. }
  3231. /*****************************************************************************
  3232. *
  3233. * driver and module entry point
  3234. *
  3235. *****************************************************************************/
  3236. static struct pci_driver il3945_driver = {
  3237. .name = DRV_NAME,
  3238. .id_table = il3945_hw_card_ids,
  3239. .probe = il3945_pci_probe,
  3240. .remove = il3945_pci_remove,
  3241. .driver.pm = IL_LEGACY_PM_OPS,
  3242. };
  3243. static int __init
  3244. il3945_init(void)
  3245. {
  3246. int ret;
  3247. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  3248. pr_info(DRV_COPYRIGHT "\n");
  3249. ret = il3945_rate_control_register();
  3250. if (ret) {
  3251. pr_err("Unable to register rate control algorithm: %d\n", ret);
  3252. return ret;
  3253. }
  3254. ret = pci_register_driver(&il3945_driver);
  3255. if (ret) {
  3256. pr_err("Unable to initialize PCI module\n");
  3257. goto error_register;
  3258. }
  3259. return ret;
  3260. error_register:
  3261. il3945_rate_control_unregister();
  3262. return ret;
  3263. }
  3264. static void __exit
  3265. il3945_exit(void)
  3266. {
  3267. pci_unregister_driver(&il3945_driver);
  3268. il3945_rate_control_unregister();
  3269. }
  3270. MODULE_FIRMWARE(IL3945_MODULE_FIRMWARE(IL3945_UCODE_API_MAX));
  3271. module_param_named(antenna, il3945_mod_params.antenna, int, S_IRUGO);
  3272. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  3273. module_param_named(swcrypto, il3945_mod_params.sw_crypto, int, S_IRUGO);
  3274. MODULE_PARM_DESC(swcrypto, "using software crypto (default 1 [software])");
  3275. module_param_named(disable_hw_scan, il3945_mod_params.disable_hw_scan, int,
  3276. S_IRUGO);
  3277. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 1)");
  3278. #ifdef CONFIG_IWLEGACY_DEBUG
  3279. module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
  3280. MODULE_PARM_DESC(debug, "debug output mask");
  3281. #endif
  3282. module_param_named(fw_restart, il3945_mod_params.restart_fw, int, S_IRUGO);
  3283. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  3284. module_exit(il3945_exit);
  3285. module_init(il3945_init);