iwl4965-base.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633
  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/wireless.h>
  42. #include <linux/firmware.h>
  43. #include <linux/etherdevice.h>
  44. #include <linux/if_arp.h>
  45. #include <net/mac80211.h>
  46. #include <asm/div64.h>
  47. #define DRV_NAME "iwl4965"
  48. #include "iwl-eeprom.h"
  49. #include "iwl-dev.h"
  50. #include "iwl-core.h"
  51. #include "iwl-io.h"
  52. #include "iwl-helpers.h"
  53. #include "iwl-sta.h"
  54. #include "iwl-4965-calib.h"
  55. #include "iwl-4965.h"
  56. #include "iwl-4965-led.h"
  57. /******************************************************************************
  58. *
  59. * module boiler plate
  60. *
  61. ******************************************************************************/
  62. /*
  63. * module name, copyright, version, etc.
  64. */
  65. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
  66. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  67. #define VD "d"
  68. #else
  69. #define VD
  70. #endif
  71. #define DRV_VERSION IWLWIFI_VERSION VD
  72. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  73. MODULE_VERSION(DRV_VERSION);
  74. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  75. MODULE_LICENSE("GPL");
  76. MODULE_ALIAS("iwl4965");
  77. void iwl4965_update_chain_flags(struct iwl_priv *priv)
  78. {
  79. struct iwl_rxon_context *ctx;
  80. if (priv->cfg->ops->hcmd->set_rxon_chain) {
  81. for_each_context(priv, ctx) {
  82. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  83. if (ctx->active.rx_chain != ctx->staging.rx_chain)
  84. iwl_legacy_commit_rxon(priv, ctx);
  85. }
  86. }
  87. }
  88. static void iwl4965_clear_free_frames(struct iwl_priv *priv)
  89. {
  90. struct list_head *element;
  91. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  92. priv->frames_count);
  93. while (!list_empty(&priv->free_frames)) {
  94. element = priv->free_frames.next;
  95. list_del(element);
  96. kfree(list_entry(element, struct iwl_frame, list));
  97. priv->frames_count--;
  98. }
  99. if (priv->frames_count) {
  100. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  101. priv->frames_count);
  102. priv->frames_count = 0;
  103. }
  104. }
  105. static struct iwl_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
  106. {
  107. struct iwl_frame *frame;
  108. struct list_head *element;
  109. if (list_empty(&priv->free_frames)) {
  110. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  111. if (!frame) {
  112. IWL_ERR(priv, "Could not allocate frame!\n");
  113. return NULL;
  114. }
  115. priv->frames_count++;
  116. return frame;
  117. }
  118. element = priv->free_frames.next;
  119. list_del(element);
  120. return list_entry(element, struct iwl_frame, list);
  121. }
  122. static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
  123. {
  124. memset(frame, 0, sizeof(*frame));
  125. list_add(&frame->list, &priv->free_frames);
  126. }
  127. static u32 iwl4965_fill_beacon_frame(struct iwl_priv *priv,
  128. struct ieee80211_hdr *hdr,
  129. int left)
  130. {
  131. lockdep_assert_held(&priv->mutex);
  132. if (!priv->beacon_skb)
  133. return 0;
  134. if (priv->beacon_skb->len > left)
  135. return 0;
  136. memcpy(hdr, priv->beacon_skb->data, priv->beacon_skb->len);
  137. return priv->beacon_skb->len;
  138. }
  139. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  140. static void iwl4965_set_beacon_tim(struct iwl_priv *priv,
  141. struct iwl_tx_beacon_cmd *tx_beacon_cmd,
  142. u8 *beacon, u32 frame_size)
  143. {
  144. u16 tim_idx;
  145. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  146. /*
  147. * The index is relative to frame start but we start looking at the
  148. * variable-length part of the beacon.
  149. */
  150. tim_idx = mgmt->u.beacon.variable - beacon;
  151. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  152. while ((tim_idx < (frame_size - 2)) &&
  153. (beacon[tim_idx] != WLAN_EID_TIM))
  154. tim_idx += beacon[tim_idx+1] + 2;
  155. /* If TIM field was found, set variables */
  156. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  157. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  158. tx_beacon_cmd->tim_size = beacon[tim_idx+1];
  159. } else
  160. IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
  161. }
  162. static unsigned int iwl4965_hw_get_beacon_cmd(struct iwl_priv *priv,
  163. struct iwl_frame *frame)
  164. {
  165. struct iwl_tx_beacon_cmd *tx_beacon_cmd;
  166. u32 frame_size;
  167. u32 rate_flags;
  168. u32 rate;
  169. /*
  170. * We have to set up the TX command, the TX Beacon command, and the
  171. * beacon contents.
  172. */
  173. lockdep_assert_held(&priv->mutex);
  174. if (!priv->beacon_ctx) {
  175. IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
  176. return 0;
  177. }
  178. /* Initialize memory */
  179. tx_beacon_cmd = &frame->u.beacon;
  180. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  181. /* Set up TX beacon contents */
  182. frame_size = iwl4965_fill_beacon_frame(priv, tx_beacon_cmd->frame,
  183. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  184. if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
  185. return 0;
  186. if (!frame_size)
  187. return 0;
  188. /* Set up TX command fields */
  189. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  190. tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
  191. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  192. tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
  193. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
  194. /* Set up TX beacon command fields */
  195. iwl4965_set_beacon_tim(priv, tx_beacon_cmd, (u8 *)tx_beacon_cmd->frame,
  196. frame_size);
  197. /* Set up packet rate and flags */
  198. rate = iwl_legacy_get_lowest_plcp(priv, priv->beacon_ctx);
  199. priv->mgmt_tx_ant = iwl4965_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  200. priv->hw_params.valid_tx_ant);
  201. rate_flags = iwl4965_ant_idx_to_flags(priv->mgmt_tx_ant);
  202. if ((rate >= IWL_FIRST_CCK_RATE) && (rate <= IWL_LAST_CCK_RATE))
  203. rate_flags |= RATE_MCS_CCK_MSK;
  204. tx_beacon_cmd->tx.rate_n_flags = iwl4965_hw_set_rate_n_flags(rate,
  205. rate_flags);
  206. return sizeof(*tx_beacon_cmd) + frame_size;
  207. }
  208. int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
  209. {
  210. struct iwl_frame *frame;
  211. unsigned int frame_size;
  212. int rc;
  213. frame = iwl4965_get_free_frame(priv);
  214. if (!frame) {
  215. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  216. "command.\n");
  217. return -ENOMEM;
  218. }
  219. frame_size = iwl4965_hw_get_beacon_cmd(priv, frame);
  220. if (!frame_size) {
  221. IWL_ERR(priv, "Error configuring the beacon command\n");
  222. iwl4965_free_frame(priv, frame);
  223. return -EINVAL;
  224. }
  225. rc = iwl_legacy_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  226. &frame->u.cmd[0]);
  227. iwl4965_free_frame(priv, frame);
  228. return rc;
  229. }
  230. static inline dma_addr_t iwl4965_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  231. {
  232. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  233. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  234. if (sizeof(dma_addr_t) > sizeof(u32))
  235. addr |=
  236. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  237. return addr;
  238. }
  239. static inline u16 iwl4965_tfd_tb_get_len(struct iwl_tfd *tfd, u8 idx)
  240. {
  241. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  242. return le16_to_cpu(tb->hi_n_len) >> 4;
  243. }
  244. static inline void iwl4965_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  245. dma_addr_t addr, u16 len)
  246. {
  247. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  248. u16 hi_n_len = len << 4;
  249. put_unaligned_le32(addr, &tb->lo);
  250. if (sizeof(dma_addr_t) > sizeof(u32))
  251. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  252. tb->hi_n_len = cpu_to_le16(hi_n_len);
  253. tfd->num_tbs = idx + 1;
  254. }
  255. static inline u8 iwl4965_tfd_get_num_tbs(struct iwl_tfd *tfd)
  256. {
  257. return tfd->num_tbs & 0x1f;
  258. }
  259. /**
  260. * iwl4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  261. * @priv - driver private data
  262. * @txq - tx queue
  263. *
  264. * Does NOT advance any TFD circular buffer read/write indexes
  265. * Does NOT free the TFD itself (which is within circular buffer)
  266. */
  267. void iwl4965_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  268. {
  269. struct iwl_tfd *tfd_tmp = (struct iwl_tfd *)txq->tfds;
  270. struct iwl_tfd *tfd;
  271. struct pci_dev *dev = priv->pci_dev;
  272. int index = txq->q.read_ptr;
  273. int i;
  274. int num_tbs;
  275. tfd = &tfd_tmp[index];
  276. /* Sanity check on number of chunks */
  277. num_tbs = iwl4965_tfd_get_num_tbs(tfd);
  278. if (num_tbs >= IWL_NUM_OF_TBS) {
  279. IWL_ERR(priv, "Too many chunks: %i\n", num_tbs);
  280. /* @todo issue fatal error, it is quite serious situation */
  281. return;
  282. }
  283. /* Unmap tx_cmd */
  284. if (num_tbs)
  285. pci_unmap_single(dev,
  286. dma_unmap_addr(&txq->meta[index], mapping),
  287. dma_unmap_len(&txq->meta[index], len),
  288. PCI_DMA_BIDIRECTIONAL);
  289. /* Unmap chunks, if any. */
  290. for (i = 1; i < num_tbs; i++)
  291. pci_unmap_single(dev, iwl4965_tfd_tb_get_addr(tfd, i),
  292. iwl4965_tfd_tb_get_len(tfd, i),
  293. PCI_DMA_TODEVICE);
  294. /* free SKB */
  295. if (txq->txb) {
  296. struct sk_buff *skb;
  297. skb = txq->txb[txq->q.read_ptr].skb;
  298. /* can be called from irqs-disabled context */
  299. if (skb) {
  300. dev_kfree_skb_any(skb);
  301. txq->txb[txq->q.read_ptr].skb = NULL;
  302. }
  303. }
  304. }
  305. int iwl4965_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv,
  306. struct iwl_tx_queue *txq,
  307. dma_addr_t addr, u16 len,
  308. u8 reset, u8 pad)
  309. {
  310. struct iwl_queue *q;
  311. struct iwl_tfd *tfd, *tfd_tmp;
  312. u32 num_tbs;
  313. q = &txq->q;
  314. tfd_tmp = (struct iwl_tfd *)txq->tfds;
  315. tfd = &tfd_tmp[q->write_ptr];
  316. if (reset)
  317. memset(tfd, 0, sizeof(*tfd));
  318. num_tbs = iwl4965_tfd_get_num_tbs(tfd);
  319. /* Each TFD can point to a maximum 20 Tx buffers */
  320. if (num_tbs >= IWL_NUM_OF_TBS) {
  321. IWL_ERR(priv, "Error can not send more than %d chunks\n",
  322. IWL_NUM_OF_TBS);
  323. return -EINVAL;
  324. }
  325. BUG_ON(addr & ~DMA_BIT_MASK(36));
  326. if (unlikely(addr & ~IWL_TX_DMA_MASK))
  327. IWL_ERR(priv, "Unaligned address = %llx\n",
  328. (unsigned long long)addr);
  329. iwl4965_tfd_set_tb(tfd, num_tbs, addr, len);
  330. return 0;
  331. }
  332. /*
  333. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  334. * given Tx queue, and enable the DMA channel used for that queue.
  335. *
  336. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  337. * channels supported in hardware.
  338. */
  339. int iwl4965_hw_tx_queue_init(struct iwl_priv *priv,
  340. struct iwl_tx_queue *txq)
  341. {
  342. int txq_id = txq->q.id;
  343. /* Circular buffer (TFD queue in DRAM) physical base address */
  344. iwl_legacy_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  345. txq->q.dma_addr >> 8);
  346. return 0;
  347. }
  348. /******************************************************************************
  349. *
  350. * Generic RX handler implementations
  351. *
  352. ******************************************************************************/
  353. static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
  354. struct iwl_rx_mem_buffer *rxb)
  355. {
  356. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  357. struct iwl_alive_resp *palive;
  358. struct delayed_work *pwork;
  359. palive = &pkt->u.alive_frame;
  360. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  361. "0x%01X 0x%01X\n",
  362. palive->is_valid, palive->ver_type,
  363. palive->ver_subtype);
  364. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  365. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  366. memcpy(&priv->card_alive_init,
  367. &pkt->u.alive_frame,
  368. sizeof(struct iwl_init_alive_resp));
  369. pwork = &priv->init_alive_start;
  370. } else {
  371. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  372. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  373. sizeof(struct iwl_alive_resp));
  374. pwork = &priv->alive_start;
  375. }
  376. /* We delay the ALIVE response by 5ms to
  377. * give the HW RF Kill time to activate... */
  378. if (palive->is_valid == UCODE_VALID_OK)
  379. queue_delayed_work(priv->workqueue, pwork,
  380. msecs_to_jiffies(5));
  381. else
  382. IWL_WARN(priv, "uCode did not respond OK.\n");
  383. }
  384. /**
  385. * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
  386. *
  387. * This callback is provided in order to send a statistics request.
  388. *
  389. * This timer function is continually reset to execute within
  390. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  391. * was received. We need to ensure we receive the statistics in order
  392. * to update the temperature used for calibrating the TXPOWER.
  393. */
  394. static void iwl4965_bg_statistics_periodic(unsigned long data)
  395. {
  396. struct iwl_priv *priv = (struct iwl_priv *)data;
  397. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  398. return;
  399. /* dont send host command if rf-kill is on */
  400. if (!iwl_legacy_is_ready_rf(priv))
  401. return;
  402. iwl_legacy_send_statistics_request(priv, CMD_ASYNC, false);
  403. }
  404. static void iwl4965_print_cont_event_trace(struct iwl_priv *priv, u32 base,
  405. u32 start_idx, u32 num_events,
  406. u32 mode)
  407. {
  408. u32 i;
  409. u32 ptr; /* SRAM byte address of log data */
  410. u32 ev, time, data; /* event log data */
  411. unsigned long reg_flags;
  412. if (mode == 0)
  413. ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
  414. else
  415. ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
  416. /* Make sure device is powered up for SRAM reads */
  417. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  418. if (iwl_grab_nic_access(priv)) {
  419. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  420. return;
  421. }
  422. /* Set starting address; reads will auto-increment */
  423. _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
  424. rmb();
  425. /*
  426. * "time" is actually "data" for mode 0 (no timestamp).
  427. * place event id # at far right for easier visual parsing.
  428. */
  429. for (i = 0; i < num_events; i++) {
  430. ev = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  431. time = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  432. if (mode == 0) {
  433. trace_iwlwifi_legacy_dev_ucode_cont_event(priv,
  434. 0, time, ev);
  435. } else {
  436. data = _iwl_legacy_read_direct32(priv,
  437. HBUS_TARG_MEM_RDAT);
  438. trace_iwlwifi_legacy_dev_ucode_cont_event(priv,
  439. time, data, ev);
  440. }
  441. }
  442. /* Allow device to power down */
  443. iwl_release_nic_access(priv);
  444. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  445. }
  446. static void iwl4965_continuous_event_trace(struct iwl_priv *priv)
  447. {
  448. u32 capacity; /* event log capacity in # entries */
  449. u32 base; /* SRAM byte address of event log header */
  450. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  451. u32 num_wraps; /* # times uCode wrapped to top of log */
  452. u32 next_entry; /* index of next entry to be written by uCode */
  453. if (priv->ucode_type == UCODE_INIT)
  454. base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
  455. else
  456. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  457. if (priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  458. capacity = iwl_legacy_read_targ_mem(priv, base);
  459. num_wraps = iwl_legacy_read_targ_mem(priv,
  460. base + (2 * sizeof(u32)));
  461. mode = iwl_legacy_read_targ_mem(priv, base + (1 * sizeof(u32)));
  462. next_entry = iwl_legacy_read_targ_mem(priv,
  463. base + (3 * sizeof(u32)));
  464. } else
  465. return;
  466. if (num_wraps == priv->event_log.num_wraps) {
  467. iwl4965_print_cont_event_trace(priv,
  468. base, priv->event_log.next_entry,
  469. next_entry - priv->event_log.next_entry,
  470. mode);
  471. priv->event_log.non_wraps_count++;
  472. } else {
  473. if ((num_wraps - priv->event_log.num_wraps) > 1)
  474. priv->event_log.wraps_more_count++;
  475. else
  476. priv->event_log.wraps_once_count++;
  477. trace_iwlwifi_legacy_dev_ucode_wrap_event(priv,
  478. num_wraps - priv->event_log.num_wraps,
  479. next_entry, priv->event_log.next_entry);
  480. if (next_entry < priv->event_log.next_entry) {
  481. iwl4965_print_cont_event_trace(priv, base,
  482. priv->event_log.next_entry,
  483. capacity - priv->event_log.next_entry,
  484. mode);
  485. iwl4965_print_cont_event_trace(priv, base, 0,
  486. next_entry, mode);
  487. } else {
  488. iwl4965_print_cont_event_trace(priv, base,
  489. next_entry, capacity - next_entry,
  490. mode);
  491. iwl4965_print_cont_event_trace(priv, base, 0,
  492. next_entry, mode);
  493. }
  494. }
  495. priv->event_log.num_wraps = num_wraps;
  496. priv->event_log.next_entry = next_entry;
  497. }
  498. /**
  499. * iwl4965_bg_ucode_trace - Timer callback to log ucode event
  500. *
  501. * The timer is continually set to execute every
  502. * UCODE_TRACE_PERIOD milliseconds after the last timer expired
  503. * this function is to perform continuous uCode event logging operation
  504. * if enabled
  505. */
  506. static void iwl4965_bg_ucode_trace(unsigned long data)
  507. {
  508. struct iwl_priv *priv = (struct iwl_priv *)data;
  509. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  510. return;
  511. if (priv->event_log.ucode_trace) {
  512. iwl4965_continuous_event_trace(priv);
  513. /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
  514. mod_timer(&priv->ucode_trace,
  515. jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
  516. }
  517. }
  518. static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
  519. struct iwl_rx_mem_buffer *rxb)
  520. {
  521. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  522. struct iwl4965_beacon_notif *beacon =
  523. (struct iwl4965_beacon_notif *)pkt->u.raw;
  524. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  525. u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  526. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  527. "tsf %d %d rate %d\n",
  528. le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
  529. beacon->beacon_notify_hdr.failure_frame,
  530. le32_to_cpu(beacon->ibss_mgr_status),
  531. le32_to_cpu(beacon->high_tsf),
  532. le32_to_cpu(beacon->low_tsf), rate);
  533. #endif
  534. priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  535. }
  536. static void iwl4965_perform_ct_kill_task(struct iwl_priv *priv)
  537. {
  538. unsigned long flags;
  539. IWL_DEBUG_POWER(priv, "Stop all queues\n");
  540. if (priv->mac80211_registered)
  541. ieee80211_stop_queues(priv->hw);
  542. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  543. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  544. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  545. spin_lock_irqsave(&priv->reg_lock, flags);
  546. if (!iwl_grab_nic_access(priv))
  547. iwl_release_nic_access(priv);
  548. spin_unlock_irqrestore(&priv->reg_lock, flags);
  549. }
  550. /* Handle notification from uCode that card's power state is changing
  551. * due to software, hardware, or critical temperature RFKILL */
  552. static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
  553. struct iwl_rx_mem_buffer *rxb)
  554. {
  555. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  556. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  557. unsigned long status = priv->status;
  558. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
  559. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  560. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  561. (flags & CT_CARD_DISABLED) ?
  562. "Reached" : "Not reached");
  563. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
  564. CT_CARD_DISABLED)) {
  565. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  566. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  567. iwl_legacy_write_direct32(priv, HBUS_TARG_MBX_C,
  568. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  569. if (!(flags & RXON_CARD_DISABLED)) {
  570. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  571. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  572. iwl_legacy_write_direct32(priv, HBUS_TARG_MBX_C,
  573. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  574. }
  575. }
  576. if (flags & CT_CARD_DISABLED)
  577. iwl4965_perform_ct_kill_task(priv);
  578. if (flags & HW_CARD_DISABLED)
  579. set_bit(STATUS_RF_KILL_HW, &priv->status);
  580. else
  581. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  582. if (!(flags & RXON_CARD_DISABLED))
  583. iwl_legacy_scan_cancel(priv);
  584. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  585. test_bit(STATUS_RF_KILL_HW, &priv->status)))
  586. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  587. test_bit(STATUS_RF_KILL_HW, &priv->status));
  588. else
  589. wake_up_interruptible(&priv->wait_command_queue);
  590. }
  591. /**
  592. * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
  593. *
  594. * Setup the RX handlers for each of the reply types sent from the uCode
  595. * to the host.
  596. *
  597. * This function chains into the hardware specific files for them to setup
  598. * any hardware specific handlers as well.
  599. */
  600. static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
  601. {
  602. priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
  603. priv->rx_handlers[REPLY_ERROR] = iwl_legacy_rx_reply_error;
  604. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_legacy_rx_csa;
  605. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  606. iwl_legacy_rx_spectrum_measure_notif;
  607. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_legacy_rx_pm_sleep_notif;
  608. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  609. iwl_legacy_rx_pm_debug_statistics_notif;
  610. priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
  611. /*
  612. * The same handler is used for both the REPLY to a discrete
  613. * statistics request from the host as well as for the periodic
  614. * statistics notifications (after received beacons) from the uCode.
  615. */
  616. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_reply_statistics;
  617. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_rx_statistics;
  618. iwl_legacy_setup_rx_scan_handlers(priv);
  619. /* status change handler */
  620. priv->rx_handlers[CARD_STATE_NOTIFICATION] =
  621. iwl4965_rx_card_state_notif;
  622. priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
  623. iwl4965_rx_missed_beacon_notif;
  624. /* Rx handlers */
  625. priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl4965_rx_reply_rx_phy;
  626. priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl4965_rx_reply_rx;
  627. /* block ack */
  628. priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl4965_rx_reply_compressed_ba;
  629. /* Set up hardware specific Rx handlers */
  630. priv->cfg->ops->lib->rx_handler_setup(priv);
  631. }
  632. /**
  633. * iwl4965_rx_handle - Main entry function for receiving responses from uCode
  634. *
  635. * Uses the priv->rx_handlers callback function array to invoke
  636. * the appropriate handlers, including command responses,
  637. * frame-received notifications, and other notifications.
  638. */
  639. void iwl4965_rx_handle(struct iwl_priv *priv)
  640. {
  641. struct iwl_rx_mem_buffer *rxb;
  642. struct iwl_rx_packet *pkt;
  643. struct iwl_rx_queue *rxq = &priv->rxq;
  644. u32 r, i;
  645. int reclaim;
  646. unsigned long flags;
  647. u8 fill_rx = 0;
  648. u32 count = 8;
  649. int total_empty;
  650. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  651. * buffer that the driver may process (last buffer filled by ucode). */
  652. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  653. i = rxq->read;
  654. /* Rx interrupt, but nothing sent from uCode */
  655. if (i == r)
  656. IWL_DEBUG_RX(priv, "r = %d, i = %d\n", r, i);
  657. /* calculate total frames need to be restock after handling RX */
  658. total_empty = r - rxq->write_actual;
  659. if (total_empty < 0)
  660. total_empty += RX_QUEUE_SIZE;
  661. if (total_empty > (RX_QUEUE_SIZE / 2))
  662. fill_rx = 1;
  663. while (i != r) {
  664. int len;
  665. rxb = rxq->queue[i];
  666. /* If an RXB doesn't have a Rx queue slot associated with it,
  667. * then a bug has been introduced in the queue refilling
  668. * routines -- catch it here */
  669. BUG_ON(rxb == NULL);
  670. rxq->queue[i] = NULL;
  671. pci_unmap_page(priv->pci_dev, rxb->page_dma,
  672. PAGE_SIZE << priv->hw_params.rx_page_order,
  673. PCI_DMA_FROMDEVICE);
  674. pkt = rxb_addr(rxb);
  675. len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  676. len += sizeof(u32); /* account for status word */
  677. trace_iwlwifi_legacy_dev_rx(priv, pkt, len);
  678. /* Reclaim a command buffer only if this packet is a response
  679. * to a (driver-originated) command.
  680. * If the packet (e.g. Rx frame) originated from uCode,
  681. * there is no command buffer to reclaim.
  682. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  683. * but apparently a few don't get set; catch them here. */
  684. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  685. (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
  686. (pkt->hdr.cmd != REPLY_RX) &&
  687. (pkt->hdr.cmd != REPLY_RX_MPDU_CMD) &&
  688. (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
  689. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  690. (pkt->hdr.cmd != REPLY_TX);
  691. /* Based on type of command response or notification,
  692. * handle those that need handling via function in
  693. * rx_handlers table. See iwl4965_setup_rx_handlers() */
  694. if (priv->rx_handlers[pkt->hdr.cmd]) {
  695. IWL_DEBUG_RX(priv, "r = %d, i = %d, %s, 0x%02x\n", r,
  696. i, iwl_legacy_get_cmd_string(pkt->hdr.cmd),
  697. pkt->hdr.cmd);
  698. priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
  699. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  700. } else {
  701. /* No handling needed */
  702. IWL_DEBUG_RX(priv,
  703. "r %d i %d No handler needed for %s, 0x%02x\n",
  704. r, i, iwl_legacy_get_cmd_string(pkt->hdr.cmd),
  705. pkt->hdr.cmd);
  706. }
  707. /*
  708. * XXX: After here, we should always check rxb->page
  709. * against NULL before touching it or its virtual
  710. * memory (pkt). Because some rx_handler might have
  711. * already taken or freed the pages.
  712. */
  713. if (reclaim) {
  714. /* Invoke any callbacks, transfer the buffer to caller,
  715. * and fire off the (possibly) blocking iwl_legacy_send_cmd()
  716. * as we reclaim the driver command queue */
  717. if (rxb->page)
  718. iwl_legacy_tx_cmd_complete(priv, rxb);
  719. else
  720. IWL_WARN(priv, "Claim null rxb?\n");
  721. }
  722. /* Reuse the page if possible. For notification packets and
  723. * SKBs that fail to Rx correctly, add them back into the
  724. * rx_free list for reuse later. */
  725. spin_lock_irqsave(&rxq->lock, flags);
  726. if (rxb->page != NULL) {
  727. rxb->page_dma = pci_map_page(priv->pci_dev, rxb->page,
  728. 0, PAGE_SIZE << priv->hw_params.rx_page_order,
  729. PCI_DMA_FROMDEVICE);
  730. list_add_tail(&rxb->list, &rxq->rx_free);
  731. rxq->free_count++;
  732. } else
  733. list_add_tail(&rxb->list, &rxq->rx_used);
  734. spin_unlock_irqrestore(&rxq->lock, flags);
  735. i = (i + 1) & RX_QUEUE_MASK;
  736. /* If there are a lot of unused frames,
  737. * restock the Rx queue so ucode wont assert. */
  738. if (fill_rx) {
  739. count++;
  740. if (count >= 8) {
  741. rxq->read = i;
  742. iwl4965_rx_replenish_now(priv);
  743. count = 0;
  744. }
  745. }
  746. }
  747. /* Backtrack one entry */
  748. rxq->read = i;
  749. if (fill_rx)
  750. iwl4965_rx_replenish_now(priv);
  751. else
  752. iwl4965_rx_queue_restock(priv);
  753. }
  754. /* call this function to flush any scheduled tasklet */
  755. static inline void iwl4965_synchronize_irq(struct iwl_priv *priv)
  756. {
  757. /* wait to make sure we flush pending tasklet*/
  758. synchronize_irq(priv->pci_dev->irq);
  759. tasklet_kill(&priv->irq_tasklet);
  760. }
  761. static void iwl4965_irq_tasklet(struct iwl_priv *priv)
  762. {
  763. u32 inta, handled = 0;
  764. u32 inta_fh;
  765. unsigned long flags;
  766. u32 i;
  767. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  768. u32 inta_mask;
  769. #endif
  770. spin_lock_irqsave(&priv->lock, flags);
  771. /* Ack/clear/reset pending uCode interrupts.
  772. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  773. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  774. inta = iwl_read32(priv, CSR_INT);
  775. iwl_write32(priv, CSR_INT, inta);
  776. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  777. * Any new interrupts that happen after this, either while we're
  778. * in this tasklet, or later, will show up in next ISR/tasklet. */
  779. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  780. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  781. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  782. if (iwl_legacy_get_debug_level(priv) & IWL_DL_ISR) {
  783. /* just for debug */
  784. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  785. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  786. inta, inta_mask, inta_fh);
  787. }
  788. #endif
  789. spin_unlock_irqrestore(&priv->lock, flags);
  790. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  791. * atomic, make sure that inta covers all the interrupts that
  792. * we've discovered, even if FH interrupt came in just after
  793. * reading CSR_INT. */
  794. if (inta_fh & CSR49_FH_INT_RX_MASK)
  795. inta |= CSR_INT_BIT_FH_RX;
  796. if (inta_fh & CSR49_FH_INT_TX_MASK)
  797. inta |= CSR_INT_BIT_FH_TX;
  798. /* Now service all interrupt bits discovered above. */
  799. if (inta & CSR_INT_BIT_HW_ERR) {
  800. IWL_ERR(priv, "Hardware error detected. Restarting.\n");
  801. /* Tell the device to stop sending interrupts */
  802. iwl_legacy_disable_interrupts(priv);
  803. priv->isr_stats.hw++;
  804. iwl_legacy_irq_handle_error(priv);
  805. handled |= CSR_INT_BIT_HW_ERR;
  806. return;
  807. }
  808. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  809. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  810. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  811. if (inta & CSR_INT_BIT_SCD) {
  812. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  813. "the frame/frames.\n");
  814. priv->isr_stats.sch++;
  815. }
  816. /* Alive notification via Rx interrupt will do the real work */
  817. if (inta & CSR_INT_BIT_ALIVE) {
  818. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  819. priv->isr_stats.alive++;
  820. }
  821. }
  822. #endif
  823. /* Safely ignore these bits for debug checks below */
  824. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  825. /* HW RF KILL switch toggled */
  826. if (inta & CSR_INT_BIT_RF_KILL) {
  827. int hw_rf_kill = 0;
  828. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  829. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  830. hw_rf_kill = 1;
  831. IWL_WARN(priv, "RF_KILL bit toggled to %s.\n",
  832. hw_rf_kill ? "disable radio" : "enable radio");
  833. priv->isr_stats.rfkill++;
  834. /* driver only loads ucode once setting the interface up.
  835. * the driver allows loading the ucode even if the radio
  836. * is killed. Hence update the killswitch state here. The
  837. * rfkill handler will care about restarting if needed.
  838. */
  839. if (!test_bit(STATUS_ALIVE, &priv->status)) {
  840. if (hw_rf_kill)
  841. set_bit(STATUS_RF_KILL_HW, &priv->status);
  842. else
  843. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  844. wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rf_kill);
  845. }
  846. handled |= CSR_INT_BIT_RF_KILL;
  847. }
  848. /* Chip got too hot and stopped itself */
  849. if (inta & CSR_INT_BIT_CT_KILL) {
  850. IWL_ERR(priv, "Microcode CT kill error detected.\n");
  851. priv->isr_stats.ctkill++;
  852. handled |= CSR_INT_BIT_CT_KILL;
  853. }
  854. /* Error detected by uCode */
  855. if (inta & CSR_INT_BIT_SW_ERR) {
  856. IWL_ERR(priv, "Microcode SW error detected. "
  857. " Restarting 0x%X.\n", inta);
  858. priv->isr_stats.sw++;
  859. iwl_legacy_irq_handle_error(priv);
  860. handled |= CSR_INT_BIT_SW_ERR;
  861. }
  862. /*
  863. * uCode wakes up after power-down sleep.
  864. * Tell device about any new tx or host commands enqueued,
  865. * and about any Rx buffers made available while asleep.
  866. */
  867. if (inta & CSR_INT_BIT_WAKEUP) {
  868. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  869. iwl_legacy_rx_queue_update_write_ptr(priv, &priv->rxq);
  870. for (i = 0; i < priv->hw_params.max_txq_num; i++)
  871. iwl_legacy_txq_update_write_ptr(priv, &priv->txq[i]);
  872. priv->isr_stats.wakeup++;
  873. handled |= CSR_INT_BIT_WAKEUP;
  874. }
  875. /* All uCode command responses, including Tx command responses,
  876. * Rx "responses" (frame-received notification), and other
  877. * notifications from uCode come through here*/
  878. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  879. iwl4965_rx_handle(priv);
  880. priv->isr_stats.rx++;
  881. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  882. }
  883. /* This "Tx" DMA channel is used only for loading uCode */
  884. if (inta & CSR_INT_BIT_FH_TX) {
  885. IWL_DEBUG_ISR(priv, "uCode load interrupt\n");
  886. priv->isr_stats.tx++;
  887. handled |= CSR_INT_BIT_FH_TX;
  888. /* Wake up uCode load routine, now that load is complete */
  889. priv->ucode_write_complete = 1;
  890. wake_up_interruptible(&priv->wait_command_queue);
  891. }
  892. if (inta & ~handled) {
  893. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  894. priv->isr_stats.unhandled++;
  895. }
  896. if (inta & ~(priv->inta_mask)) {
  897. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  898. inta & ~priv->inta_mask);
  899. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  900. }
  901. /* Re-enable all interrupts */
  902. /* only Re-enable if disabled by irq */
  903. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  904. iwl_legacy_enable_interrupts(priv);
  905. /* Re-enable RF_KILL if it occurred */
  906. else if (handled & CSR_INT_BIT_RF_KILL)
  907. iwl_legacy_enable_rfkill_int(priv);
  908. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  909. if (iwl_legacy_get_debug_level(priv) & (IWL_DL_ISR)) {
  910. inta = iwl_read32(priv, CSR_INT);
  911. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  912. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  913. IWL_DEBUG_ISR(priv,
  914. "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  915. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  916. }
  917. #endif
  918. }
  919. /*****************************************************************************
  920. *
  921. * sysfs attributes
  922. *
  923. *****************************************************************************/
  924. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  925. /*
  926. * The following adds a new attribute to the sysfs representation
  927. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  928. * used for controlling the debug level.
  929. *
  930. * See the level definitions in iwl for details.
  931. *
  932. * The debug_level being managed using sysfs below is a per device debug
  933. * level that is used instead of the global debug level if it (the per
  934. * device debug level) is set.
  935. */
  936. static ssize_t iwl4965_show_debug_level(struct device *d,
  937. struct device_attribute *attr, char *buf)
  938. {
  939. struct iwl_priv *priv = dev_get_drvdata(d);
  940. return sprintf(buf, "0x%08X\n", iwl_legacy_get_debug_level(priv));
  941. }
  942. static ssize_t iwl4965_store_debug_level(struct device *d,
  943. struct device_attribute *attr,
  944. const char *buf, size_t count)
  945. {
  946. struct iwl_priv *priv = dev_get_drvdata(d);
  947. unsigned long val;
  948. int ret;
  949. ret = strict_strtoul(buf, 0, &val);
  950. if (ret)
  951. IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
  952. else {
  953. priv->debug_level = val;
  954. if (iwl_legacy_alloc_traffic_mem(priv))
  955. IWL_ERR(priv,
  956. "Not enough memory to generate traffic log\n");
  957. }
  958. return strnlen(buf, count);
  959. }
  960. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  961. iwl4965_show_debug_level, iwl4965_store_debug_level);
  962. #endif /* CONFIG_IWLWIFI_LEGACY_DEBUG */
  963. static ssize_t iwl4965_show_temperature(struct device *d,
  964. struct device_attribute *attr, char *buf)
  965. {
  966. struct iwl_priv *priv = dev_get_drvdata(d);
  967. if (!iwl_legacy_is_alive(priv))
  968. return -EAGAIN;
  969. return sprintf(buf, "%d\n", priv->temperature);
  970. }
  971. static DEVICE_ATTR(temperature, S_IRUGO, iwl4965_show_temperature, NULL);
  972. static ssize_t iwl4965_show_tx_power(struct device *d,
  973. struct device_attribute *attr, char *buf)
  974. {
  975. struct iwl_priv *priv = dev_get_drvdata(d);
  976. if (!iwl_legacy_is_ready_rf(priv))
  977. return sprintf(buf, "off\n");
  978. else
  979. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  980. }
  981. static ssize_t iwl4965_store_tx_power(struct device *d,
  982. struct device_attribute *attr,
  983. const char *buf, size_t count)
  984. {
  985. struct iwl_priv *priv = dev_get_drvdata(d);
  986. unsigned long val;
  987. int ret;
  988. ret = strict_strtoul(buf, 10, &val);
  989. if (ret)
  990. IWL_INFO(priv, "%s is not in decimal form.\n", buf);
  991. else {
  992. ret = iwl_legacy_set_tx_power(priv, val, false);
  993. if (ret)
  994. IWL_ERR(priv, "failed setting tx power (0x%d).\n",
  995. ret);
  996. else
  997. ret = count;
  998. }
  999. return ret;
  1000. }
  1001. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO,
  1002. iwl4965_show_tx_power, iwl4965_store_tx_power);
  1003. static struct attribute *iwl_sysfs_entries[] = {
  1004. &dev_attr_temperature.attr,
  1005. &dev_attr_tx_power.attr,
  1006. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1007. &dev_attr_debug_level.attr,
  1008. #endif
  1009. NULL
  1010. };
  1011. static struct attribute_group iwl_attribute_group = {
  1012. .name = NULL, /* put in device directory */
  1013. .attrs = iwl_sysfs_entries,
  1014. };
  1015. /******************************************************************************
  1016. *
  1017. * uCode download functions
  1018. *
  1019. ******************************************************************************/
  1020. static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
  1021. {
  1022. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  1023. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  1024. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1025. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  1026. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1027. iwl_legacy_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1028. }
  1029. static void iwl4965_nic_start(struct iwl_priv *priv)
  1030. {
  1031. /* Remove all resets to allow NIC to operate */
  1032. iwl_write32(priv, CSR_RESET, 0);
  1033. }
  1034. static void iwl4965_ucode_callback(const struct firmware *ucode_raw,
  1035. void *context);
  1036. static int iwl4965_mac_setup_register(struct iwl_priv *priv,
  1037. u32 max_probe_length);
  1038. static int __must_check iwl4965_request_firmware(struct iwl_priv *priv, bool first)
  1039. {
  1040. const char *name_pre = priv->cfg->fw_name_pre;
  1041. char tag[8];
  1042. if (first) {
  1043. priv->fw_index = priv->cfg->ucode_api_max;
  1044. sprintf(tag, "%d", priv->fw_index);
  1045. } else {
  1046. priv->fw_index--;
  1047. sprintf(tag, "%d", priv->fw_index);
  1048. }
  1049. if (priv->fw_index < priv->cfg->ucode_api_min) {
  1050. IWL_ERR(priv, "no suitable firmware found!\n");
  1051. return -ENOENT;
  1052. }
  1053. sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  1054. IWL_DEBUG_INFO(priv, "attempting to load firmware '%s'\n",
  1055. priv->firmware_name);
  1056. return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
  1057. &priv->pci_dev->dev, GFP_KERNEL, priv,
  1058. iwl4965_ucode_callback);
  1059. }
  1060. struct iwl4965_firmware_pieces {
  1061. const void *inst, *data, *init, *init_data, *boot;
  1062. size_t inst_size, data_size, init_size, init_data_size, boot_size;
  1063. };
  1064. static int iwl4965_load_firmware(struct iwl_priv *priv,
  1065. const struct firmware *ucode_raw,
  1066. struct iwl4965_firmware_pieces *pieces)
  1067. {
  1068. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  1069. u32 api_ver, hdr_size;
  1070. const u8 *src;
  1071. priv->ucode_ver = le32_to_cpu(ucode->ver);
  1072. api_ver = IWL_UCODE_API(priv->ucode_ver);
  1073. switch (api_ver) {
  1074. default:
  1075. case 0:
  1076. case 1:
  1077. case 2:
  1078. hdr_size = 24;
  1079. if (ucode_raw->size < hdr_size) {
  1080. IWL_ERR(priv, "File size too small!\n");
  1081. return -EINVAL;
  1082. }
  1083. pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
  1084. pieces->data_size = le32_to_cpu(ucode->v1.data_size);
  1085. pieces->init_size = le32_to_cpu(ucode->v1.init_size);
  1086. pieces->init_data_size =
  1087. le32_to_cpu(ucode->v1.init_data_size);
  1088. pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
  1089. src = ucode->v1.data;
  1090. break;
  1091. }
  1092. /* Verify size of file vs. image size info in file's header */
  1093. if (ucode_raw->size != hdr_size + pieces->inst_size +
  1094. pieces->data_size + pieces->init_size +
  1095. pieces->init_data_size + pieces->boot_size) {
  1096. IWL_ERR(priv,
  1097. "uCode file size %d does not match expected size\n",
  1098. (int)ucode_raw->size);
  1099. return -EINVAL;
  1100. }
  1101. pieces->inst = src;
  1102. src += pieces->inst_size;
  1103. pieces->data = src;
  1104. src += pieces->data_size;
  1105. pieces->init = src;
  1106. src += pieces->init_size;
  1107. pieces->init_data = src;
  1108. src += pieces->init_data_size;
  1109. pieces->boot = src;
  1110. src += pieces->boot_size;
  1111. return 0;
  1112. }
  1113. /**
  1114. * iwl4965_ucode_callback - callback when firmware was loaded
  1115. *
  1116. * If loaded successfully, copies the firmware into buffers
  1117. * for the card to fetch (via DMA).
  1118. */
  1119. static void
  1120. iwl4965_ucode_callback(const struct firmware *ucode_raw, void *context)
  1121. {
  1122. struct iwl_priv *priv = context;
  1123. struct iwl_ucode_header *ucode;
  1124. int err;
  1125. struct iwl4965_firmware_pieces pieces;
  1126. const unsigned int api_max = priv->cfg->ucode_api_max;
  1127. const unsigned int api_min = priv->cfg->ucode_api_min;
  1128. u32 api_ver;
  1129. u32 max_probe_length = 200;
  1130. u32 standard_phy_calibration_size =
  1131. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1132. memset(&pieces, 0, sizeof(pieces));
  1133. if (!ucode_raw) {
  1134. if (priv->fw_index <= priv->cfg->ucode_api_max)
  1135. IWL_ERR(priv,
  1136. "request for firmware file '%s' failed.\n",
  1137. priv->firmware_name);
  1138. goto try_again;
  1139. }
  1140. IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
  1141. priv->firmware_name, ucode_raw->size);
  1142. /* Make sure that we got at least the API version number */
  1143. if (ucode_raw->size < 4) {
  1144. IWL_ERR(priv, "File size way too small!\n");
  1145. goto try_again;
  1146. }
  1147. /* Data from ucode file: header followed by uCode images */
  1148. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  1149. err = iwl4965_load_firmware(priv, ucode_raw, &pieces);
  1150. if (err)
  1151. goto try_again;
  1152. api_ver = IWL_UCODE_API(priv->ucode_ver);
  1153. /*
  1154. * api_ver should match the api version forming part of the
  1155. * firmware filename ... but we don't check for that and only rely
  1156. * on the API version read from firmware header from here on forward
  1157. */
  1158. if (api_ver < api_min || api_ver > api_max) {
  1159. IWL_ERR(priv,
  1160. "Driver unable to support your firmware API. "
  1161. "Driver supports v%u, firmware is v%u.\n",
  1162. api_max, api_ver);
  1163. goto try_again;
  1164. }
  1165. if (api_ver != api_max)
  1166. IWL_ERR(priv,
  1167. "Firmware has old API version. Expected v%u, "
  1168. "got v%u. New firmware can be obtained "
  1169. "from http://www.intellinuxwireless.org.\n",
  1170. api_max, api_ver);
  1171. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  1172. IWL_UCODE_MAJOR(priv->ucode_ver),
  1173. IWL_UCODE_MINOR(priv->ucode_ver),
  1174. IWL_UCODE_API(priv->ucode_ver),
  1175. IWL_UCODE_SERIAL(priv->ucode_ver));
  1176. snprintf(priv->hw->wiphy->fw_version,
  1177. sizeof(priv->hw->wiphy->fw_version),
  1178. "%u.%u.%u.%u",
  1179. IWL_UCODE_MAJOR(priv->ucode_ver),
  1180. IWL_UCODE_MINOR(priv->ucode_ver),
  1181. IWL_UCODE_API(priv->ucode_ver),
  1182. IWL_UCODE_SERIAL(priv->ucode_ver));
  1183. /*
  1184. * For any of the failures below (before allocating pci memory)
  1185. * we will try to load a version with a smaller API -- maybe the
  1186. * user just got a corrupted version of the latest API.
  1187. */
  1188. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  1189. priv->ucode_ver);
  1190. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
  1191. pieces.inst_size);
  1192. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
  1193. pieces.data_size);
  1194. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
  1195. pieces.init_size);
  1196. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
  1197. pieces.init_data_size);
  1198. IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %Zd\n",
  1199. pieces.boot_size);
  1200. /* Verify that uCode images will fit in card's SRAM */
  1201. if (pieces.inst_size > priv->hw_params.max_inst_size) {
  1202. IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
  1203. pieces.inst_size);
  1204. goto try_again;
  1205. }
  1206. if (pieces.data_size > priv->hw_params.max_data_size) {
  1207. IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
  1208. pieces.data_size);
  1209. goto try_again;
  1210. }
  1211. if (pieces.init_size > priv->hw_params.max_inst_size) {
  1212. IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
  1213. pieces.init_size);
  1214. goto try_again;
  1215. }
  1216. if (pieces.init_data_size > priv->hw_params.max_data_size) {
  1217. IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
  1218. pieces.init_data_size);
  1219. goto try_again;
  1220. }
  1221. if (pieces.boot_size > priv->hw_params.max_bsm_size) {
  1222. IWL_ERR(priv, "uCode boot instr len %Zd too large to fit in\n",
  1223. pieces.boot_size);
  1224. goto try_again;
  1225. }
  1226. /* Allocate ucode buffers for card's bus-master loading ... */
  1227. /* Runtime instructions and 2 copies of data:
  1228. * 1) unmodified from disk
  1229. * 2) backup cache for save/restore during power-downs */
  1230. priv->ucode_code.len = pieces.inst_size;
  1231. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  1232. priv->ucode_data.len = pieces.data_size;
  1233. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  1234. priv->ucode_data_backup.len = pieces.data_size;
  1235. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1236. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  1237. !priv->ucode_data_backup.v_addr)
  1238. goto err_pci_alloc;
  1239. /* Initialization instructions and data */
  1240. if (pieces.init_size && pieces.init_data_size) {
  1241. priv->ucode_init.len = pieces.init_size;
  1242. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  1243. priv->ucode_init_data.len = pieces.init_data_size;
  1244. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1245. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  1246. goto err_pci_alloc;
  1247. }
  1248. /* Bootstrap (instructions only, no data) */
  1249. if (pieces.boot_size) {
  1250. priv->ucode_boot.len = pieces.boot_size;
  1251. iwl_legacy_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1252. if (!priv->ucode_boot.v_addr)
  1253. goto err_pci_alloc;
  1254. }
  1255. /* Now that we can no longer fail, copy information */
  1256. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  1257. /* Copy images into buffers for card's bus-master reads ... */
  1258. /* Runtime instructions (first block of data in file) */
  1259. IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode instr len %Zd\n",
  1260. pieces.inst_size);
  1261. memcpy(priv->ucode_code.v_addr, pieces.inst, pieces.inst_size);
  1262. IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  1263. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  1264. /*
  1265. * Runtime data
  1266. * NOTE: Copy into backup buffer will be done in iwl_up()
  1267. */
  1268. IWL_DEBUG_INFO(priv, "Copying (but not loading) uCode data len %Zd\n",
  1269. pieces.data_size);
  1270. memcpy(priv->ucode_data.v_addr, pieces.data, pieces.data_size);
  1271. memcpy(priv->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
  1272. /* Initialization instructions */
  1273. if (pieces.init_size) {
  1274. IWL_DEBUG_INFO(priv,
  1275. "Copying (but not loading) init instr len %Zd\n",
  1276. pieces.init_size);
  1277. memcpy(priv->ucode_init.v_addr, pieces.init, pieces.init_size);
  1278. }
  1279. /* Initialization data */
  1280. if (pieces.init_data_size) {
  1281. IWL_DEBUG_INFO(priv,
  1282. "Copying (but not loading) init data len %Zd\n",
  1283. pieces.init_data_size);
  1284. memcpy(priv->ucode_init_data.v_addr, pieces.init_data,
  1285. pieces.init_data_size);
  1286. }
  1287. /* Bootstrap instructions */
  1288. IWL_DEBUG_INFO(priv, "Copying (but not loading) boot instr len %Zd\n",
  1289. pieces.boot_size);
  1290. memcpy(priv->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
  1291. /*
  1292. * figure out the offset of chain noise reset and gain commands
  1293. * base on the size of standard phy calibration commands table size
  1294. */
  1295. priv->_4965.phy_calib_chain_noise_reset_cmd =
  1296. standard_phy_calibration_size;
  1297. priv->_4965.phy_calib_chain_noise_gain_cmd =
  1298. standard_phy_calibration_size + 1;
  1299. /**************************************************
  1300. * This is still part of probe() in a sense...
  1301. *
  1302. * 9. Setup and register with mac80211 and debugfs
  1303. **************************************************/
  1304. err = iwl4965_mac_setup_register(priv, max_probe_length);
  1305. if (err)
  1306. goto out_unbind;
  1307. err = iwl_legacy_dbgfs_register(priv, DRV_NAME);
  1308. if (err)
  1309. IWL_ERR(priv,
  1310. "failed to create debugfs files. Ignoring error: %d\n", err);
  1311. err = sysfs_create_group(&priv->pci_dev->dev.kobj,
  1312. &iwl_attribute_group);
  1313. if (err) {
  1314. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  1315. goto out_unbind;
  1316. }
  1317. /* We have our copies now, allow OS release its copies */
  1318. release_firmware(ucode_raw);
  1319. complete(&priv->_4965.firmware_loading_complete);
  1320. return;
  1321. try_again:
  1322. /* try next, if any */
  1323. if (iwl4965_request_firmware(priv, false))
  1324. goto out_unbind;
  1325. release_firmware(ucode_raw);
  1326. return;
  1327. err_pci_alloc:
  1328. IWL_ERR(priv, "failed to allocate pci memory\n");
  1329. iwl4965_dealloc_ucode_pci(priv);
  1330. out_unbind:
  1331. complete(&priv->_4965.firmware_loading_complete);
  1332. device_release_driver(&priv->pci_dev->dev);
  1333. release_firmware(ucode_raw);
  1334. }
  1335. static const char * const desc_lookup_text[] = {
  1336. "OK",
  1337. "FAIL",
  1338. "BAD_PARAM",
  1339. "BAD_CHECKSUM",
  1340. "NMI_INTERRUPT_WDG",
  1341. "SYSASSERT",
  1342. "FATAL_ERROR",
  1343. "BAD_COMMAND",
  1344. "HW_ERROR_TUNE_LOCK",
  1345. "HW_ERROR_TEMPERATURE",
  1346. "ILLEGAL_CHAN_FREQ",
  1347. "VCC_NOT_STABLE",
  1348. "FH_ERROR",
  1349. "NMI_INTERRUPT_HOST",
  1350. "NMI_INTERRUPT_ACTION_PT",
  1351. "NMI_INTERRUPT_UNKNOWN",
  1352. "UCODE_VERSION_MISMATCH",
  1353. "HW_ERROR_ABS_LOCK",
  1354. "HW_ERROR_CAL_LOCK_FAIL",
  1355. "NMI_INTERRUPT_INST_ACTION_PT",
  1356. "NMI_INTERRUPT_DATA_ACTION_PT",
  1357. "NMI_TRM_HW_ER",
  1358. "NMI_INTERRUPT_TRM",
  1359. "NMI_INTERRUPT_BREAK_POINT"
  1360. "DEBUG_0",
  1361. "DEBUG_1",
  1362. "DEBUG_2",
  1363. "DEBUG_3",
  1364. };
  1365. static struct { char *name; u8 num; } advanced_lookup[] = {
  1366. { "NMI_INTERRUPT_WDG", 0x34 },
  1367. { "SYSASSERT", 0x35 },
  1368. { "UCODE_VERSION_MISMATCH", 0x37 },
  1369. { "BAD_COMMAND", 0x38 },
  1370. { "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C },
  1371. { "FATAL_ERROR", 0x3D },
  1372. { "NMI_TRM_HW_ERR", 0x46 },
  1373. { "NMI_INTERRUPT_TRM", 0x4C },
  1374. { "NMI_INTERRUPT_BREAK_POINT", 0x54 },
  1375. { "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C },
  1376. { "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64 },
  1377. { "NMI_INTERRUPT_HOST", 0x66 },
  1378. { "NMI_INTERRUPT_ACTION_PT", 0x7C },
  1379. { "NMI_INTERRUPT_UNKNOWN", 0x84 },
  1380. { "NMI_INTERRUPT_INST_ACTION_PT", 0x86 },
  1381. { "ADVANCED_SYSASSERT", 0 },
  1382. };
  1383. static const char *iwl4965_desc_lookup(u32 num)
  1384. {
  1385. int i;
  1386. int max = ARRAY_SIZE(desc_lookup_text);
  1387. if (num < max)
  1388. return desc_lookup_text[num];
  1389. max = ARRAY_SIZE(advanced_lookup) - 1;
  1390. for (i = 0; i < max; i++) {
  1391. if (advanced_lookup[i].num == num)
  1392. break;
  1393. }
  1394. return advanced_lookup[i].name;
  1395. }
  1396. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1397. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1398. void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
  1399. {
  1400. u32 data2, line;
  1401. u32 desc, time, count, base, data1;
  1402. u32 blink1, blink2, ilink1, ilink2;
  1403. u32 pc, hcmd;
  1404. if (priv->ucode_type == UCODE_INIT) {
  1405. base = le32_to_cpu(priv->card_alive_init.error_event_table_ptr);
  1406. } else {
  1407. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1408. }
  1409. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  1410. IWL_ERR(priv,
  1411. "Not valid error log pointer 0x%08X for %s uCode\n",
  1412. base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
  1413. return;
  1414. }
  1415. count = iwl_legacy_read_targ_mem(priv, base);
  1416. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1417. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1418. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1419. priv->status, count);
  1420. }
  1421. desc = iwl_legacy_read_targ_mem(priv, base + 1 * sizeof(u32));
  1422. priv->isr_stats.err_code = desc;
  1423. pc = iwl_legacy_read_targ_mem(priv, base + 2 * sizeof(u32));
  1424. blink1 = iwl_legacy_read_targ_mem(priv, base + 3 * sizeof(u32));
  1425. blink2 = iwl_legacy_read_targ_mem(priv, base + 4 * sizeof(u32));
  1426. ilink1 = iwl_legacy_read_targ_mem(priv, base + 5 * sizeof(u32));
  1427. ilink2 = iwl_legacy_read_targ_mem(priv, base + 6 * sizeof(u32));
  1428. data1 = iwl_legacy_read_targ_mem(priv, base + 7 * sizeof(u32));
  1429. data2 = iwl_legacy_read_targ_mem(priv, base + 8 * sizeof(u32));
  1430. line = iwl_legacy_read_targ_mem(priv, base + 9 * sizeof(u32));
  1431. time = iwl_legacy_read_targ_mem(priv, base + 11 * sizeof(u32));
  1432. hcmd = iwl_legacy_read_targ_mem(priv, base + 22 * sizeof(u32));
  1433. trace_iwlwifi_legacy_dev_ucode_error(priv, desc,
  1434. time, data1, data2, line,
  1435. blink1, blink2, ilink1, ilink2);
  1436. IWL_ERR(priv, "Desc Time "
  1437. "data1 data2 line\n");
  1438. IWL_ERR(priv, "%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
  1439. iwl4965_desc_lookup(desc), desc, time, data1, data2, line);
  1440. IWL_ERR(priv, "pc blink1 blink2 ilink1 ilink2 hcmd\n");
  1441. IWL_ERR(priv, "0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n",
  1442. pc, blink1, blink2, ilink1, ilink2, hcmd);
  1443. }
  1444. #define EVENT_START_OFFSET (4 * sizeof(u32))
  1445. /**
  1446. * iwl4965_print_event_log - Dump error event log to syslog
  1447. *
  1448. */
  1449. static int iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
  1450. u32 num_events, u32 mode,
  1451. int pos, char **buf, size_t bufsz)
  1452. {
  1453. u32 i;
  1454. u32 base; /* SRAM byte address of event log header */
  1455. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  1456. u32 ptr; /* SRAM byte address of log data */
  1457. u32 ev, time, data; /* event log data */
  1458. unsigned long reg_flags;
  1459. if (num_events == 0)
  1460. return pos;
  1461. if (priv->ucode_type == UCODE_INIT) {
  1462. base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
  1463. } else {
  1464. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1465. }
  1466. if (mode == 0)
  1467. event_size = 2 * sizeof(u32);
  1468. else
  1469. event_size = 3 * sizeof(u32);
  1470. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  1471. /* Make sure device is powered up for SRAM reads */
  1472. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  1473. iwl_grab_nic_access(priv);
  1474. /* Set starting address; reads will auto-increment */
  1475. _iwl_legacy_write_direct32(priv, HBUS_TARG_MEM_RADDR, ptr);
  1476. rmb();
  1477. /* "time" is actually "data" for mode 0 (no timestamp).
  1478. * place event id # at far right for easier visual parsing. */
  1479. for (i = 0; i < num_events; i++) {
  1480. ev = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1481. time = _iwl_legacy_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1482. if (mode == 0) {
  1483. /* data, ev */
  1484. if (bufsz) {
  1485. pos += scnprintf(*buf + pos, bufsz - pos,
  1486. "EVT_LOG:0x%08x:%04u\n",
  1487. time, ev);
  1488. } else {
  1489. trace_iwlwifi_legacy_dev_ucode_event(priv, 0,
  1490. time, ev);
  1491. IWL_ERR(priv, "EVT_LOG:0x%08x:%04u\n",
  1492. time, ev);
  1493. }
  1494. } else {
  1495. data = _iwl_legacy_read_direct32(priv,
  1496. HBUS_TARG_MEM_RDAT);
  1497. if (bufsz) {
  1498. pos += scnprintf(*buf + pos, bufsz - pos,
  1499. "EVT_LOGT:%010u:0x%08x:%04u\n",
  1500. time, data, ev);
  1501. } else {
  1502. IWL_ERR(priv, "EVT_LOGT:%010u:0x%08x:%04u\n",
  1503. time, data, ev);
  1504. trace_iwlwifi_legacy_dev_ucode_event(priv, time,
  1505. data, ev);
  1506. }
  1507. }
  1508. }
  1509. /* Allow device to power down */
  1510. iwl_release_nic_access(priv);
  1511. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  1512. return pos;
  1513. }
  1514. /**
  1515. * iwl4965_print_last_event_logs - Dump the newest # of event log to syslog
  1516. */
  1517. static int iwl4965_print_last_event_logs(struct iwl_priv *priv, u32 capacity,
  1518. u32 num_wraps, u32 next_entry,
  1519. u32 size, u32 mode,
  1520. int pos, char **buf, size_t bufsz)
  1521. {
  1522. /*
  1523. * display the newest DEFAULT_LOG_ENTRIES entries
  1524. * i.e the entries just before the next ont that uCode would fill.
  1525. */
  1526. if (num_wraps) {
  1527. if (next_entry < size) {
  1528. pos = iwl4965_print_event_log(priv,
  1529. capacity - (size - next_entry),
  1530. size - next_entry, mode,
  1531. pos, buf, bufsz);
  1532. pos = iwl4965_print_event_log(priv, 0,
  1533. next_entry, mode,
  1534. pos, buf, bufsz);
  1535. } else
  1536. pos = iwl4965_print_event_log(priv, next_entry - size,
  1537. size, mode, pos, buf, bufsz);
  1538. } else {
  1539. if (next_entry < size) {
  1540. pos = iwl4965_print_event_log(priv, 0, next_entry,
  1541. mode, pos, buf, bufsz);
  1542. } else {
  1543. pos = iwl4965_print_event_log(priv, next_entry - size,
  1544. size, mode, pos, buf, bufsz);
  1545. }
  1546. }
  1547. return pos;
  1548. }
  1549. #define DEFAULT_DUMP_EVENT_LOG_ENTRIES (20)
  1550. int iwl4965_dump_nic_event_log(struct iwl_priv *priv, bool full_log,
  1551. char **buf, bool display)
  1552. {
  1553. u32 base; /* SRAM byte address of event log header */
  1554. u32 capacity; /* event log capacity in # entries */
  1555. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  1556. u32 num_wraps; /* # times uCode wrapped to top of log */
  1557. u32 next_entry; /* index of next entry to be written by uCode */
  1558. u32 size; /* # entries that we'll print */
  1559. int pos = 0;
  1560. size_t bufsz = 0;
  1561. if (priv->ucode_type == UCODE_INIT) {
  1562. base = le32_to_cpu(priv->card_alive_init.log_event_table_ptr);
  1563. } else {
  1564. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1565. }
  1566. if (!priv->cfg->ops->lib->is_valid_rtc_data_addr(base)) {
  1567. IWL_ERR(priv,
  1568. "Invalid event log pointer 0x%08X for %s uCode\n",
  1569. base, (priv->ucode_type == UCODE_INIT) ? "Init" : "RT");
  1570. return -EINVAL;
  1571. }
  1572. /* event log header */
  1573. capacity = iwl_legacy_read_targ_mem(priv, base);
  1574. mode = iwl_legacy_read_targ_mem(priv, base + (1 * sizeof(u32)));
  1575. num_wraps = iwl_legacy_read_targ_mem(priv, base + (2 * sizeof(u32)));
  1576. next_entry = iwl_legacy_read_targ_mem(priv, base + (3 * sizeof(u32)));
  1577. size = num_wraps ? capacity : next_entry;
  1578. /* bail out if nothing in log */
  1579. if (size == 0) {
  1580. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  1581. return pos;
  1582. }
  1583. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1584. if (!(iwl_legacy_get_debug_level(priv) & IWL_DL_FW_ERRORS) && !full_log)
  1585. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  1586. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  1587. #else
  1588. size = (size > DEFAULT_DUMP_EVENT_LOG_ENTRIES)
  1589. ? DEFAULT_DUMP_EVENT_LOG_ENTRIES : size;
  1590. #endif
  1591. IWL_ERR(priv, "Start IWL Event Log Dump: display last %u entries\n",
  1592. size);
  1593. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  1594. if (display) {
  1595. if (full_log)
  1596. bufsz = capacity * 48;
  1597. else
  1598. bufsz = size * 48;
  1599. *buf = kmalloc(bufsz, GFP_KERNEL);
  1600. if (!*buf)
  1601. return -ENOMEM;
  1602. }
  1603. if ((iwl_legacy_get_debug_level(priv) & IWL_DL_FW_ERRORS) || full_log) {
  1604. /*
  1605. * if uCode has wrapped back to top of log,
  1606. * start at the oldest entry,
  1607. * i.e the next one that uCode would fill.
  1608. */
  1609. if (num_wraps)
  1610. pos = iwl4965_print_event_log(priv, next_entry,
  1611. capacity - next_entry, mode,
  1612. pos, buf, bufsz);
  1613. /* (then/else) start at top of log */
  1614. pos = iwl4965_print_event_log(priv, 0,
  1615. next_entry, mode, pos, buf, bufsz);
  1616. } else
  1617. pos = iwl4965_print_last_event_logs(priv, capacity, num_wraps,
  1618. next_entry, size, mode,
  1619. pos, buf, bufsz);
  1620. #else
  1621. pos = iwl4965_print_last_event_logs(priv, capacity, num_wraps,
  1622. next_entry, size, mode,
  1623. pos, buf, bufsz);
  1624. #endif
  1625. return pos;
  1626. }
  1627. static void iwl4965_rf_kill_ct_config(struct iwl_priv *priv)
  1628. {
  1629. struct iwl_ct_kill_config cmd;
  1630. unsigned long flags;
  1631. int ret = 0;
  1632. spin_lock_irqsave(&priv->lock, flags);
  1633. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  1634. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  1635. spin_unlock_irqrestore(&priv->lock, flags);
  1636. cmd.critical_temperature_R =
  1637. cpu_to_le32(priv->hw_params.ct_kill_threshold);
  1638. ret = iwl_legacy_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
  1639. sizeof(cmd), &cmd);
  1640. if (ret)
  1641. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1642. else
  1643. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1644. "succeeded, "
  1645. "critical temperature is %d\n",
  1646. priv->hw_params.ct_kill_threshold);
  1647. }
  1648. static const s8 default_queue_to_tx_fifo[] = {
  1649. IWL_TX_FIFO_VO,
  1650. IWL_TX_FIFO_VI,
  1651. IWL_TX_FIFO_BE,
  1652. IWL_TX_FIFO_BK,
  1653. IWL49_CMD_FIFO_NUM,
  1654. IWL_TX_FIFO_UNUSED,
  1655. IWL_TX_FIFO_UNUSED,
  1656. };
  1657. static int iwl4965_alive_notify(struct iwl_priv *priv)
  1658. {
  1659. u32 a;
  1660. unsigned long flags;
  1661. int i, chan;
  1662. u32 reg_val;
  1663. spin_lock_irqsave(&priv->lock, flags);
  1664. /* Clear 4965's internal Tx Scheduler data base */
  1665. priv->scd_base_addr = iwl_legacy_read_prph(priv,
  1666. IWL49_SCD_SRAM_BASE_ADDR);
  1667. a = priv->scd_base_addr + IWL49_SCD_CONTEXT_DATA_OFFSET;
  1668. for (; a < priv->scd_base_addr + IWL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  1669. iwl_legacy_write_targ_mem(priv, a, 0);
  1670. for (; a < priv->scd_base_addr + IWL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
  1671. iwl_legacy_write_targ_mem(priv, a, 0);
  1672. for (; a < priv->scd_base_addr +
  1673. IWL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(priv->hw_params.max_txq_num); a += 4)
  1674. iwl_legacy_write_targ_mem(priv, a, 0);
  1675. /* Tel 4965 where to find Tx byte count tables */
  1676. iwl_legacy_write_prph(priv, IWL49_SCD_DRAM_BASE_ADDR,
  1677. priv->scd_bc_tbls.dma >> 10);
  1678. /* Enable DMA channel */
  1679. for (chan = 0; chan < FH49_TCSR_CHNL_NUM ; chan++)
  1680. iwl_legacy_write_direct32(priv,
  1681. FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  1682. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  1683. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  1684. /* Update FH chicken bits */
  1685. reg_val = iwl_legacy_read_direct32(priv, FH_TX_CHICKEN_BITS_REG);
  1686. iwl_legacy_write_direct32(priv, FH_TX_CHICKEN_BITS_REG,
  1687. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  1688. /* Disable chain mode for all queues */
  1689. iwl_legacy_write_prph(priv, IWL49_SCD_QUEUECHAIN_SEL, 0);
  1690. /* Initialize each Tx queue (including the command queue) */
  1691. for (i = 0; i < priv->hw_params.max_txq_num; i++) {
  1692. /* TFD circular buffer read/write indexes */
  1693. iwl_legacy_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(i), 0);
  1694. iwl_legacy_write_direct32(priv, HBUS_TARG_WRPTR, 0 | (i << 8));
  1695. /* Max Tx Window size for Scheduler-ACK mode */
  1696. iwl_legacy_write_targ_mem(priv, priv->scd_base_addr +
  1697. IWL49_SCD_CONTEXT_QUEUE_OFFSET(i),
  1698. (SCD_WIN_SIZE <<
  1699. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  1700. IWL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1701. /* Frame limit */
  1702. iwl_legacy_write_targ_mem(priv, priv->scd_base_addr +
  1703. IWL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
  1704. sizeof(u32),
  1705. (SCD_FRAME_LIMIT <<
  1706. IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1707. IWL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1708. }
  1709. iwl_legacy_write_prph(priv, IWL49_SCD_INTERRUPT_MASK,
  1710. (1 << priv->hw_params.max_txq_num) - 1);
  1711. /* Activate all Tx DMA/FIFO channels */
  1712. iwl4965_txq_set_sched(priv, IWL_MASK(0, 6));
  1713. iwl4965_set_wr_ptrs(priv, IWL_DEFAULT_CMD_QUEUE_NUM, 0);
  1714. /* make sure all queue are not stopped */
  1715. memset(&priv->queue_stopped[0], 0, sizeof(priv->queue_stopped));
  1716. for (i = 0; i < 4; i++)
  1717. atomic_set(&priv->queue_stop_count[i], 0);
  1718. /* reset to 0 to enable all the queue first */
  1719. priv->txq_ctx_active_msk = 0;
  1720. /* Map each Tx/cmd queue to its corresponding fifo */
  1721. BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
  1722. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  1723. int ac = default_queue_to_tx_fifo[i];
  1724. iwl_txq_ctx_activate(priv, i);
  1725. if (ac == IWL_TX_FIFO_UNUSED)
  1726. continue;
  1727. iwl4965_tx_queue_set_status(priv, &priv->txq[i], ac, 0);
  1728. }
  1729. spin_unlock_irqrestore(&priv->lock, flags);
  1730. return 0;
  1731. }
  1732. /**
  1733. * iwl4965_alive_start - called after REPLY_ALIVE notification received
  1734. * from protocol/runtime uCode (initialization uCode's
  1735. * Alive gets handled by iwl_init_alive_start()).
  1736. */
  1737. static void iwl4965_alive_start(struct iwl_priv *priv)
  1738. {
  1739. int ret = 0;
  1740. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1741. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1742. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  1743. /* We had an error bringing up the hardware, so take it
  1744. * all the way back down so we can try again */
  1745. IWL_DEBUG_INFO(priv, "Alive failed.\n");
  1746. goto restart;
  1747. }
  1748. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  1749. * This is a paranoid check, because we would not have gotten the
  1750. * "runtime" alive if code weren't properly loaded. */
  1751. if (iwl4965_verify_ucode(priv)) {
  1752. /* Runtime instruction load was bad;
  1753. * take it all the way back down so we can try again */
  1754. IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
  1755. goto restart;
  1756. }
  1757. ret = iwl4965_alive_notify(priv);
  1758. if (ret) {
  1759. IWL_WARN(priv,
  1760. "Could not complete ALIVE transition [ntf]: %d\n", ret);
  1761. goto restart;
  1762. }
  1763. /* After the ALIVE response, we can send host commands to the uCode */
  1764. set_bit(STATUS_ALIVE, &priv->status);
  1765. /* Enable watchdog to monitor the driver tx queues */
  1766. iwl_legacy_setup_watchdog(priv);
  1767. if (iwl_legacy_is_rfkill(priv))
  1768. return;
  1769. ieee80211_wake_queues(priv->hw);
  1770. priv->active_rate = IWL_RATES_MASK;
  1771. if (iwl_legacy_is_associated_ctx(ctx)) {
  1772. struct iwl_legacy_rxon_cmd *active_rxon =
  1773. (struct iwl_legacy_rxon_cmd *)&ctx->active;
  1774. /* apply any changes in staging */
  1775. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1776. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1777. } else {
  1778. struct iwl_rxon_context *tmp;
  1779. /* Initialize our rx_config data */
  1780. for_each_context(priv, tmp)
  1781. iwl_legacy_connection_init_rx_config(priv, tmp);
  1782. if (priv->cfg->ops->hcmd->set_rxon_chain)
  1783. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  1784. }
  1785. /* Configure bluetooth coexistence if enabled */
  1786. iwl_legacy_send_bt_config(priv);
  1787. iwl4965_reset_run_time_calib(priv);
  1788. set_bit(STATUS_READY, &priv->status);
  1789. /* Configure the adapter for unassociated operation */
  1790. iwl_legacy_commit_rxon(priv, ctx);
  1791. /* At this point, the NIC is initialized and operational */
  1792. iwl4965_rf_kill_ct_config(priv);
  1793. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  1794. wake_up_interruptible(&priv->wait_command_queue);
  1795. iwl_legacy_power_update_mode(priv, true);
  1796. IWL_DEBUG_INFO(priv, "Updated power mode\n");
  1797. return;
  1798. restart:
  1799. queue_work(priv->workqueue, &priv->restart);
  1800. }
  1801. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
  1802. static void __iwl4965_down(struct iwl_priv *priv)
  1803. {
  1804. unsigned long flags;
  1805. int exit_pending;
  1806. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  1807. iwl_legacy_scan_cancel_timeout(priv, 200);
  1808. exit_pending = test_and_set_bit(STATUS_EXIT_PENDING, &priv->status);
  1809. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  1810. * to prevent rearm timer */
  1811. del_timer_sync(&priv->watchdog);
  1812. iwl_legacy_clear_ucode_stations(priv, NULL);
  1813. iwl_legacy_dealloc_bcast_stations(priv);
  1814. iwl_legacy_clear_driver_stations(priv);
  1815. /* Unblock any waiting calls */
  1816. wake_up_interruptible_all(&priv->wait_command_queue);
  1817. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1818. * exiting the module */
  1819. if (!exit_pending)
  1820. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  1821. /* stop and reset the on-board processor */
  1822. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  1823. /* tell the device to stop sending interrupts */
  1824. spin_lock_irqsave(&priv->lock, flags);
  1825. iwl_legacy_disable_interrupts(priv);
  1826. spin_unlock_irqrestore(&priv->lock, flags);
  1827. iwl4965_synchronize_irq(priv);
  1828. if (priv->mac80211_registered)
  1829. ieee80211_stop_queues(priv->hw);
  1830. /* If we have not previously called iwl_init() then
  1831. * clear all bits but the RF Kill bit and return */
  1832. if (!iwl_legacy_is_init(priv)) {
  1833. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  1834. STATUS_RF_KILL_HW |
  1835. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  1836. STATUS_GEO_CONFIGURED |
  1837. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  1838. STATUS_EXIT_PENDING;
  1839. goto exit;
  1840. }
  1841. /* ...otherwise clear out all the status bits but the RF Kill
  1842. * bit and continue taking the NIC down. */
  1843. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  1844. STATUS_RF_KILL_HW |
  1845. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  1846. STATUS_GEO_CONFIGURED |
  1847. test_bit(STATUS_FW_ERROR, &priv->status) <<
  1848. STATUS_FW_ERROR |
  1849. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  1850. STATUS_EXIT_PENDING;
  1851. iwl4965_txq_ctx_stop(priv);
  1852. iwl4965_rxq_stop(priv);
  1853. /* Power-down device's busmaster DMA clocks */
  1854. iwl_legacy_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  1855. udelay(5);
  1856. /* Make sure (redundant) we've released our request to stay awake */
  1857. iwl_legacy_clear_bit(priv, CSR_GP_CNTRL,
  1858. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  1859. /* Stop the device, and put it in low power state */
  1860. iwl_legacy_apm_stop(priv);
  1861. exit:
  1862. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  1863. dev_kfree_skb(priv->beacon_skb);
  1864. priv->beacon_skb = NULL;
  1865. /* clear out any free frames */
  1866. iwl4965_clear_free_frames(priv);
  1867. }
  1868. static void iwl4965_down(struct iwl_priv *priv)
  1869. {
  1870. mutex_lock(&priv->mutex);
  1871. __iwl4965_down(priv);
  1872. mutex_unlock(&priv->mutex);
  1873. iwl4965_cancel_deferred_work(priv);
  1874. }
  1875. #define HW_READY_TIMEOUT (50)
  1876. static int iwl4965_set_hw_ready(struct iwl_priv *priv)
  1877. {
  1878. int ret = 0;
  1879. iwl_legacy_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1880. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  1881. /* See if we got it */
  1882. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  1883. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  1884. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  1885. HW_READY_TIMEOUT);
  1886. if (ret != -ETIMEDOUT)
  1887. priv->hw_ready = true;
  1888. else
  1889. priv->hw_ready = false;
  1890. IWL_DEBUG_INFO(priv, "hardware %s\n",
  1891. (priv->hw_ready == 1) ? "ready" : "not ready");
  1892. return ret;
  1893. }
  1894. static int iwl4965_prepare_card_hw(struct iwl_priv *priv)
  1895. {
  1896. int ret = 0;
  1897. IWL_DEBUG_INFO(priv, "iwl4965_prepare_card_hw enter\n");
  1898. ret = iwl4965_set_hw_ready(priv);
  1899. if (priv->hw_ready)
  1900. return ret;
  1901. /* If HW is not ready, prepare the conditions to check again */
  1902. iwl_legacy_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  1903. CSR_HW_IF_CONFIG_REG_PREPARE);
  1904. ret = iwl_poll_bit(priv, CSR_HW_IF_CONFIG_REG,
  1905. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  1906. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  1907. /* HW should be ready by now, check again. */
  1908. if (ret != -ETIMEDOUT)
  1909. iwl4965_set_hw_ready(priv);
  1910. return ret;
  1911. }
  1912. #define MAX_HW_RESTARTS 5
  1913. static int __iwl4965_up(struct iwl_priv *priv)
  1914. {
  1915. struct iwl_rxon_context *ctx;
  1916. int i;
  1917. int ret;
  1918. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  1919. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  1920. return -EIO;
  1921. }
  1922. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  1923. IWL_ERR(priv, "ucode not available for device bringup\n");
  1924. return -EIO;
  1925. }
  1926. for_each_context(priv, ctx) {
  1927. ret = iwl4965_alloc_bcast_station(priv, ctx);
  1928. if (ret) {
  1929. iwl_legacy_dealloc_bcast_stations(priv);
  1930. return ret;
  1931. }
  1932. }
  1933. iwl4965_prepare_card_hw(priv);
  1934. if (!priv->hw_ready) {
  1935. IWL_WARN(priv, "Exit HW not ready\n");
  1936. return -EIO;
  1937. }
  1938. /* If platform's RF_KILL switch is NOT set to KILL */
  1939. if (iwl_read32(priv,
  1940. CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  1941. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1942. else
  1943. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1944. if (iwl_legacy_is_rfkill(priv)) {
  1945. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  1946. iwl_legacy_enable_interrupts(priv);
  1947. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  1948. return 0;
  1949. }
  1950. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  1951. /* must be initialised before iwl_hw_nic_init */
  1952. priv->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  1953. ret = iwl4965_hw_nic_init(priv);
  1954. if (ret) {
  1955. IWL_ERR(priv, "Unable to init nic\n");
  1956. return ret;
  1957. }
  1958. /* make sure rfkill handshake bits are cleared */
  1959. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1960. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  1961. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  1962. /* clear (again), then enable host interrupts */
  1963. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  1964. iwl_legacy_enable_interrupts(priv);
  1965. /* really make sure rfkill handshake bits are cleared */
  1966. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1967. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  1968. /* Copy original ucode data image from disk into backup cache.
  1969. * This will be used to initialize the on-board processor's
  1970. * data SRAM for a clean start when the runtime program first loads. */
  1971. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  1972. priv->ucode_data.len);
  1973. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  1974. /* load bootstrap state machine,
  1975. * load bootstrap program into processor's memory,
  1976. * prepare to load the "initialize" uCode */
  1977. ret = priv->cfg->ops->lib->load_ucode(priv);
  1978. if (ret) {
  1979. IWL_ERR(priv, "Unable to set up bootstrap uCode: %d\n",
  1980. ret);
  1981. continue;
  1982. }
  1983. /* start card; "initialize" will load runtime ucode */
  1984. iwl4965_nic_start(priv);
  1985. IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
  1986. return 0;
  1987. }
  1988. set_bit(STATUS_EXIT_PENDING, &priv->status);
  1989. __iwl4965_down(priv);
  1990. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  1991. /* tried to restart and config the device for as long as our
  1992. * patience could withstand */
  1993. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  1994. return -EIO;
  1995. }
  1996. /*****************************************************************************
  1997. *
  1998. * Workqueue callbacks
  1999. *
  2000. *****************************************************************************/
  2001. static void iwl4965_bg_init_alive_start(struct work_struct *data)
  2002. {
  2003. struct iwl_priv *priv =
  2004. container_of(data, struct iwl_priv, init_alive_start.work);
  2005. mutex_lock(&priv->mutex);
  2006. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2007. goto out;
  2008. priv->cfg->ops->lib->init_alive_start(priv);
  2009. out:
  2010. mutex_unlock(&priv->mutex);
  2011. }
  2012. static void iwl4965_bg_alive_start(struct work_struct *data)
  2013. {
  2014. struct iwl_priv *priv =
  2015. container_of(data, struct iwl_priv, alive_start.work);
  2016. mutex_lock(&priv->mutex);
  2017. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2018. goto out;
  2019. iwl4965_alive_start(priv);
  2020. out:
  2021. mutex_unlock(&priv->mutex);
  2022. }
  2023. static void iwl4965_bg_run_time_calib_work(struct work_struct *work)
  2024. {
  2025. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  2026. run_time_calib_work);
  2027. mutex_lock(&priv->mutex);
  2028. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  2029. test_bit(STATUS_SCANNING, &priv->status)) {
  2030. mutex_unlock(&priv->mutex);
  2031. return;
  2032. }
  2033. if (priv->start_calib) {
  2034. iwl4965_chain_noise_calibration(priv,
  2035. (void *)&priv->_4965.statistics);
  2036. iwl4965_sensitivity_calibration(priv,
  2037. (void *)&priv->_4965.statistics);
  2038. }
  2039. mutex_unlock(&priv->mutex);
  2040. }
  2041. static void iwl4965_bg_restart(struct work_struct *data)
  2042. {
  2043. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  2044. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2045. return;
  2046. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
  2047. struct iwl_rxon_context *ctx;
  2048. mutex_lock(&priv->mutex);
  2049. for_each_context(priv, ctx)
  2050. ctx->vif = NULL;
  2051. priv->is_open = 0;
  2052. __iwl4965_down(priv);
  2053. mutex_unlock(&priv->mutex);
  2054. iwl4965_cancel_deferred_work(priv);
  2055. ieee80211_restart_hw(priv->hw);
  2056. } else {
  2057. iwl4965_down(priv);
  2058. mutex_lock(&priv->mutex);
  2059. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2060. mutex_unlock(&priv->mutex);
  2061. return;
  2062. }
  2063. __iwl4965_up(priv);
  2064. mutex_unlock(&priv->mutex);
  2065. }
  2066. }
  2067. static void iwl4965_bg_rx_replenish(struct work_struct *data)
  2068. {
  2069. struct iwl_priv *priv =
  2070. container_of(data, struct iwl_priv, rx_replenish);
  2071. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2072. return;
  2073. mutex_lock(&priv->mutex);
  2074. iwl4965_rx_replenish(priv);
  2075. mutex_unlock(&priv->mutex);
  2076. }
  2077. /*****************************************************************************
  2078. *
  2079. * mac80211 entry point functions
  2080. *
  2081. *****************************************************************************/
  2082. #define UCODE_READY_TIMEOUT (4 * HZ)
  2083. /*
  2084. * Not a mac80211 entry point function, but it fits in with all the
  2085. * other mac80211 functions grouped here.
  2086. */
  2087. static int iwl4965_mac_setup_register(struct iwl_priv *priv,
  2088. u32 max_probe_length)
  2089. {
  2090. int ret;
  2091. struct ieee80211_hw *hw = priv->hw;
  2092. struct iwl_rxon_context *ctx;
  2093. hw->rate_control_algorithm = "iwl-4965-rs";
  2094. /* Tell mac80211 our characteristics */
  2095. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  2096. IEEE80211_HW_AMPDU_AGGREGATION |
  2097. IEEE80211_HW_NEED_DTIM_PERIOD |
  2098. IEEE80211_HW_SPECTRUM_MGMT |
  2099. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  2100. if (priv->cfg->sku & IWL_SKU_N)
  2101. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  2102. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  2103. hw->sta_data_size = sizeof(struct iwl_station_priv);
  2104. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  2105. for_each_context(priv, ctx) {
  2106. hw->wiphy->interface_modes |= ctx->interface_modes;
  2107. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  2108. }
  2109. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  2110. WIPHY_FLAG_DISABLE_BEACON_HINTS;
  2111. /*
  2112. * For now, disable PS by default because it affects
  2113. * RX performance significantly.
  2114. */
  2115. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  2116. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  2117. /* we create the 802.11 header and a zero-length SSID element */
  2118. hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
  2119. /* Default value; 4 EDCA QOS priorities */
  2120. hw->queues = 4;
  2121. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  2122. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  2123. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  2124. &priv->bands[IEEE80211_BAND_2GHZ];
  2125. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  2126. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  2127. &priv->bands[IEEE80211_BAND_5GHZ];
  2128. iwl_legacy_leds_init(priv);
  2129. ret = ieee80211_register_hw(priv->hw);
  2130. if (ret) {
  2131. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  2132. return ret;
  2133. }
  2134. priv->mac80211_registered = 1;
  2135. return 0;
  2136. }
  2137. int iwl4965_mac_start(struct ieee80211_hw *hw)
  2138. {
  2139. struct iwl_priv *priv = hw->priv;
  2140. int ret;
  2141. IWL_DEBUG_MAC80211(priv, "enter\n");
  2142. /* we should be verifying the device is ready to be opened */
  2143. mutex_lock(&priv->mutex);
  2144. ret = __iwl4965_up(priv);
  2145. mutex_unlock(&priv->mutex);
  2146. if (ret)
  2147. return ret;
  2148. if (iwl_legacy_is_rfkill(priv))
  2149. goto out;
  2150. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  2151. /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
  2152. * mac80211 will not be run successfully. */
  2153. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  2154. test_bit(STATUS_READY, &priv->status),
  2155. UCODE_READY_TIMEOUT);
  2156. if (!ret) {
  2157. if (!test_bit(STATUS_READY, &priv->status)) {
  2158. IWL_ERR(priv, "START_ALIVE timeout after %dms.\n",
  2159. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  2160. return -ETIMEDOUT;
  2161. }
  2162. }
  2163. iwl4965_led_enable(priv);
  2164. out:
  2165. priv->is_open = 1;
  2166. IWL_DEBUG_MAC80211(priv, "leave\n");
  2167. return 0;
  2168. }
  2169. void iwl4965_mac_stop(struct ieee80211_hw *hw)
  2170. {
  2171. struct iwl_priv *priv = hw->priv;
  2172. IWL_DEBUG_MAC80211(priv, "enter\n");
  2173. if (!priv->is_open)
  2174. return;
  2175. priv->is_open = 0;
  2176. iwl4965_down(priv);
  2177. flush_workqueue(priv->workqueue);
  2178. /* User space software may expect getting rfkill changes
  2179. * even if interface is down */
  2180. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2181. iwl_legacy_enable_rfkill_int(priv);
  2182. IWL_DEBUG_MAC80211(priv, "leave\n");
  2183. }
  2184. void iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2185. {
  2186. struct iwl_priv *priv = hw->priv;
  2187. IWL_DEBUG_MACDUMP(priv, "enter\n");
  2188. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2189. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2190. if (iwl4965_tx_skb(priv, skb))
  2191. dev_kfree_skb_any(skb);
  2192. IWL_DEBUG_MACDUMP(priv, "leave\n");
  2193. }
  2194. void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
  2195. struct ieee80211_vif *vif,
  2196. struct ieee80211_key_conf *keyconf,
  2197. struct ieee80211_sta *sta,
  2198. u32 iv32, u16 *phase1key)
  2199. {
  2200. struct iwl_priv *priv = hw->priv;
  2201. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2202. IWL_DEBUG_MAC80211(priv, "enter\n");
  2203. iwl4965_update_tkip_key(priv, vif_priv->ctx, keyconf, sta,
  2204. iv32, phase1key);
  2205. IWL_DEBUG_MAC80211(priv, "leave\n");
  2206. }
  2207. int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2208. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  2209. struct ieee80211_key_conf *key)
  2210. {
  2211. struct iwl_priv *priv = hw->priv;
  2212. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2213. struct iwl_rxon_context *ctx = vif_priv->ctx;
  2214. int ret;
  2215. u8 sta_id;
  2216. bool is_default_wep_key = false;
  2217. IWL_DEBUG_MAC80211(priv, "enter\n");
  2218. if (priv->cfg->mod_params->sw_crypto) {
  2219. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  2220. return -EOPNOTSUPP;
  2221. }
  2222. sta_id = iwl_legacy_sta_id_or_broadcast(priv, vif_priv->ctx, sta);
  2223. if (sta_id == IWL_INVALID_STATION)
  2224. return -EINVAL;
  2225. mutex_lock(&priv->mutex);
  2226. iwl_legacy_scan_cancel_timeout(priv, 100);
  2227. /*
  2228. * If we are getting WEP group key and we didn't receive any key mapping
  2229. * so far, we are in legacy wep mode (group key only), otherwise we are
  2230. * in 1X mode.
  2231. * In legacy wep mode, we use another host command to the uCode.
  2232. */
  2233. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2234. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  2235. !sta) {
  2236. if (cmd == SET_KEY)
  2237. is_default_wep_key = !ctx->key_mapping_keys;
  2238. else
  2239. is_default_wep_key =
  2240. (key->hw_key_idx == HW_KEY_DEFAULT);
  2241. }
  2242. switch (cmd) {
  2243. case SET_KEY:
  2244. if (is_default_wep_key)
  2245. ret = iwl4965_set_default_wep_key(priv,
  2246. vif_priv->ctx, key);
  2247. else
  2248. ret = iwl4965_set_dynamic_key(priv, vif_priv->ctx,
  2249. key, sta_id);
  2250. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  2251. break;
  2252. case DISABLE_KEY:
  2253. if (is_default_wep_key)
  2254. ret = iwl4965_remove_default_wep_key(priv, ctx, key);
  2255. else
  2256. ret = iwl4965_remove_dynamic_key(priv, ctx,
  2257. key, sta_id);
  2258. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  2259. break;
  2260. default:
  2261. ret = -EINVAL;
  2262. }
  2263. mutex_unlock(&priv->mutex);
  2264. IWL_DEBUG_MAC80211(priv, "leave\n");
  2265. return ret;
  2266. }
  2267. int iwl4965_mac_ampdu_action(struct ieee80211_hw *hw,
  2268. struct ieee80211_vif *vif,
  2269. enum ieee80211_ampdu_mlme_action action,
  2270. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  2271. u8 buf_size)
  2272. {
  2273. struct iwl_priv *priv = hw->priv;
  2274. int ret = -EINVAL;
  2275. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  2276. sta->addr, tid);
  2277. if (!(priv->cfg->sku & IWL_SKU_N))
  2278. return -EACCES;
  2279. mutex_lock(&priv->mutex);
  2280. switch (action) {
  2281. case IEEE80211_AMPDU_RX_START:
  2282. IWL_DEBUG_HT(priv, "start Rx\n");
  2283. ret = iwl4965_sta_rx_agg_start(priv, sta, tid, *ssn);
  2284. break;
  2285. case IEEE80211_AMPDU_RX_STOP:
  2286. IWL_DEBUG_HT(priv, "stop Rx\n");
  2287. ret = iwl4965_sta_rx_agg_stop(priv, sta, tid);
  2288. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2289. ret = 0;
  2290. break;
  2291. case IEEE80211_AMPDU_TX_START:
  2292. IWL_DEBUG_HT(priv, "start Tx\n");
  2293. ret = iwl4965_tx_agg_start(priv, vif, sta, tid, ssn);
  2294. if (ret == 0) {
  2295. priv->_4965.agg_tids_count++;
  2296. IWL_DEBUG_HT(priv, "priv->_4965.agg_tids_count = %u\n",
  2297. priv->_4965.agg_tids_count);
  2298. }
  2299. break;
  2300. case IEEE80211_AMPDU_TX_STOP:
  2301. IWL_DEBUG_HT(priv, "stop Tx\n");
  2302. ret = iwl4965_tx_agg_stop(priv, vif, sta, tid);
  2303. if ((ret == 0) && (priv->_4965.agg_tids_count > 0)) {
  2304. priv->_4965.agg_tids_count--;
  2305. IWL_DEBUG_HT(priv, "priv->_4965.agg_tids_count = %u\n",
  2306. priv->_4965.agg_tids_count);
  2307. }
  2308. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2309. ret = 0;
  2310. break;
  2311. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2312. ret = 0;
  2313. break;
  2314. }
  2315. mutex_unlock(&priv->mutex);
  2316. return ret;
  2317. }
  2318. int iwl4965_mac_sta_add(struct ieee80211_hw *hw,
  2319. struct ieee80211_vif *vif,
  2320. struct ieee80211_sta *sta)
  2321. {
  2322. struct iwl_priv *priv = hw->priv;
  2323. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  2324. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2325. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  2326. int ret;
  2327. u8 sta_id;
  2328. IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
  2329. sta->addr);
  2330. mutex_lock(&priv->mutex);
  2331. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  2332. sta->addr);
  2333. sta_priv->common.sta_id = IWL_INVALID_STATION;
  2334. atomic_set(&sta_priv->pending_frames, 0);
  2335. ret = iwl_legacy_add_station_common(priv, vif_priv->ctx, sta->addr,
  2336. is_ap, sta, &sta_id);
  2337. if (ret) {
  2338. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  2339. sta->addr, ret);
  2340. /* Should we return success if return code is EEXIST ? */
  2341. mutex_unlock(&priv->mutex);
  2342. return ret;
  2343. }
  2344. sta_priv->common.sta_id = sta_id;
  2345. /* Initialize rate scaling */
  2346. IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
  2347. sta->addr);
  2348. iwl4965_rs_rate_init(priv, sta, sta_id);
  2349. mutex_unlock(&priv->mutex);
  2350. return 0;
  2351. }
  2352. void iwl4965_mac_channel_switch(struct ieee80211_hw *hw,
  2353. struct ieee80211_channel_switch *ch_switch)
  2354. {
  2355. struct iwl_priv *priv = hw->priv;
  2356. const struct iwl_channel_info *ch_info;
  2357. struct ieee80211_conf *conf = &hw->conf;
  2358. struct ieee80211_channel *channel = ch_switch->channel;
  2359. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  2360. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2361. u16 ch;
  2362. unsigned long flags = 0;
  2363. IWL_DEBUG_MAC80211(priv, "enter\n");
  2364. mutex_lock(&priv->mutex);
  2365. if (iwl_legacy_is_rfkill(priv))
  2366. goto out;
  2367. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  2368. test_bit(STATUS_SCANNING, &priv->status))
  2369. goto out;
  2370. if (!iwl_legacy_is_associated_ctx(ctx))
  2371. goto out;
  2372. /* channel switch in progress */
  2373. if (priv->switch_rxon.switch_in_progress == true)
  2374. goto out;
  2375. if (priv->cfg->ops->lib->set_channel_switch) {
  2376. ch = channel->hw_value;
  2377. if (le16_to_cpu(ctx->active.channel) != ch) {
  2378. ch_info = iwl_legacy_get_channel_info(priv,
  2379. channel->band,
  2380. ch);
  2381. if (!iwl_legacy_is_channel_valid(ch_info)) {
  2382. IWL_DEBUG_MAC80211(priv, "invalid channel\n");
  2383. goto out;
  2384. }
  2385. spin_lock_irqsave(&priv->lock, flags);
  2386. priv->current_ht_config.smps = conf->smps_mode;
  2387. /* Configure HT40 channels */
  2388. ctx->ht.enabled = conf_is_ht(conf);
  2389. if (ctx->ht.enabled) {
  2390. if (conf_is_ht40_minus(conf)) {
  2391. ctx->ht.extension_chan_offset =
  2392. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2393. ctx->ht.is_40mhz = true;
  2394. } else if (conf_is_ht40_plus(conf)) {
  2395. ctx->ht.extension_chan_offset =
  2396. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2397. ctx->ht.is_40mhz = true;
  2398. } else {
  2399. ctx->ht.extension_chan_offset =
  2400. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2401. ctx->ht.is_40mhz = false;
  2402. }
  2403. } else
  2404. ctx->ht.is_40mhz = false;
  2405. if ((le16_to_cpu(ctx->staging.channel) != ch))
  2406. ctx->staging.flags = 0;
  2407. iwl_legacy_set_rxon_channel(priv, channel, ctx);
  2408. iwl_legacy_set_rxon_ht(priv, ht_conf);
  2409. iwl_legacy_set_flags_for_band(priv, ctx, channel->band,
  2410. ctx->vif);
  2411. spin_unlock_irqrestore(&priv->lock, flags);
  2412. iwl_legacy_set_rate(priv);
  2413. /*
  2414. * at this point, staging_rxon has the
  2415. * configuration for channel switch
  2416. */
  2417. if (priv->cfg->ops->lib->set_channel_switch(priv,
  2418. ch_switch))
  2419. priv->switch_rxon.switch_in_progress = false;
  2420. }
  2421. }
  2422. out:
  2423. mutex_unlock(&priv->mutex);
  2424. if (!priv->switch_rxon.switch_in_progress)
  2425. ieee80211_chswitch_done(ctx->vif, false);
  2426. IWL_DEBUG_MAC80211(priv, "leave\n");
  2427. }
  2428. void iwl4965_configure_filter(struct ieee80211_hw *hw,
  2429. unsigned int changed_flags,
  2430. unsigned int *total_flags,
  2431. u64 multicast)
  2432. {
  2433. struct iwl_priv *priv = hw->priv;
  2434. __le32 filter_or = 0, filter_nand = 0;
  2435. struct iwl_rxon_context *ctx;
  2436. #define CHK(test, flag) do { \
  2437. if (*total_flags & (test)) \
  2438. filter_or |= (flag); \
  2439. else \
  2440. filter_nand |= (flag); \
  2441. } while (0)
  2442. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  2443. changed_flags, *total_flags);
  2444. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  2445. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  2446. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  2447. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  2448. #undef CHK
  2449. mutex_lock(&priv->mutex);
  2450. for_each_context(priv, ctx) {
  2451. ctx->staging.filter_flags &= ~filter_nand;
  2452. ctx->staging.filter_flags |= filter_or;
  2453. /*
  2454. * Not committing directly because hardware can perform a scan,
  2455. * but we'll eventually commit the filter flags change anyway.
  2456. */
  2457. }
  2458. mutex_unlock(&priv->mutex);
  2459. /*
  2460. * Receiving all multicast frames is always enabled by the
  2461. * default flags setup in iwl_legacy_connection_init_rx_config()
  2462. * since we currently do not support programming multicast
  2463. * filters into the device.
  2464. */
  2465. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  2466. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  2467. }
  2468. /*****************************************************************************
  2469. *
  2470. * driver setup and teardown
  2471. *
  2472. *****************************************************************************/
  2473. static void iwl4965_bg_txpower_work(struct work_struct *work)
  2474. {
  2475. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  2476. txpower_work);
  2477. /* If a scan happened to start before we got here
  2478. * then just return; the statistics notification will
  2479. * kick off another scheduled work to compensate for
  2480. * any temperature delta we missed here. */
  2481. if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
  2482. test_bit(STATUS_SCANNING, &priv->status))
  2483. return;
  2484. mutex_lock(&priv->mutex);
  2485. /* Regardless of if we are associated, we must reconfigure the
  2486. * TX power since frames can be sent on non-radar channels while
  2487. * not associated */
  2488. priv->cfg->ops->lib->send_tx_power(priv);
  2489. /* Update last_temperature to keep is_calib_needed from running
  2490. * when it isn't needed... */
  2491. priv->last_temperature = priv->temperature;
  2492. mutex_unlock(&priv->mutex);
  2493. }
  2494. static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
  2495. {
  2496. priv->workqueue = create_singlethread_workqueue(DRV_NAME);
  2497. init_waitqueue_head(&priv->wait_command_queue);
  2498. INIT_WORK(&priv->restart, iwl4965_bg_restart);
  2499. INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
  2500. INIT_WORK(&priv->run_time_calib_work, iwl4965_bg_run_time_calib_work);
  2501. INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
  2502. INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
  2503. iwl_legacy_setup_scan_deferred_work(priv);
  2504. INIT_WORK(&priv->txpower_work, iwl4965_bg_txpower_work);
  2505. init_timer(&priv->statistics_periodic);
  2506. priv->statistics_periodic.data = (unsigned long)priv;
  2507. priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
  2508. init_timer(&priv->ucode_trace);
  2509. priv->ucode_trace.data = (unsigned long)priv;
  2510. priv->ucode_trace.function = iwl4965_bg_ucode_trace;
  2511. init_timer(&priv->watchdog);
  2512. priv->watchdog.data = (unsigned long)priv;
  2513. priv->watchdog.function = iwl_legacy_bg_watchdog;
  2514. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  2515. iwl4965_irq_tasklet, (unsigned long)priv);
  2516. }
  2517. static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
  2518. {
  2519. cancel_work_sync(&priv->txpower_work);
  2520. cancel_delayed_work_sync(&priv->init_alive_start);
  2521. cancel_delayed_work(&priv->alive_start);
  2522. cancel_work_sync(&priv->run_time_calib_work);
  2523. iwl_legacy_cancel_scan_deferred_work(priv);
  2524. del_timer_sync(&priv->statistics_periodic);
  2525. del_timer_sync(&priv->ucode_trace);
  2526. }
  2527. static void iwl4965_init_hw_rates(struct iwl_priv *priv,
  2528. struct ieee80211_rate *rates)
  2529. {
  2530. int i;
  2531. for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
  2532. rates[i].bitrate = iwlegacy_rates[i].ieee * 5;
  2533. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  2534. rates[i].hw_value_short = i;
  2535. rates[i].flags = 0;
  2536. if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
  2537. /*
  2538. * If CCK != 1M then set short preamble rate flag.
  2539. */
  2540. rates[i].flags |=
  2541. (iwlegacy_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  2542. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  2543. }
  2544. }
  2545. }
  2546. /*
  2547. * Acquire priv->lock before calling this function !
  2548. */
  2549. void iwl4965_set_wr_ptrs(struct iwl_priv *priv, int txq_id, u32 index)
  2550. {
  2551. iwl_legacy_write_direct32(priv, HBUS_TARG_WRPTR,
  2552. (index & 0xff) | (txq_id << 8));
  2553. iwl_legacy_write_prph(priv, IWL49_SCD_QUEUE_RDPTR(txq_id), index);
  2554. }
  2555. void iwl4965_tx_queue_set_status(struct iwl_priv *priv,
  2556. struct iwl_tx_queue *txq,
  2557. int tx_fifo_id, int scd_retry)
  2558. {
  2559. int txq_id = txq->q.id;
  2560. /* Find out whether to activate Tx queue */
  2561. int active = test_bit(txq_id, &priv->txq_ctx_active_msk) ? 1 : 0;
  2562. /* Set up and activate */
  2563. iwl_legacy_write_prph(priv, IWL49_SCD_QUEUE_STATUS_BITS(txq_id),
  2564. (active << IWL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  2565. (tx_fifo_id << IWL49_SCD_QUEUE_STTS_REG_POS_TXF) |
  2566. (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_WSL) |
  2567. (scd_retry << IWL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  2568. IWL49_SCD_QUEUE_STTS_REG_MSK);
  2569. txq->sched_retry = scd_retry;
  2570. IWL_DEBUG_INFO(priv, "%s %s Queue %d on AC %d\n",
  2571. active ? "Activate" : "Deactivate",
  2572. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  2573. }
  2574. static int iwl4965_init_drv(struct iwl_priv *priv)
  2575. {
  2576. int ret;
  2577. spin_lock_init(&priv->sta_lock);
  2578. spin_lock_init(&priv->hcmd_lock);
  2579. INIT_LIST_HEAD(&priv->free_frames);
  2580. mutex_init(&priv->mutex);
  2581. priv->ieee_channels = NULL;
  2582. priv->ieee_rates = NULL;
  2583. priv->band = IEEE80211_BAND_2GHZ;
  2584. priv->iw_mode = NL80211_IFTYPE_STATION;
  2585. priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  2586. priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  2587. priv->_4965.agg_tids_count = 0;
  2588. /* initialize force reset */
  2589. priv->force_reset[IWL_RF_RESET].reset_duration =
  2590. IWL_DELAY_NEXT_FORCE_RF_RESET;
  2591. priv->force_reset[IWL_FW_RESET].reset_duration =
  2592. IWL_DELAY_NEXT_FORCE_FW_RELOAD;
  2593. /* Choose which receivers/antennas to use */
  2594. if (priv->cfg->ops->hcmd->set_rxon_chain)
  2595. priv->cfg->ops->hcmd->set_rxon_chain(priv,
  2596. &priv->contexts[IWL_RXON_CTX_BSS]);
  2597. iwl_legacy_init_scan_params(priv);
  2598. ret = iwl_legacy_init_channel_map(priv);
  2599. if (ret) {
  2600. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  2601. goto err;
  2602. }
  2603. ret = iwl_legacy_init_geos(priv);
  2604. if (ret) {
  2605. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  2606. goto err_free_channel_map;
  2607. }
  2608. iwl4965_init_hw_rates(priv, priv->ieee_rates);
  2609. return 0;
  2610. err_free_channel_map:
  2611. iwl_legacy_free_channel_map(priv);
  2612. err:
  2613. return ret;
  2614. }
  2615. static void iwl4965_uninit_drv(struct iwl_priv *priv)
  2616. {
  2617. iwl4965_calib_free_results(priv);
  2618. iwl_legacy_free_geos(priv);
  2619. iwl_legacy_free_channel_map(priv);
  2620. kfree(priv->scan_cmd);
  2621. }
  2622. static void iwl4965_hw_detect(struct iwl_priv *priv)
  2623. {
  2624. priv->hw_rev = _iwl_legacy_read32(priv, CSR_HW_REV);
  2625. priv->hw_wa_rev = _iwl_legacy_read32(priv, CSR_HW_REV_WA_REG);
  2626. priv->rev_id = priv->pci_dev->revision;
  2627. IWL_DEBUG_INFO(priv, "HW Revision ID = 0x%X\n", priv->rev_id);
  2628. }
  2629. static int iwl4965_set_hw_params(struct iwl_priv *priv)
  2630. {
  2631. priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  2632. priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  2633. if (priv->cfg->mod_params->amsdu_size_8K)
  2634. priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_8K);
  2635. else
  2636. priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_4K);
  2637. priv->hw_params.max_beacon_itrvl = IWL_MAX_UCODE_BEACON_INTERVAL;
  2638. if (priv->cfg->mod_params->disable_11n)
  2639. priv->cfg->sku &= ~IWL_SKU_N;
  2640. /* Device-specific setup */
  2641. return priv->cfg->ops->lib->set_hw_params(priv);
  2642. }
  2643. static const u8 iwl4965_bss_ac_to_fifo[] = {
  2644. IWL_TX_FIFO_VO,
  2645. IWL_TX_FIFO_VI,
  2646. IWL_TX_FIFO_BE,
  2647. IWL_TX_FIFO_BK,
  2648. };
  2649. static const u8 iwl4965_bss_ac_to_queue[] = {
  2650. 0, 1, 2, 3,
  2651. };
  2652. static int
  2653. iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2654. {
  2655. int err = 0, i;
  2656. struct iwl_priv *priv;
  2657. struct ieee80211_hw *hw;
  2658. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  2659. unsigned long flags;
  2660. u16 pci_cmd;
  2661. /************************
  2662. * 1. Allocating HW data
  2663. ************************/
  2664. hw = iwl_legacy_alloc_all(cfg);
  2665. if (!hw) {
  2666. err = -ENOMEM;
  2667. goto out;
  2668. }
  2669. priv = hw->priv;
  2670. /* At this point both hw and priv are allocated. */
  2671. /*
  2672. * The default context is always valid,
  2673. * more may be discovered when firmware
  2674. * is loaded.
  2675. */
  2676. priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
  2677. for (i = 0; i < NUM_IWL_RXON_CTX; i++)
  2678. priv->contexts[i].ctxid = i;
  2679. priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
  2680. priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
  2681. priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
  2682. priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
  2683. priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
  2684. priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
  2685. priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
  2686. priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
  2687. priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo = iwl4965_bss_ac_to_fifo;
  2688. priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue = iwl4965_bss_ac_to_queue;
  2689. priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
  2690. BIT(NL80211_IFTYPE_ADHOC);
  2691. priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
  2692. BIT(NL80211_IFTYPE_STATION);
  2693. priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
  2694. priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
  2695. priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
  2696. priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
  2697. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 1);
  2698. SET_IEEE80211_DEV(hw, &pdev->dev);
  2699. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  2700. priv->cfg = cfg;
  2701. priv->pci_dev = pdev;
  2702. priv->inta_mask = CSR_INI_SET_MASK;
  2703. if (iwl_legacy_alloc_traffic_mem(priv))
  2704. IWL_ERR(priv, "Not enough memory to generate traffic log\n");
  2705. /**************************
  2706. * 2. Initializing PCI bus
  2707. **************************/
  2708. pci_disable_link_state(pdev, PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  2709. PCIE_LINK_STATE_CLKPM);
  2710. if (pci_enable_device(pdev)) {
  2711. err = -ENODEV;
  2712. goto out_ieee80211_free_hw;
  2713. }
  2714. pci_set_master(pdev);
  2715. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  2716. if (!err)
  2717. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  2718. if (err) {
  2719. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  2720. if (!err)
  2721. err = pci_set_consistent_dma_mask(pdev,
  2722. DMA_BIT_MASK(32));
  2723. /* both attempts failed: */
  2724. if (err) {
  2725. IWL_WARN(priv, "No suitable DMA available.\n");
  2726. goto out_pci_disable_device;
  2727. }
  2728. }
  2729. err = pci_request_regions(pdev, DRV_NAME);
  2730. if (err)
  2731. goto out_pci_disable_device;
  2732. pci_set_drvdata(pdev, priv);
  2733. /***********************
  2734. * 3. Read REV register
  2735. ***********************/
  2736. priv->hw_base = pci_iomap(pdev, 0, 0);
  2737. if (!priv->hw_base) {
  2738. err = -ENODEV;
  2739. goto out_pci_release_regions;
  2740. }
  2741. IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
  2742. (unsigned long long) pci_resource_len(pdev, 0));
  2743. IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
  2744. /* these spin locks will be used in apm_ops.init and EEPROM access
  2745. * we should init now
  2746. */
  2747. spin_lock_init(&priv->reg_lock);
  2748. spin_lock_init(&priv->lock);
  2749. /*
  2750. * stop and reset the on-board processor just in case it is in a
  2751. * strange state ... like being left stranded by a primary kernel
  2752. * and this is now the kdump kernel trying to start up
  2753. */
  2754. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2755. iwl4965_hw_detect(priv);
  2756. IWL_INFO(priv, "Detected %s, REV=0x%X\n",
  2757. priv->cfg->name, priv->hw_rev);
  2758. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  2759. * PCI Tx retries from interfering with C3 CPU state */
  2760. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  2761. iwl4965_prepare_card_hw(priv);
  2762. if (!priv->hw_ready) {
  2763. IWL_WARN(priv, "Failed, HW not ready\n");
  2764. goto out_iounmap;
  2765. }
  2766. /*****************
  2767. * 4. Read EEPROM
  2768. *****************/
  2769. /* Read the EEPROM */
  2770. err = iwl_legacy_eeprom_init(priv);
  2771. if (err) {
  2772. IWL_ERR(priv, "Unable to init EEPROM\n");
  2773. goto out_iounmap;
  2774. }
  2775. err = iwl4965_eeprom_check_version(priv);
  2776. if (err)
  2777. goto out_free_eeprom;
  2778. if (err)
  2779. goto out_free_eeprom;
  2780. /* extract MAC Address */
  2781. iwl4965_eeprom_get_mac(priv, priv->addresses[0].addr);
  2782. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
  2783. priv->hw->wiphy->addresses = priv->addresses;
  2784. priv->hw->wiphy->n_addresses = 1;
  2785. /************************
  2786. * 5. Setup HW constants
  2787. ************************/
  2788. if (iwl4965_set_hw_params(priv)) {
  2789. IWL_ERR(priv, "failed to set hw parameters\n");
  2790. goto out_free_eeprom;
  2791. }
  2792. /*******************
  2793. * 6. Setup priv
  2794. *******************/
  2795. err = iwl4965_init_drv(priv);
  2796. if (err)
  2797. goto out_free_eeprom;
  2798. /* At this point both hw and priv are initialized. */
  2799. /********************
  2800. * 7. Setup services
  2801. ********************/
  2802. spin_lock_irqsave(&priv->lock, flags);
  2803. iwl_legacy_disable_interrupts(priv);
  2804. spin_unlock_irqrestore(&priv->lock, flags);
  2805. pci_enable_msi(priv->pci_dev);
  2806. err = request_irq(priv->pci_dev->irq, iwl_legacy_isr,
  2807. IRQF_SHARED, DRV_NAME, priv);
  2808. if (err) {
  2809. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  2810. goto out_disable_msi;
  2811. }
  2812. iwl4965_setup_deferred_work(priv);
  2813. iwl4965_setup_rx_handlers(priv);
  2814. /*********************************************
  2815. * 8. Enable interrupts and read RFKILL state
  2816. *********************************************/
  2817. /* enable rfkill interrupt: hw bug w/a */
  2818. pci_read_config_word(priv->pci_dev, PCI_COMMAND, &pci_cmd);
  2819. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  2820. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  2821. pci_write_config_word(priv->pci_dev, PCI_COMMAND, pci_cmd);
  2822. }
  2823. iwl_legacy_enable_rfkill_int(priv);
  2824. /* If platform's RF_KILL switch is NOT set to KILL */
  2825. if (iwl_read32(priv, CSR_GP_CNTRL) &
  2826. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2827. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2828. else
  2829. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2830. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  2831. test_bit(STATUS_RF_KILL_HW, &priv->status));
  2832. iwl_legacy_power_initialize(priv);
  2833. init_completion(&priv->_4965.firmware_loading_complete);
  2834. err = iwl4965_request_firmware(priv, true);
  2835. if (err)
  2836. goto out_destroy_workqueue;
  2837. return 0;
  2838. out_destroy_workqueue:
  2839. destroy_workqueue(priv->workqueue);
  2840. priv->workqueue = NULL;
  2841. free_irq(priv->pci_dev->irq, priv);
  2842. out_disable_msi:
  2843. pci_disable_msi(priv->pci_dev);
  2844. iwl4965_uninit_drv(priv);
  2845. out_free_eeprom:
  2846. iwl_legacy_eeprom_free(priv);
  2847. out_iounmap:
  2848. pci_iounmap(pdev, priv->hw_base);
  2849. out_pci_release_regions:
  2850. pci_set_drvdata(pdev, NULL);
  2851. pci_release_regions(pdev);
  2852. out_pci_disable_device:
  2853. pci_disable_device(pdev);
  2854. out_ieee80211_free_hw:
  2855. iwl_legacy_free_traffic_mem(priv);
  2856. ieee80211_free_hw(priv->hw);
  2857. out:
  2858. return err;
  2859. }
  2860. static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
  2861. {
  2862. struct iwl_priv *priv = pci_get_drvdata(pdev);
  2863. unsigned long flags;
  2864. if (!priv)
  2865. return;
  2866. wait_for_completion(&priv->_4965.firmware_loading_complete);
  2867. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  2868. iwl_legacy_dbgfs_unregister(priv);
  2869. sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
  2870. /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
  2871. * to be called and iwl4965_down since we are removing the device
  2872. * we need to set STATUS_EXIT_PENDING bit.
  2873. */
  2874. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2875. iwl_legacy_leds_exit(priv);
  2876. if (priv->mac80211_registered) {
  2877. ieee80211_unregister_hw(priv->hw);
  2878. priv->mac80211_registered = 0;
  2879. } else {
  2880. iwl4965_down(priv);
  2881. }
  2882. /*
  2883. * Make sure device is reset to low power before unloading driver.
  2884. * This may be redundant with iwl4965_down(), but there are paths to
  2885. * run iwl4965_down() without calling apm_ops.stop(), and there are
  2886. * paths to avoid running iwl4965_down() at all before leaving driver.
  2887. * This (inexpensive) call *makes sure* device is reset.
  2888. */
  2889. iwl_legacy_apm_stop(priv);
  2890. /* make sure we flush any pending irq or
  2891. * tasklet for the driver
  2892. */
  2893. spin_lock_irqsave(&priv->lock, flags);
  2894. iwl_legacy_disable_interrupts(priv);
  2895. spin_unlock_irqrestore(&priv->lock, flags);
  2896. iwl4965_synchronize_irq(priv);
  2897. iwl4965_dealloc_ucode_pci(priv);
  2898. if (priv->rxq.bd)
  2899. iwl4965_rx_queue_free(priv, &priv->rxq);
  2900. iwl4965_hw_txq_ctx_free(priv);
  2901. iwl_legacy_eeprom_free(priv);
  2902. /*netif_stop_queue(dev); */
  2903. flush_workqueue(priv->workqueue);
  2904. /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
  2905. * priv->workqueue... so we can't take down the workqueue
  2906. * until now... */
  2907. destroy_workqueue(priv->workqueue);
  2908. priv->workqueue = NULL;
  2909. iwl_legacy_free_traffic_mem(priv);
  2910. free_irq(priv->pci_dev->irq, priv);
  2911. pci_disable_msi(priv->pci_dev);
  2912. pci_iounmap(pdev, priv->hw_base);
  2913. pci_release_regions(pdev);
  2914. pci_disable_device(pdev);
  2915. pci_set_drvdata(pdev, NULL);
  2916. iwl4965_uninit_drv(priv);
  2917. dev_kfree_skb(priv->beacon_skb);
  2918. ieee80211_free_hw(priv->hw);
  2919. }
  2920. /*
  2921. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  2922. * must be called under priv->lock and mac access
  2923. */
  2924. void iwl4965_txq_set_sched(struct iwl_priv *priv, u32 mask)
  2925. {
  2926. iwl_legacy_write_prph(priv, IWL49_SCD_TXFACT, mask);
  2927. }
  2928. /*****************************************************************************
  2929. *
  2930. * driver and module entry point
  2931. *
  2932. *****************************************************************************/
  2933. /* Hardware specific file defines the PCI IDs table for that hardware module */
  2934. static DEFINE_PCI_DEVICE_TABLE(iwl4965_hw_card_ids) = {
  2935. #if defined(CONFIG_IWL4965_MODULE) || defined(CONFIG_IWL4965)
  2936. {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_cfg)},
  2937. {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_cfg)},
  2938. #endif /* CONFIG_IWL4965 */
  2939. {0}
  2940. };
  2941. MODULE_DEVICE_TABLE(pci, iwl4965_hw_card_ids);
  2942. static struct pci_driver iwl4965_driver = {
  2943. .name = DRV_NAME,
  2944. .id_table = iwl4965_hw_card_ids,
  2945. .probe = iwl4965_pci_probe,
  2946. .remove = __devexit_p(iwl4965_pci_remove),
  2947. .driver.pm = IWL_LEGACY_PM_OPS,
  2948. };
  2949. static int __init iwl4965_init(void)
  2950. {
  2951. int ret;
  2952. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  2953. pr_info(DRV_COPYRIGHT "\n");
  2954. ret = iwl4965_rate_control_register();
  2955. if (ret) {
  2956. pr_err("Unable to register rate control algorithm: %d\n", ret);
  2957. return ret;
  2958. }
  2959. ret = pci_register_driver(&iwl4965_driver);
  2960. if (ret) {
  2961. pr_err("Unable to initialize PCI module\n");
  2962. goto error_register;
  2963. }
  2964. return ret;
  2965. error_register:
  2966. iwl4965_rate_control_unregister();
  2967. return ret;
  2968. }
  2969. static void __exit iwl4965_exit(void)
  2970. {
  2971. pci_unregister_driver(&iwl4965_driver);
  2972. iwl4965_rate_control_unregister();
  2973. }
  2974. module_exit(iwl4965_exit);
  2975. module_init(iwl4965_init);
  2976. #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
  2977. module_param_named(debug, iwlegacy_debug_level, uint, S_IRUGO | S_IWUSR);
  2978. MODULE_PARM_DESC(debug, "debug output mask");
  2979. #endif
  2980. module_param_named(swcrypto, iwl4965_mod_params.sw_crypto, int, S_IRUGO);
  2981. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  2982. module_param_named(queues_num, iwl4965_mod_params.num_of_queues, int, S_IRUGO);
  2983. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  2984. module_param_named(11n_disable, iwl4965_mod_params.disable_11n, int, S_IRUGO);
  2985. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  2986. module_param_named(amsdu_size_8K, iwl4965_mod_params.amsdu_size_8K,
  2987. int, S_IRUGO);
  2988. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  2989. module_param_named(fw_restart, iwl4965_mod_params.restart_fw, int, S_IRUGO);
  2990. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");