iwl-agn.c 99 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655
  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. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/mac80211.h>
  42. #include <asm/div64.h>
  43. #include "iwl-eeprom.h"
  44. #include "iwl-dev.h"
  45. #include "iwl-core.h"
  46. #include "iwl-io.h"
  47. #include "iwl-helpers.h"
  48. #include "iwl-sta.h"
  49. #include "iwl-agn-calib.h"
  50. #include "iwl-agn.h"
  51. #include "iwl-shared.h"
  52. #include "iwl-bus.h"
  53. #include "iwl-trans.h"
  54. /******************************************************************************
  55. *
  56. * module boiler plate
  57. *
  58. ******************************************************************************/
  59. /*
  60. * module name, copyright, version, etc.
  61. */
  62. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
  63. #ifdef CONFIG_IWLWIFI_DEBUG
  64. #define VD "d"
  65. #else
  66. #define VD
  67. #endif
  68. #define DRV_VERSION IWLWIFI_VERSION VD
  69. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  70. MODULE_VERSION(DRV_VERSION);
  71. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  72. MODULE_LICENSE("GPL");
  73. void iwl_update_chain_flags(struct iwl_priv *priv)
  74. {
  75. struct iwl_rxon_context *ctx;
  76. for_each_context(priv, ctx) {
  77. iwlagn_set_rxon_chain(priv, ctx);
  78. if (ctx->active.rx_chain != ctx->staging.rx_chain)
  79. iwlagn_commit_rxon(priv, ctx);
  80. }
  81. }
  82. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  83. static void iwl_set_beacon_tim(struct iwl_priv *priv,
  84. struct iwl_tx_beacon_cmd *tx_beacon_cmd,
  85. u8 *beacon, u32 frame_size)
  86. {
  87. u16 tim_idx;
  88. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  89. /*
  90. * The index is relative to frame start but we start looking at the
  91. * variable-length part of the beacon.
  92. */
  93. tim_idx = mgmt->u.beacon.variable - beacon;
  94. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  95. while ((tim_idx < (frame_size - 2)) &&
  96. (beacon[tim_idx] != WLAN_EID_TIM))
  97. tim_idx += beacon[tim_idx+1] + 2;
  98. /* If TIM field was found, set variables */
  99. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  100. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  101. tx_beacon_cmd->tim_size = beacon[tim_idx+1];
  102. } else
  103. IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
  104. }
  105. int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
  106. {
  107. struct iwl_tx_beacon_cmd *tx_beacon_cmd;
  108. struct iwl_host_cmd cmd = {
  109. .id = REPLY_TX_BEACON,
  110. .flags = CMD_SYNC,
  111. };
  112. struct ieee80211_tx_info *info;
  113. u32 frame_size;
  114. u32 rate_flags;
  115. u32 rate;
  116. /*
  117. * We have to set up the TX command, the TX Beacon command, and the
  118. * beacon contents.
  119. */
  120. lockdep_assert_held(&priv->shrd->mutex);
  121. if (!priv->beacon_ctx) {
  122. IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
  123. return 0;
  124. }
  125. if (WARN_ON(!priv->beacon_skb))
  126. return -EINVAL;
  127. /* Allocate beacon command */
  128. if (!priv->beacon_cmd)
  129. priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
  130. tx_beacon_cmd = priv->beacon_cmd;
  131. if (!tx_beacon_cmd)
  132. return -ENOMEM;
  133. frame_size = priv->beacon_skb->len;
  134. /* Set up TX command fields */
  135. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  136. tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
  137. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  138. tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
  139. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
  140. /* Set up TX beacon command fields */
  141. iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
  142. frame_size);
  143. /* Set up packet rate and flags */
  144. info = IEEE80211_SKB_CB(priv->beacon_skb);
  145. /*
  146. * Let's set up the rate at least somewhat correctly;
  147. * it will currently not actually be used by the uCode,
  148. * it uses the broadcast station's rate instead.
  149. */
  150. if (info->control.rates[0].idx < 0 ||
  151. info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  152. rate = 0;
  153. else
  154. rate = info->control.rates[0].idx;
  155. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  156. hw_params(priv).valid_tx_ant);
  157. rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  158. /* In mac80211, rates for 5 GHz start at 0 */
  159. if (info->band == IEEE80211_BAND_5GHZ)
  160. rate += IWL_FIRST_OFDM_RATE;
  161. else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
  162. rate_flags |= RATE_MCS_CCK_MSK;
  163. tx_beacon_cmd->tx.rate_n_flags =
  164. iwl_hw_set_rate_n_flags(rate, rate_flags);
  165. /* Submit command */
  166. cmd.len[0] = sizeof(*tx_beacon_cmd);
  167. cmd.data[0] = tx_beacon_cmd;
  168. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  169. cmd.len[1] = frame_size;
  170. cmd.data[1] = priv->beacon_skb->data;
  171. cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
  172. return iwl_trans_send_cmd(trans(priv), &cmd);
  173. }
  174. static void iwl_bg_beacon_update(struct work_struct *work)
  175. {
  176. struct iwl_priv *priv =
  177. container_of(work, struct iwl_priv, beacon_update);
  178. struct sk_buff *beacon;
  179. mutex_lock(&priv->shrd->mutex);
  180. if (!priv->beacon_ctx) {
  181. IWL_ERR(priv, "updating beacon w/o beacon context!\n");
  182. goto out;
  183. }
  184. if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
  185. /*
  186. * The ucode will send beacon notifications even in
  187. * IBSS mode, but we don't want to process them. But
  188. * we need to defer the type check to here due to
  189. * requiring locking around the beacon_ctx access.
  190. */
  191. goto out;
  192. }
  193. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  194. beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif);
  195. if (!beacon) {
  196. IWL_ERR(priv, "update beacon failed -- keeping old\n");
  197. goto out;
  198. }
  199. /* new beacon skb is allocated every time; dispose previous.*/
  200. dev_kfree_skb(priv->beacon_skb);
  201. priv->beacon_skb = beacon;
  202. iwlagn_send_beacon_cmd(priv);
  203. out:
  204. mutex_unlock(&priv->shrd->mutex);
  205. }
  206. static void iwl_bg_bt_runtime_config(struct work_struct *work)
  207. {
  208. struct iwl_priv *priv =
  209. container_of(work, struct iwl_priv, bt_runtime_config);
  210. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  211. return;
  212. /* dont send host command if rf-kill is on */
  213. if (!iwl_is_ready_rf(priv->shrd))
  214. return;
  215. iwlagn_send_advance_bt_config(priv);
  216. }
  217. static void iwl_bg_bt_full_concurrency(struct work_struct *work)
  218. {
  219. struct iwl_priv *priv =
  220. container_of(work, struct iwl_priv, bt_full_concurrency);
  221. struct iwl_rxon_context *ctx;
  222. mutex_lock(&priv->shrd->mutex);
  223. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  224. goto out;
  225. /* dont send host command if rf-kill is on */
  226. if (!iwl_is_ready_rf(priv->shrd))
  227. goto out;
  228. IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
  229. priv->bt_full_concurrent ?
  230. "full concurrency" : "3-wire");
  231. /*
  232. * LQ & RXON updated cmds must be sent before BT Config cmd
  233. * to avoid 3-wire collisions
  234. */
  235. for_each_context(priv, ctx) {
  236. iwlagn_set_rxon_chain(priv, ctx);
  237. iwlagn_commit_rxon(priv, ctx);
  238. }
  239. iwlagn_send_advance_bt_config(priv);
  240. out:
  241. mutex_unlock(&priv->shrd->mutex);
  242. }
  243. /**
  244. * iwl_bg_statistics_periodic - Timer callback to queue statistics
  245. *
  246. * This callback is provided in order to send a statistics request.
  247. *
  248. * This timer function is continually reset to execute within
  249. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  250. * was received. We need to ensure we receive the statistics in order
  251. * to update the temperature used for calibrating the TXPOWER.
  252. */
  253. static void iwl_bg_statistics_periodic(unsigned long data)
  254. {
  255. struct iwl_priv *priv = (struct iwl_priv *)data;
  256. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  257. return;
  258. /* dont send host command if rf-kill is on */
  259. if (!iwl_is_ready_rf(priv->shrd))
  260. return;
  261. iwl_send_statistics_request(priv, CMD_ASYNC, false);
  262. }
  263. static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
  264. u32 start_idx, u32 num_events,
  265. u32 mode)
  266. {
  267. u32 i;
  268. u32 ptr; /* SRAM byte address of log data */
  269. u32 ev, time, data; /* event log data */
  270. unsigned long reg_flags;
  271. if (mode == 0)
  272. ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
  273. else
  274. ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
  275. /* Make sure device is powered up for SRAM reads */
  276. spin_lock_irqsave(&bus(priv)->reg_lock, reg_flags);
  277. if (iwl_grab_nic_access(bus(priv))) {
  278. spin_unlock_irqrestore(&bus(priv)->reg_lock, reg_flags);
  279. return;
  280. }
  281. /* Set starting address; reads will auto-increment */
  282. iwl_write32(bus(priv), HBUS_TARG_MEM_RADDR, ptr);
  283. rmb();
  284. /*
  285. * "time" is actually "data" for mode 0 (no timestamp).
  286. * place event id # at far right for easier visual parsing.
  287. */
  288. for (i = 0; i < num_events; i++) {
  289. ev = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
  290. time = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
  291. if (mode == 0) {
  292. trace_iwlwifi_dev_ucode_cont_event(priv,
  293. 0, time, ev);
  294. } else {
  295. data = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
  296. trace_iwlwifi_dev_ucode_cont_event(priv,
  297. time, data, ev);
  298. }
  299. }
  300. /* Allow device to power down */
  301. iwl_release_nic_access(bus(priv));
  302. spin_unlock_irqrestore(&bus(priv)->reg_lock, reg_flags);
  303. }
  304. static void iwl_continuous_event_trace(struct iwl_priv *priv)
  305. {
  306. u32 capacity; /* event log capacity in # entries */
  307. u32 base; /* SRAM byte address of event log header */
  308. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  309. u32 num_wraps; /* # times uCode wrapped to top of log */
  310. u32 next_entry; /* index of next entry to be written by uCode */
  311. base = priv->device_pointers.error_event_table;
  312. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  313. capacity = iwl_read_targ_mem(bus(priv), base);
  314. num_wraps = iwl_read_targ_mem(bus(priv),
  315. base + (2 * sizeof(u32)));
  316. mode = iwl_read_targ_mem(bus(priv), base + (1 * sizeof(u32)));
  317. next_entry = iwl_read_targ_mem(bus(priv),
  318. base + (3 * sizeof(u32)));
  319. } else
  320. return;
  321. if (num_wraps == priv->event_log.num_wraps) {
  322. iwl_print_cont_event_trace(priv,
  323. base, priv->event_log.next_entry,
  324. next_entry - priv->event_log.next_entry,
  325. mode);
  326. priv->event_log.non_wraps_count++;
  327. } else {
  328. if ((num_wraps - priv->event_log.num_wraps) > 1)
  329. priv->event_log.wraps_more_count++;
  330. else
  331. priv->event_log.wraps_once_count++;
  332. trace_iwlwifi_dev_ucode_wrap_event(priv,
  333. num_wraps - priv->event_log.num_wraps,
  334. next_entry, priv->event_log.next_entry);
  335. if (next_entry < priv->event_log.next_entry) {
  336. iwl_print_cont_event_trace(priv, base,
  337. priv->event_log.next_entry,
  338. capacity - priv->event_log.next_entry,
  339. mode);
  340. iwl_print_cont_event_trace(priv, base, 0,
  341. next_entry, mode);
  342. } else {
  343. iwl_print_cont_event_trace(priv, base,
  344. next_entry, capacity - next_entry,
  345. mode);
  346. iwl_print_cont_event_trace(priv, base, 0,
  347. next_entry, mode);
  348. }
  349. }
  350. priv->event_log.num_wraps = num_wraps;
  351. priv->event_log.next_entry = next_entry;
  352. }
  353. /**
  354. * iwl_bg_ucode_trace - Timer callback to log ucode event
  355. *
  356. * The timer is continually set to execute every
  357. * UCODE_TRACE_PERIOD milliseconds after the last timer expired
  358. * this function is to perform continuous uCode event logging operation
  359. * if enabled
  360. */
  361. static void iwl_bg_ucode_trace(unsigned long data)
  362. {
  363. struct iwl_priv *priv = (struct iwl_priv *)data;
  364. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  365. return;
  366. if (priv->event_log.ucode_trace) {
  367. iwl_continuous_event_trace(priv);
  368. /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
  369. mod_timer(&priv->ucode_trace,
  370. jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
  371. }
  372. }
  373. static void iwl_bg_tx_flush(struct work_struct *work)
  374. {
  375. struct iwl_priv *priv =
  376. container_of(work, struct iwl_priv, tx_flush);
  377. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  378. return;
  379. /* do nothing if rf-kill is on */
  380. if (!iwl_is_ready_rf(priv->shrd))
  381. return;
  382. IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
  383. iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
  384. }
  385. /*****************************************************************************
  386. *
  387. * sysfs attributes
  388. *
  389. *****************************************************************************/
  390. #ifdef CONFIG_IWLWIFI_DEBUG
  391. /*
  392. * The following adds a new attribute to the sysfs representation
  393. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  394. * used for controlling the debug level.
  395. *
  396. * See the level definitions in iwl for details.
  397. *
  398. * The debug_level being managed using sysfs below is a per device debug
  399. * level that is used instead of the global debug level if it (the per
  400. * device debug level) is set.
  401. */
  402. static ssize_t show_debug_level(struct device *d,
  403. struct device_attribute *attr, char *buf)
  404. {
  405. struct iwl_shared *shrd = dev_get_drvdata(d);
  406. return sprintf(buf, "0x%08X\n", iwl_get_debug_level(shrd));
  407. }
  408. static ssize_t store_debug_level(struct device *d,
  409. struct device_attribute *attr,
  410. const char *buf, size_t count)
  411. {
  412. struct iwl_shared *shrd = dev_get_drvdata(d);
  413. struct iwl_priv *priv = shrd->priv;
  414. unsigned long val;
  415. int ret;
  416. ret = strict_strtoul(buf, 0, &val);
  417. if (ret)
  418. IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
  419. else {
  420. shrd->dbg_level_dev = val;
  421. if (iwl_alloc_traffic_mem(priv))
  422. IWL_ERR(shrd->priv,
  423. "Not enough memory to generate traffic log\n");
  424. }
  425. return strnlen(buf, count);
  426. }
  427. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  428. show_debug_level, store_debug_level);
  429. #endif /* CONFIG_IWLWIFI_DEBUG */
  430. static ssize_t show_temperature(struct device *d,
  431. struct device_attribute *attr, char *buf)
  432. {
  433. struct iwl_shared *shrd = dev_get_drvdata(d);
  434. struct iwl_priv *priv = shrd->priv;
  435. if (!iwl_is_alive(priv->shrd))
  436. return -EAGAIN;
  437. return sprintf(buf, "%d\n", priv->temperature);
  438. }
  439. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  440. static ssize_t show_tx_power(struct device *d,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct iwl_priv *priv = dev_get_drvdata(d);
  444. if (!iwl_is_ready_rf(priv->shrd))
  445. return sprintf(buf, "off\n");
  446. else
  447. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  448. }
  449. static ssize_t store_tx_power(struct device *d,
  450. struct device_attribute *attr,
  451. const char *buf, size_t count)
  452. {
  453. struct iwl_priv *priv = dev_get_drvdata(d);
  454. unsigned long val;
  455. int ret;
  456. ret = strict_strtoul(buf, 10, &val);
  457. if (ret)
  458. IWL_INFO(priv, "%s is not in decimal form.\n", buf);
  459. else {
  460. ret = iwl_set_tx_power(priv, val, false);
  461. if (ret)
  462. IWL_ERR(priv, "failed setting tx power (0x%d).\n",
  463. ret);
  464. else
  465. ret = count;
  466. }
  467. return ret;
  468. }
  469. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  470. static struct attribute *iwl_sysfs_entries[] = {
  471. &dev_attr_temperature.attr,
  472. &dev_attr_tx_power.attr,
  473. #ifdef CONFIG_IWLWIFI_DEBUG
  474. &dev_attr_debug_level.attr,
  475. #endif
  476. NULL
  477. };
  478. static struct attribute_group iwl_attribute_group = {
  479. .name = NULL, /* put in device directory */
  480. .attrs = iwl_sysfs_entries,
  481. };
  482. /******************************************************************************
  483. *
  484. * uCode download functions
  485. *
  486. ******************************************************************************/
  487. static void iwl_free_fw_desc(struct iwl_priv *priv, struct fw_desc *desc)
  488. {
  489. if (desc->v_addr)
  490. dma_free_coherent(priv->bus->dev, desc->len,
  491. desc->v_addr, desc->p_addr);
  492. desc->v_addr = NULL;
  493. desc->len = 0;
  494. }
  495. static void iwl_free_fw_img(struct iwl_priv *priv, struct fw_img *img)
  496. {
  497. iwl_free_fw_desc(priv, &img->code);
  498. iwl_free_fw_desc(priv, &img->data);
  499. }
  500. static void iwl_dealloc_ucode(struct iwl_priv *priv)
  501. {
  502. iwl_free_fw_img(priv, &priv->ucode_rt);
  503. iwl_free_fw_img(priv, &priv->ucode_init);
  504. iwl_free_fw_img(priv, &priv->ucode_wowlan);
  505. }
  506. static int iwl_alloc_fw_desc(struct iwl_priv *priv, struct fw_desc *desc,
  507. const void *data, size_t len)
  508. {
  509. if (!len) {
  510. desc->v_addr = NULL;
  511. return -EINVAL;
  512. }
  513. desc->v_addr = dma_alloc_coherent(priv->bus->dev, len,
  514. &desc->p_addr, GFP_KERNEL);
  515. if (!desc->v_addr)
  516. return -ENOMEM;
  517. desc->len = len;
  518. memcpy(desc->v_addr, data, len);
  519. return 0;
  520. }
  521. static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
  522. {
  523. int i;
  524. /*
  525. * The default context is always valid,
  526. * the PAN context depends on uCode.
  527. */
  528. priv->shrd->valid_contexts = BIT(IWL_RXON_CTX_BSS);
  529. if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
  530. priv->shrd->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
  531. for (i = 0; i < NUM_IWL_RXON_CTX; i++)
  532. priv->contexts[i].ctxid = i;
  533. priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
  534. priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
  535. priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
  536. priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
  537. priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
  538. priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
  539. priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
  540. priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
  541. priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
  542. BIT(NL80211_IFTYPE_ADHOC);
  543. priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
  544. BIT(NL80211_IFTYPE_STATION);
  545. priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
  546. priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
  547. priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
  548. priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
  549. priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
  550. priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
  551. REPLY_WIPAN_RXON_TIMING;
  552. priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
  553. REPLY_WIPAN_RXON_ASSOC;
  554. priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
  555. priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
  556. priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
  557. priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
  558. priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
  559. priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
  560. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
  561. if (ucode_flags & IWL_UCODE_TLV_FLAGS_P2P)
  562. priv->contexts[IWL_RXON_CTX_PAN].interface_modes |=
  563. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  564. BIT(NL80211_IFTYPE_P2P_GO);
  565. priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
  566. priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
  567. priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
  568. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  569. }
  570. struct iwlagn_ucode_capabilities {
  571. u32 max_probe_length;
  572. u32 standard_phy_calibration_size;
  573. u32 flags;
  574. };
  575. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  576. static int iwl_mac_setup_register(struct iwl_priv *priv,
  577. struct iwlagn_ucode_capabilities *capa);
  578. #define UCODE_EXPERIMENTAL_INDEX 100
  579. #define UCODE_EXPERIMENTAL_TAG "exp"
  580. static int __must_check iwl_request_firmware(struct iwl_priv *priv, bool first)
  581. {
  582. const char *name_pre = priv->cfg->fw_name_pre;
  583. char tag[8];
  584. if (first) {
  585. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  586. priv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  587. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  588. } else if (priv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  589. #endif
  590. priv->fw_index = priv->cfg->ucode_api_max;
  591. sprintf(tag, "%d", priv->fw_index);
  592. } else {
  593. priv->fw_index--;
  594. sprintf(tag, "%d", priv->fw_index);
  595. }
  596. if (priv->fw_index < priv->cfg->ucode_api_min) {
  597. IWL_ERR(priv, "no suitable firmware found!\n");
  598. return -ENOENT;
  599. }
  600. sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  601. IWL_DEBUG_INFO(priv, "attempting to load firmware %s'%s'\n",
  602. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  603. ? "EXPERIMENTAL " : "",
  604. priv->firmware_name);
  605. return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
  606. priv->bus->dev,
  607. GFP_KERNEL, priv, iwl_ucode_callback);
  608. }
  609. struct iwlagn_firmware_pieces {
  610. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  611. size_t inst_size, data_size, init_size, init_data_size,
  612. wowlan_inst_size, wowlan_data_size;
  613. u32 build;
  614. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  615. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  616. };
  617. static int iwlagn_load_legacy_firmware(struct iwl_priv *priv,
  618. const struct firmware *ucode_raw,
  619. struct iwlagn_firmware_pieces *pieces)
  620. {
  621. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  622. u32 api_ver, hdr_size;
  623. const u8 *src;
  624. priv->ucode_ver = le32_to_cpu(ucode->ver);
  625. api_ver = IWL_UCODE_API(priv->ucode_ver);
  626. switch (api_ver) {
  627. default:
  628. hdr_size = 28;
  629. if (ucode_raw->size < hdr_size) {
  630. IWL_ERR(priv, "File size too small!\n");
  631. return -EINVAL;
  632. }
  633. pieces->build = le32_to_cpu(ucode->u.v2.build);
  634. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  635. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  636. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  637. pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
  638. src = ucode->u.v2.data;
  639. break;
  640. case 0:
  641. case 1:
  642. case 2:
  643. hdr_size = 24;
  644. if (ucode_raw->size < hdr_size) {
  645. IWL_ERR(priv, "File size too small!\n");
  646. return -EINVAL;
  647. }
  648. pieces->build = 0;
  649. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  650. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  651. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  652. pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
  653. src = ucode->u.v1.data;
  654. break;
  655. }
  656. /* Verify size of file vs. image size info in file's header */
  657. if (ucode_raw->size != hdr_size + pieces->inst_size +
  658. pieces->data_size + pieces->init_size +
  659. pieces->init_data_size) {
  660. IWL_ERR(priv,
  661. "uCode file size %d does not match expected size\n",
  662. (int)ucode_raw->size);
  663. return -EINVAL;
  664. }
  665. pieces->inst = src;
  666. src += pieces->inst_size;
  667. pieces->data = src;
  668. src += pieces->data_size;
  669. pieces->init = src;
  670. src += pieces->init_size;
  671. pieces->init_data = src;
  672. src += pieces->init_data_size;
  673. return 0;
  674. }
  675. static int iwlagn_load_firmware(struct iwl_priv *priv,
  676. const struct firmware *ucode_raw,
  677. struct iwlagn_firmware_pieces *pieces,
  678. struct iwlagn_ucode_capabilities *capa)
  679. {
  680. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  681. struct iwl_ucode_tlv *tlv;
  682. size_t len = ucode_raw->size;
  683. const u8 *data;
  684. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  685. int tmp;
  686. u64 alternatives;
  687. u32 tlv_len;
  688. enum iwl_ucode_tlv_type tlv_type;
  689. const u8 *tlv_data;
  690. if (len < sizeof(*ucode)) {
  691. IWL_ERR(priv, "uCode has invalid length: %zd\n", len);
  692. return -EINVAL;
  693. }
  694. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  695. IWL_ERR(priv, "invalid uCode magic: 0X%x\n",
  696. le32_to_cpu(ucode->magic));
  697. return -EINVAL;
  698. }
  699. /*
  700. * Check which alternatives are present, and "downgrade"
  701. * when the chosen alternative is not present, warning
  702. * the user when that happens. Some files may not have
  703. * any alternatives, so don't warn in that case.
  704. */
  705. alternatives = le64_to_cpu(ucode->alternatives);
  706. tmp = wanted_alternative;
  707. if (wanted_alternative > 63)
  708. wanted_alternative = 63;
  709. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  710. wanted_alternative--;
  711. if (wanted_alternative && wanted_alternative != tmp)
  712. IWL_WARN(priv,
  713. "uCode alternative %d not available, choosing %d\n",
  714. tmp, wanted_alternative);
  715. priv->ucode_ver = le32_to_cpu(ucode->ver);
  716. pieces->build = le32_to_cpu(ucode->build);
  717. data = ucode->data;
  718. len -= sizeof(*ucode);
  719. while (len >= sizeof(*tlv)) {
  720. u16 tlv_alt;
  721. len -= sizeof(*tlv);
  722. tlv = (void *)data;
  723. tlv_len = le32_to_cpu(tlv->length);
  724. tlv_type = le16_to_cpu(tlv->type);
  725. tlv_alt = le16_to_cpu(tlv->alternative);
  726. tlv_data = tlv->data;
  727. if (len < tlv_len) {
  728. IWL_ERR(priv, "invalid TLV len: %zd/%u\n",
  729. len, tlv_len);
  730. return -EINVAL;
  731. }
  732. len -= ALIGN(tlv_len, 4);
  733. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  734. /*
  735. * Alternative 0 is always valid.
  736. *
  737. * Skip alternative TLVs that are not selected.
  738. */
  739. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  740. continue;
  741. switch (tlv_type) {
  742. case IWL_UCODE_TLV_INST:
  743. pieces->inst = tlv_data;
  744. pieces->inst_size = tlv_len;
  745. break;
  746. case IWL_UCODE_TLV_DATA:
  747. pieces->data = tlv_data;
  748. pieces->data_size = tlv_len;
  749. break;
  750. case IWL_UCODE_TLV_INIT:
  751. pieces->init = tlv_data;
  752. pieces->init_size = tlv_len;
  753. break;
  754. case IWL_UCODE_TLV_INIT_DATA:
  755. pieces->init_data = tlv_data;
  756. pieces->init_data_size = tlv_len;
  757. break;
  758. case IWL_UCODE_TLV_BOOT:
  759. IWL_ERR(priv, "Found unexpected BOOT ucode\n");
  760. break;
  761. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  762. if (tlv_len != sizeof(u32))
  763. goto invalid_tlv_len;
  764. capa->max_probe_length =
  765. le32_to_cpup((__le32 *)tlv_data);
  766. break;
  767. case IWL_UCODE_TLV_PAN:
  768. if (tlv_len)
  769. goto invalid_tlv_len;
  770. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  771. break;
  772. case IWL_UCODE_TLV_FLAGS:
  773. /* must be at least one u32 */
  774. if (tlv_len < sizeof(u32))
  775. goto invalid_tlv_len;
  776. /* and a proper number of u32s */
  777. if (tlv_len % sizeof(u32))
  778. goto invalid_tlv_len;
  779. /*
  780. * This driver only reads the first u32 as
  781. * right now no more features are defined,
  782. * if that changes then either the driver
  783. * will not work with the new firmware, or
  784. * it'll not take advantage of new features.
  785. */
  786. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  787. break;
  788. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  789. if (tlv_len != sizeof(u32))
  790. goto invalid_tlv_len;
  791. pieces->init_evtlog_ptr =
  792. le32_to_cpup((__le32 *)tlv_data);
  793. break;
  794. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  795. if (tlv_len != sizeof(u32))
  796. goto invalid_tlv_len;
  797. pieces->init_evtlog_size =
  798. le32_to_cpup((__le32 *)tlv_data);
  799. break;
  800. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  801. if (tlv_len != sizeof(u32))
  802. goto invalid_tlv_len;
  803. pieces->init_errlog_ptr =
  804. le32_to_cpup((__le32 *)tlv_data);
  805. break;
  806. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  807. if (tlv_len != sizeof(u32))
  808. goto invalid_tlv_len;
  809. pieces->inst_evtlog_ptr =
  810. le32_to_cpup((__le32 *)tlv_data);
  811. break;
  812. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  813. if (tlv_len != sizeof(u32))
  814. goto invalid_tlv_len;
  815. pieces->inst_evtlog_size =
  816. le32_to_cpup((__le32 *)tlv_data);
  817. break;
  818. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  819. if (tlv_len != sizeof(u32))
  820. goto invalid_tlv_len;
  821. pieces->inst_errlog_ptr =
  822. le32_to_cpup((__le32 *)tlv_data);
  823. break;
  824. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  825. if (tlv_len)
  826. goto invalid_tlv_len;
  827. priv->enhance_sensitivity_table = true;
  828. break;
  829. case IWL_UCODE_TLV_WOWLAN_INST:
  830. pieces->wowlan_inst = tlv_data;
  831. pieces->wowlan_inst_size = tlv_len;
  832. break;
  833. case IWL_UCODE_TLV_WOWLAN_DATA:
  834. pieces->wowlan_data = tlv_data;
  835. pieces->wowlan_data_size = tlv_len;
  836. break;
  837. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  838. if (tlv_len != sizeof(u32))
  839. goto invalid_tlv_len;
  840. capa->standard_phy_calibration_size =
  841. le32_to_cpup((__le32 *)tlv_data);
  842. break;
  843. default:
  844. IWL_DEBUG_INFO(priv, "unknown TLV: %d\n", tlv_type);
  845. break;
  846. }
  847. }
  848. if (len) {
  849. IWL_ERR(priv, "invalid TLV after parsing: %zd\n", len);
  850. iwl_print_hex_dump(priv, IWL_DL_FW, (u8 *)data, len);
  851. return -EINVAL;
  852. }
  853. return 0;
  854. invalid_tlv_len:
  855. IWL_ERR(priv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  856. iwl_print_hex_dump(priv, IWL_DL_FW, tlv_data, tlv_len);
  857. return -EINVAL;
  858. }
  859. /**
  860. * iwl_ucode_callback - callback when firmware was loaded
  861. *
  862. * If loaded successfully, copies the firmware into buffers
  863. * for the card to fetch (via DMA).
  864. */
  865. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  866. {
  867. struct iwl_priv *priv = context;
  868. struct iwl_ucode_header *ucode;
  869. int err;
  870. struct iwlagn_firmware_pieces pieces;
  871. const unsigned int api_max = priv->cfg->ucode_api_max;
  872. unsigned int api_ok = priv->cfg->ucode_api_ok;
  873. const unsigned int api_min = priv->cfg->ucode_api_min;
  874. u32 api_ver;
  875. char buildstr[25];
  876. u32 build;
  877. struct iwlagn_ucode_capabilities ucode_capa = {
  878. .max_probe_length = 200,
  879. .standard_phy_calibration_size =
  880. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE,
  881. };
  882. if (!api_ok)
  883. api_ok = api_max;
  884. memset(&pieces, 0, sizeof(pieces));
  885. if (!ucode_raw) {
  886. if (priv->fw_index <= api_ok)
  887. IWL_ERR(priv,
  888. "request for firmware file '%s' failed.\n",
  889. priv->firmware_name);
  890. goto try_again;
  891. }
  892. IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
  893. priv->firmware_name, ucode_raw->size);
  894. /* Make sure that we got at least the API version number */
  895. if (ucode_raw->size < 4) {
  896. IWL_ERR(priv, "File size way too small!\n");
  897. goto try_again;
  898. }
  899. /* Data from ucode file: header followed by uCode images */
  900. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  901. if (ucode->ver)
  902. err = iwlagn_load_legacy_firmware(priv, ucode_raw, &pieces);
  903. else
  904. err = iwlagn_load_firmware(priv, ucode_raw, &pieces,
  905. &ucode_capa);
  906. if (err)
  907. goto try_again;
  908. api_ver = IWL_UCODE_API(priv->ucode_ver);
  909. build = pieces.build;
  910. /*
  911. * api_ver should match the api version forming part of the
  912. * firmware filename ... but we don't check for that and only rely
  913. * on the API version read from firmware header from here on forward
  914. */
  915. /* no api version check required for experimental uCode */
  916. if (priv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  917. if (api_ver < api_min || api_ver > api_max) {
  918. IWL_ERR(priv,
  919. "Driver unable to support your firmware API. "
  920. "Driver supports v%u, firmware is v%u.\n",
  921. api_max, api_ver);
  922. goto try_again;
  923. }
  924. if (api_ver < api_ok) {
  925. if (api_ok != api_max)
  926. IWL_ERR(priv, "Firmware has old API version, "
  927. "expected v%u through v%u, got v%u.\n",
  928. api_ok, api_max, api_ver);
  929. else
  930. IWL_ERR(priv, "Firmware has old API version, "
  931. "expected v%u, got v%u.\n",
  932. api_max, api_ver);
  933. IWL_ERR(priv, "New firmware can be obtained from "
  934. "http://www.intellinuxwireless.org/.\n");
  935. }
  936. }
  937. if (build)
  938. sprintf(buildstr, " build %u%s", build,
  939. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  940. ? " (EXP)" : "");
  941. else
  942. buildstr[0] = '\0';
  943. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u%s\n",
  944. IWL_UCODE_MAJOR(priv->ucode_ver),
  945. IWL_UCODE_MINOR(priv->ucode_ver),
  946. IWL_UCODE_API(priv->ucode_ver),
  947. IWL_UCODE_SERIAL(priv->ucode_ver),
  948. buildstr);
  949. snprintf(priv->hw->wiphy->fw_version,
  950. sizeof(priv->hw->wiphy->fw_version),
  951. "%u.%u.%u.%u%s",
  952. IWL_UCODE_MAJOR(priv->ucode_ver),
  953. IWL_UCODE_MINOR(priv->ucode_ver),
  954. IWL_UCODE_API(priv->ucode_ver),
  955. IWL_UCODE_SERIAL(priv->ucode_ver),
  956. buildstr);
  957. /*
  958. * For any of the failures below (before allocating pci memory)
  959. * we will try to load a version with a smaller API -- maybe the
  960. * user just got a corrupted version of the latest API.
  961. */
  962. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  963. priv->ucode_ver);
  964. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
  965. pieces.inst_size);
  966. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
  967. pieces.data_size);
  968. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
  969. pieces.init_size);
  970. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
  971. pieces.init_data_size);
  972. /* Verify that uCode images will fit in card's SRAM */
  973. if (pieces.inst_size > hw_params(priv).max_inst_size) {
  974. IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
  975. pieces.inst_size);
  976. goto try_again;
  977. }
  978. if (pieces.data_size > hw_params(priv).max_data_size) {
  979. IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
  980. pieces.data_size);
  981. goto try_again;
  982. }
  983. if (pieces.init_size > hw_params(priv).max_inst_size) {
  984. IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
  985. pieces.init_size);
  986. goto try_again;
  987. }
  988. if (pieces.init_data_size > hw_params(priv).max_data_size) {
  989. IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
  990. pieces.init_data_size);
  991. goto try_again;
  992. }
  993. /* Allocate ucode buffers for card's bus-master loading ... */
  994. /* Runtime instructions and 2 copies of data:
  995. * 1) unmodified from disk
  996. * 2) backup cache for save/restore during power-downs */
  997. if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.code,
  998. pieces.inst, pieces.inst_size))
  999. goto err_pci_alloc;
  1000. if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.data,
  1001. pieces.data, pieces.data_size))
  1002. goto err_pci_alloc;
  1003. /* Initialization instructions and data */
  1004. if (pieces.init_size && pieces.init_data_size) {
  1005. if (iwl_alloc_fw_desc(priv, &priv->ucode_init.code,
  1006. pieces.init, pieces.init_size))
  1007. goto err_pci_alloc;
  1008. if (iwl_alloc_fw_desc(priv, &priv->ucode_init.data,
  1009. pieces.init_data, pieces.init_data_size))
  1010. goto err_pci_alloc;
  1011. }
  1012. /* WoWLAN instructions and data */
  1013. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  1014. if (iwl_alloc_fw_desc(priv, &priv->ucode_wowlan.code,
  1015. pieces.wowlan_inst,
  1016. pieces.wowlan_inst_size))
  1017. goto err_pci_alloc;
  1018. if (iwl_alloc_fw_desc(priv, &priv->ucode_wowlan.data,
  1019. pieces.wowlan_data,
  1020. pieces.wowlan_data_size))
  1021. goto err_pci_alloc;
  1022. }
  1023. /* Now that we can no longer fail, copy information */
  1024. /*
  1025. * The (size - 16) / 12 formula is based on the information recorded
  1026. * for each event, which is of mode 1 (including timestamp) for all
  1027. * new microcodes that include this information.
  1028. */
  1029. priv->init_evtlog_ptr = pieces.init_evtlog_ptr;
  1030. if (pieces.init_evtlog_size)
  1031. priv->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  1032. else
  1033. priv->init_evtlog_size =
  1034. priv->cfg->base_params->max_event_log_size;
  1035. priv->init_errlog_ptr = pieces.init_errlog_ptr;
  1036. priv->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  1037. if (pieces.inst_evtlog_size)
  1038. priv->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  1039. else
  1040. priv->inst_evtlog_size =
  1041. priv->cfg->base_params->max_event_log_size;
  1042. priv->inst_errlog_ptr = pieces.inst_errlog_ptr;
  1043. priv->new_scan_threshold_behaviour =
  1044. !!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
  1045. if (!(priv->cfg->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
  1046. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1047. /*
  1048. * if not PAN, then don't support P2P -- might be a uCode
  1049. * packaging bug or due to the eeprom check above
  1050. */
  1051. if (!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
  1052. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
  1053. if (ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
  1054. priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
  1055. priv->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
  1056. } else {
  1057. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  1058. priv->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  1059. }
  1060. /*
  1061. * figure out the offset of chain noise reset and gain commands
  1062. * base on the size of standard phy calibration commands table size
  1063. */
  1064. if (ucode_capa.standard_phy_calibration_size >
  1065. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1066. ucode_capa.standard_phy_calibration_size =
  1067. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1068. priv->phy_calib_chain_noise_reset_cmd =
  1069. ucode_capa.standard_phy_calibration_size;
  1070. priv->phy_calib_chain_noise_gain_cmd =
  1071. ucode_capa.standard_phy_calibration_size + 1;
  1072. /* initialize all valid contexts */
  1073. iwl_init_context(priv, ucode_capa.flags);
  1074. /**************************************************
  1075. * This is still part of probe() in a sense...
  1076. *
  1077. * 9. Setup and register with mac80211 and debugfs
  1078. **************************************************/
  1079. err = iwl_mac_setup_register(priv, &ucode_capa);
  1080. if (err)
  1081. goto out_unbind;
  1082. err = iwl_dbgfs_register(priv, DRV_NAME);
  1083. if (err)
  1084. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  1085. err = sysfs_create_group(&(priv->bus->dev->kobj),
  1086. &iwl_attribute_group);
  1087. if (err) {
  1088. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  1089. goto out_unbind;
  1090. }
  1091. /* We have our copies now, allow OS release its copies */
  1092. release_firmware(ucode_raw);
  1093. complete(&priv->firmware_loading_complete);
  1094. return;
  1095. try_again:
  1096. /* try next, if any */
  1097. if (iwl_request_firmware(priv, false))
  1098. goto out_unbind;
  1099. release_firmware(ucode_raw);
  1100. return;
  1101. err_pci_alloc:
  1102. IWL_ERR(priv, "failed to allocate pci memory\n");
  1103. iwl_dealloc_ucode(priv);
  1104. out_unbind:
  1105. complete(&priv->firmware_loading_complete);
  1106. device_release_driver(priv->bus->dev);
  1107. release_firmware(ucode_raw);
  1108. }
  1109. static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
  1110. {
  1111. struct iwl_ct_kill_config cmd;
  1112. struct iwl_ct_kill_throttling_config adv_cmd;
  1113. unsigned long flags;
  1114. int ret = 0;
  1115. spin_lock_irqsave(&priv->shrd->lock, flags);
  1116. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
  1117. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  1118. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  1119. priv->thermal_throttle.ct_kill_toggle = false;
  1120. if (priv->cfg->base_params->support_ct_kill_exit) {
  1121. adv_cmd.critical_temperature_enter =
  1122. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1123. adv_cmd.critical_temperature_exit =
  1124. cpu_to_le32(hw_params(priv).ct_kill_exit_threshold);
  1125. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1126. REPLY_CT_KILL_CONFIG_CMD,
  1127. CMD_SYNC, sizeof(adv_cmd), &adv_cmd);
  1128. if (ret)
  1129. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1130. else
  1131. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1132. "succeeded, critical temperature enter is %d,"
  1133. "exit is %d\n",
  1134. hw_params(priv).ct_kill_threshold,
  1135. hw_params(priv).ct_kill_exit_threshold);
  1136. } else {
  1137. cmd.critical_temperature_R =
  1138. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1139. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1140. REPLY_CT_KILL_CONFIG_CMD,
  1141. CMD_SYNC, sizeof(cmd), &cmd);
  1142. if (ret)
  1143. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1144. else
  1145. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1146. "succeeded, "
  1147. "critical temperature is %d\n",
  1148. hw_params(priv).ct_kill_threshold);
  1149. }
  1150. }
  1151. static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
  1152. {
  1153. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  1154. struct iwl_host_cmd cmd = {
  1155. .id = CALIBRATION_CFG_CMD,
  1156. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  1157. .data = { &calib_cfg_cmd, },
  1158. };
  1159. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  1160. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  1161. calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
  1162. return iwl_trans_send_cmd(trans(priv), &cmd);
  1163. }
  1164. static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
  1165. {
  1166. struct iwl_tx_ant_config_cmd tx_ant_cmd = {
  1167. .valid = cpu_to_le32(valid_tx_ant),
  1168. };
  1169. if (IWL_UCODE_API(priv->ucode_ver) > 1) {
  1170. IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
  1171. return iwl_trans_send_cmd_pdu(trans(priv),
  1172. TX_ANT_CONFIGURATION_CMD,
  1173. CMD_SYNC,
  1174. sizeof(struct iwl_tx_ant_config_cmd),
  1175. &tx_ant_cmd);
  1176. } else {
  1177. IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
  1178. return -EOPNOTSUPP;
  1179. }
  1180. }
  1181. /**
  1182. * iwl_alive_start - called after REPLY_ALIVE notification received
  1183. * from protocol/runtime uCode (initialization uCode's
  1184. * Alive gets handled by iwl_init_alive_start()).
  1185. */
  1186. int iwl_alive_start(struct iwl_priv *priv)
  1187. {
  1188. int ret = 0;
  1189. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1190. /*TODO: this should go to the transport layer */
  1191. iwl_reset_ict(trans(priv));
  1192. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1193. /* After the ALIVE response, we can send host commands to the uCode */
  1194. set_bit(STATUS_ALIVE, &priv->shrd->status);
  1195. /* Enable watchdog to monitor the driver tx queues */
  1196. iwl_setup_watchdog(priv);
  1197. if (iwl_is_rfkill(priv->shrd))
  1198. return -ERFKILL;
  1199. /* download priority table before any calibration request */
  1200. if (priv->cfg->bt_params &&
  1201. priv->cfg->bt_params->advanced_bt_coexist) {
  1202. /* Configure Bluetooth device coexistence support */
  1203. if (priv->cfg->bt_params->bt_sco_disable)
  1204. priv->bt_enable_pspoll = false;
  1205. else
  1206. priv->bt_enable_pspoll = true;
  1207. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  1208. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  1209. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  1210. iwlagn_send_advance_bt_config(priv);
  1211. priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
  1212. priv->cur_rssi_ctx = NULL;
  1213. iwlagn_send_prio_tbl(priv);
  1214. /* FIXME: w/a to force change uCode BT state machine */
  1215. ret = iwlagn_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
  1216. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1217. if (ret)
  1218. return ret;
  1219. ret = iwlagn_send_bt_env(priv, IWL_BT_COEX_ENV_CLOSE,
  1220. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1221. if (ret)
  1222. return ret;
  1223. } else {
  1224. /*
  1225. * default is 2-wire BT coexexistence support
  1226. */
  1227. iwl_send_bt_config(priv);
  1228. }
  1229. if (hw_params(priv).calib_rt_cfg)
  1230. iwlagn_send_calib_cfg_rt(priv,
  1231. hw_params(priv).calib_rt_cfg);
  1232. ieee80211_wake_queues(priv->hw);
  1233. priv->active_rate = IWL_RATES_MASK;
  1234. /* Configure Tx antenna selection based on H/W config */
  1235. iwlagn_send_tx_ant_config(priv, priv->cfg->valid_tx_ant);
  1236. if (iwl_is_associated_ctx(ctx) && !priv->shrd->wowlan) {
  1237. struct iwl_rxon_cmd *active_rxon =
  1238. (struct iwl_rxon_cmd *)&ctx->active;
  1239. /* apply any changes in staging */
  1240. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1241. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1242. } else {
  1243. struct iwl_rxon_context *tmp;
  1244. /* Initialize our rx_config data */
  1245. for_each_context(priv, tmp)
  1246. iwl_connection_init_rx_config(priv, tmp);
  1247. iwlagn_set_rxon_chain(priv, ctx);
  1248. }
  1249. if (!priv->shrd->wowlan) {
  1250. /* WoWLAN ucode will not reply in the same way, skip it */
  1251. iwl_reset_run_time_calib(priv);
  1252. }
  1253. set_bit(STATUS_READY, &priv->shrd->status);
  1254. /* Configure the adapter for unassociated operation */
  1255. ret = iwlagn_commit_rxon(priv, ctx);
  1256. if (ret)
  1257. return ret;
  1258. /* At this point, the NIC is initialized and operational */
  1259. iwl_rf_kill_ct_config(priv);
  1260. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  1261. return iwl_power_update_mode(priv, true);
  1262. }
  1263. static void iwl_cancel_deferred_work(struct iwl_priv *priv);
  1264. static void __iwl_down(struct iwl_priv *priv)
  1265. {
  1266. int exit_pending;
  1267. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  1268. iwl_scan_cancel_timeout(priv, 200);
  1269. /*
  1270. * If active, scanning won't cancel it, so say it expired.
  1271. * No race since we hold the mutex here and a new one
  1272. * can't come in at this time.
  1273. */
  1274. ieee80211_remain_on_channel_expired(priv->hw);
  1275. exit_pending =
  1276. test_and_set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1277. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  1278. * to prevent rearm timer */
  1279. del_timer_sync(&priv->watchdog);
  1280. iwl_clear_ucode_stations(priv, NULL);
  1281. iwl_dealloc_bcast_stations(priv);
  1282. iwl_clear_driver_stations(priv);
  1283. /* reset BT coex data */
  1284. priv->bt_status = 0;
  1285. priv->cur_rssi_ctx = NULL;
  1286. priv->bt_is_sco = 0;
  1287. if (priv->cfg->bt_params)
  1288. priv->bt_traffic_load =
  1289. priv->cfg->bt_params->bt_init_traffic_load;
  1290. else
  1291. priv->bt_traffic_load = 0;
  1292. priv->bt_full_concurrent = false;
  1293. priv->bt_ci_compliance = 0;
  1294. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1295. * exiting the module */
  1296. if (!exit_pending)
  1297. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1298. if (priv->shrd->mac80211_registered)
  1299. ieee80211_stop_queues(priv->hw);
  1300. /* Clear out all status bits but a few that are stable across reset */
  1301. priv->shrd->status &=
  1302. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status) <<
  1303. STATUS_RF_KILL_HW |
  1304. test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) <<
  1305. STATUS_GEO_CONFIGURED |
  1306. test_bit(STATUS_FW_ERROR, &priv->shrd->status) <<
  1307. STATUS_FW_ERROR |
  1308. test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) <<
  1309. STATUS_EXIT_PENDING;
  1310. iwl_trans_stop_device(trans(priv));
  1311. dev_kfree_skb(priv->beacon_skb);
  1312. priv->beacon_skb = NULL;
  1313. }
  1314. static void iwl_down(struct iwl_priv *priv)
  1315. {
  1316. mutex_lock(&priv->shrd->mutex);
  1317. __iwl_down(priv);
  1318. mutex_unlock(&priv->shrd->mutex);
  1319. iwl_cancel_deferred_work(priv);
  1320. }
  1321. #define MAX_HW_RESTARTS 5
  1322. static int __iwl_up(struct iwl_priv *priv)
  1323. {
  1324. struct iwl_rxon_context *ctx;
  1325. int ret;
  1326. lockdep_assert_held(&priv->shrd->mutex);
  1327. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  1328. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  1329. return -EIO;
  1330. }
  1331. for_each_context(priv, ctx) {
  1332. ret = iwlagn_alloc_bcast_station(priv, ctx);
  1333. if (ret) {
  1334. iwl_dealloc_bcast_stations(priv);
  1335. return ret;
  1336. }
  1337. }
  1338. ret = iwlagn_run_init_ucode(priv);
  1339. if (ret) {
  1340. IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
  1341. goto error;
  1342. }
  1343. ret = iwlagn_load_ucode_wait_alive(priv,
  1344. &priv->ucode_rt,
  1345. IWL_UCODE_REGULAR);
  1346. if (ret) {
  1347. IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
  1348. goto error;
  1349. }
  1350. ret = iwl_alive_start(priv);
  1351. if (ret)
  1352. goto error;
  1353. return 0;
  1354. error:
  1355. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1356. __iwl_down(priv);
  1357. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1358. IWL_ERR(priv, "Unable to initialize device.\n");
  1359. return ret;
  1360. }
  1361. /*****************************************************************************
  1362. *
  1363. * Workqueue callbacks
  1364. *
  1365. *****************************************************************************/
  1366. static void iwl_bg_run_time_calib_work(struct work_struct *work)
  1367. {
  1368. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  1369. run_time_calib_work);
  1370. mutex_lock(&priv->shrd->mutex);
  1371. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  1372. test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  1373. mutex_unlock(&priv->shrd->mutex);
  1374. return;
  1375. }
  1376. if (priv->start_calib) {
  1377. iwl_chain_noise_calibration(priv);
  1378. iwl_sensitivity_calibration(priv);
  1379. }
  1380. mutex_unlock(&priv->shrd->mutex);
  1381. }
  1382. static void iwlagn_prepare_restart(struct iwl_priv *priv)
  1383. {
  1384. struct iwl_rxon_context *ctx;
  1385. bool bt_full_concurrent;
  1386. u8 bt_ci_compliance;
  1387. u8 bt_load;
  1388. u8 bt_status;
  1389. bool bt_is_sco;
  1390. lockdep_assert_held(&priv->shrd->mutex);
  1391. for_each_context(priv, ctx)
  1392. ctx->vif = NULL;
  1393. priv->is_open = 0;
  1394. /*
  1395. * __iwl_down() will clear the BT status variables,
  1396. * which is correct, but when we restart we really
  1397. * want to keep them so restore them afterwards.
  1398. *
  1399. * The restart process will later pick them up and
  1400. * re-configure the hw when we reconfigure the BT
  1401. * command.
  1402. */
  1403. bt_full_concurrent = priv->bt_full_concurrent;
  1404. bt_ci_compliance = priv->bt_ci_compliance;
  1405. bt_load = priv->bt_traffic_load;
  1406. bt_status = priv->bt_status;
  1407. bt_is_sco = priv->bt_is_sco;
  1408. __iwl_down(priv);
  1409. priv->bt_full_concurrent = bt_full_concurrent;
  1410. priv->bt_ci_compliance = bt_ci_compliance;
  1411. priv->bt_traffic_load = bt_load;
  1412. priv->bt_status = bt_status;
  1413. priv->bt_is_sco = bt_is_sco;
  1414. }
  1415. static void iwl_bg_restart(struct work_struct *data)
  1416. {
  1417. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  1418. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1419. return;
  1420. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->shrd->status)) {
  1421. mutex_lock(&priv->shrd->mutex);
  1422. iwlagn_prepare_restart(priv);
  1423. mutex_unlock(&priv->shrd->mutex);
  1424. iwl_cancel_deferred_work(priv);
  1425. ieee80211_restart_hw(priv->hw);
  1426. } else {
  1427. WARN_ON(1);
  1428. }
  1429. }
  1430. /*****************************************************************************
  1431. *
  1432. * mac80211 entry point functions
  1433. *
  1434. *****************************************************************************/
  1435. static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  1436. {
  1437. .max = 1,
  1438. .types = BIT(NL80211_IFTYPE_STATION),
  1439. },
  1440. {
  1441. .max = 1,
  1442. .types = BIT(NL80211_IFTYPE_AP),
  1443. },
  1444. };
  1445. static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  1446. {
  1447. .max = 2,
  1448. .types = BIT(NL80211_IFTYPE_STATION),
  1449. },
  1450. };
  1451. static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
  1452. {
  1453. .max = 1,
  1454. .types = BIT(NL80211_IFTYPE_STATION),
  1455. },
  1456. {
  1457. .max = 1,
  1458. .types = BIT(NL80211_IFTYPE_P2P_GO) |
  1459. BIT(NL80211_IFTYPE_AP),
  1460. },
  1461. };
  1462. static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
  1463. {
  1464. .max = 2,
  1465. .types = BIT(NL80211_IFTYPE_STATION),
  1466. },
  1467. {
  1468. .max = 1,
  1469. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  1470. },
  1471. };
  1472. static const struct ieee80211_iface_combination
  1473. iwlagn_iface_combinations_dualmode[] = {
  1474. { .num_different_channels = 1,
  1475. .max_interfaces = 2,
  1476. .beacon_int_infra_match = true,
  1477. .limits = iwlagn_sta_ap_limits,
  1478. .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
  1479. },
  1480. { .num_different_channels = 1,
  1481. .max_interfaces = 2,
  1482. .limits = iwlagn_2sta_limits,
  1483. .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
  1484. },
  1485. };
  1486. static const struct ieee80211_iface_combination
  1487. iwlagn_iface_combinations_p2p[] = {
  1488. { .num_different_channels = 1,
  1489. .max_interfaces = 2,
  1490. .beacon_int_infra_match = true,
  1491. .limits = iwlagn_p2p_sta_go_limits,
  1492. .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
  1493. },
  1494. { .num_different_channels = 1,
  1495. .max_interfaces = 2,
  1496. .limits = iwlagn_p2p_2sta_limits,
  1497. .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
  1498. },
  1499. };
  1500. /*
  1501. * Not a mac80211 entry point function, but it fits in with all the
  1502. * other mac80211 functions grouped here.
  1503. */
  1504. static int iwl_mac_setup_register(struct iwl_priv *priv,
  1505. struct iwlagn_ucode_capabilities *capa)
  1506. {
  1507. int ret;
  1508. struct ieee80211_hw *hw = priv->hw;
  1509. struct iwl_rxon_context *ctx;
  1510. hw->rate_control_algorithm = "iwl-agn-rs";
  1511. /* Tell mac80211 our characteristics */
  1512. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1513. IEEE80211_HW_AMPDU_AGGREGATION |
  1514. IEEE80211_HW_NEED_DTIM_PERIOD |
  1515. IEEE80211_HW_SPECTRUM_MGMT |
  1516. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  1517. hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  1518. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  1519. IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  1520. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  1521. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1522. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  1523. if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
  1524. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1525. hw->sta_data_size = sizeof(struct iwl_station_priv);
  1526. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  1527. for_each_context(priv, ctx) {
  1528. hw->wiphy->interface_modes |= ctx->interface_modes;
  1529. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  1530. }
  1531. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1532. if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
  1533. hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
  1534. hw->wiphy->n_iface_combinations =
  1535. ARRAY_SIZE(iwlagn_iface_combinations_p2p);
  1536. } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  1537. hw->wiphy->iface_combinations = iwlagn_iface_combinations_dualmode;
  1538. hw->wiphy->n_iface_combinations =
  1539. ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
  1540. }
  1541. hw->wiphy->max_remain_on_channel_duration = 1000;
  1542. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  1543. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  1544. WIPHY_FLAG_IBSS_RSN;
  1545. if (priv->ucode_wowlan.code.len && device_can_wakeup(priv->bus->dev)) {
  1546. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  1547. WIPHY_WOWLAN_DISCONNECT |
  1548. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  1549. WIPHY_WOWLAN_RFKILL_RELEASE;
  1550. if (!iwlagn_mod_params.sw_crypto)
  1551. hw->wiphy->wowlan.flags |=
  1552. WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  1553. WIPHY_WOWLAN_GTK_REKEY_FAILURE;
  1554. hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
  1555. hw->wiphy->wowlan.pattern_min_len =
  1556. IWLAGN_WOWLAN_MIN_PATTERN_LEN;
  1557. hw->wiphy->wowlan.pattern_max_len =
  1558. IWLAGN_WOWLAN_MAX_PATTERN_LEN;
  1559. }
  1560. if (iwlagn_mod_params.power_save)
  1561. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  1562. else
  1563. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  1564. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  1565. /* we create the 802.11 header and a zero-length SSID element */
  1566. hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
  1567. /* Default value; 4 EDCA QOS priorities */
  1568. hw->queues = 4;
  1569. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  1570. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  1571. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1572. &priv->bands[IEEE80211_BAND_2GHZ];
  1573. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  1574. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1575. &priv->bands[IEEE80211_BAND_5GHZ];
  1576. iwl_leds_init(priv);
  1577. ret = ieee80211_register_hw(priv->hw);
  1578. if (ret) {
  1579. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  1580. return ret;
  1581. }
  1582. priv->shrd->mac80211_registered = 1;
  1583. return 0;
  1584. }
  1585. static int iwlagn_mac_start(struct ieee80211_hw *hw)
  1586. {
  1587. struct iwl_priv *priv = hw->priv;
  1588. int ret;
  1589. IWL_DEBUG_MAC80211(priv, "enter\n");
  1590. /* we should be verifying the device is ready to be opened */
  1591. mutex_lock(&priv->shrd->mutex);
  1592. ret = __iwl_up(priv);
  1593. mutex_unlock(&priv->shrd->mutex);
  1594. if (ret)
  1595. return ret;
  1596. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  1597. /* Now we should be done, and the READY bit should be set. */
  1598. if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
  1599. ret = -EIO;
  1600. iwlagn_led_enable(priv);
  1601. priv->is_open = 1;
  1602. IWL_DEBUG_MAC80211(priv, "leave\n");
  1603. return 0;
  1604. }
  1605. static void iwlagn_mac_stop(struct ieee80211_hw *hw)
  1606. {
  1607. struct iwl_priv *priv = hw->priv;
  1608. IWL_DEBUG_MAC80211(priv, "enter\n");
  1609. if (!priv->is_open)
  1610. return;
  1611. priv->is_open = 0;
  1612. iwl_down(priv);
  1613. flush_workqueue(priv->shrd->workqueue);
  1614. /* User space software may expect getting rfkill changes
  1615. * even if interface is down */
  1616. iwl_write32(bus(priv), CSR_INT, 0xFFFFFFFF);
  1617. iwl_enable_rfkill_int(priv);
  1618. IWL_DEBUG_MAC80211(priv, "leave\n");
  1619. }
  1620. #ifdef CONFIG_PM
  1621. static int iwlagn_send_patterns(struct iwl_priv *priv,
  1622. struct cfg80211_wowlan *wowlan)
  1623. {
  1624. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  1625. struct iwl_host_cmd cmd = {
  1626. .id = REPLY_WOWLAN_PATTERNS,
  1627. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1628. .flags = CMD_SYNC,
  1629. };
  1630. int i, err;
  1631. if (!wowlan->n_patterns)
  1632. return 0;
  1633. cmd.len[0] = sizeof(*pattern_cmd) +
  1634. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  1635. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  1636. if (!pattern_cmd)
  1637. return -ENOMEM;
  1638. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  1639. for (i = 0; i < wowlan->n_patterns; i++) {
  1640. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  1641. memcpy(&pattern_cmd->patterns[i].mask,
  1642. wowlan->patterns[i].mask, mask_len);
  1643. memcpy(&pattern_cmd->patterns[i].pattern,
  1644. wowlan->patterns[i].pattern,
  1645. wowlan->patterns[i].pattern_len);
  1646. pattern_cmd->patterns[i].mask_size = mask_len;
  1647. pattern_cmd->patterns[i].pattern_size =
  1648. wowlan->patterns[i].pattern_len;
  1649. }
  1650. cmd.data[0] = pattern_cmd;
  1651. err = iwl_trans_send_cmd(trans(priv), &cmd);
  1652. kfree(pattern_cmd);
  1653. return err;
  1654. }
  1655. #endif
  1656. static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
  1657. struct ieee80211_vif *vif,
  1658. struct cfg80211_gtk_rekey_data *data)
  1659. {
  1660. struct iwl_priv *priv = hw->priv;
  1661. if (iwlagn_mod_params.sw_crypto)
  1662. return;
  1663. mutex_lock(&priv->shrd->mutex);
  1664. if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
  1665. goto out;
  1666. memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
  1667. memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
  1668. priv->replay_ctr = cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  1669. priv->have_rekey_data = true;
  1670. out:
  1671. mutex_unlock(&priv->shrd->mutex);
  1672. }
  1673. struct wowlan_key_data {
  1674. struct iwl_rxon_context *ctx;
  1675. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  1676. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  1677. const u8 *bssid;
  1678. bool error, use_rsc_tsc, use_tkip;
  1679. };
  1680. #ifdef CONFIG_PM
  1681. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  1682. {
  1683. int i;
  1684. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  1685. out[i] = cpu_to_le16(p1k[i]);
  1686. }
  1687. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  1688. struct ieee80211_vif *vif,
  1689. struct ieee80211_sta *sta,
  1690. struct ieee80211_key_conf *key,
  1691. void *_data)
  1692. {
  1693. struct iwl_priv *priv = hw->priv;
  1694. struct wowlan_key_data *data = _data;
  1695. struct iwl_rxon_context *ctx = data->ctx;
  1696. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  1697. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  1698. struct iwlagn_p1k_cache *rx_p1ks;
  1699. u8 *rx_mic_key;
  1700. struct ieee80211_key_seq seq;
  1701. u32 cur_rx_iv32 = 0;
  1702. u16 p1k[IWLAGN_P1K_SIZE];
  1703. int ret, i;
  1704. mutex_lock(&priv->shrd->mutex);
  1705. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1706. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  1707. !sta && !ctx->key_mapping_keys)
  1708. ret = iwl_set_default_wep_key(priv, ctx, key);
  1709. else
  1710. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  1711. if (ret) {
  1712. IWL_ERR(priv, "Error setting key during suspend!\n");
  1713. data->error = true;
  1714. }
  1715. switch (key->cipher) {
  1716. case WLAN_CIPHER_SUITE_TKIP:
  1717. if (sta) {
  1718. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  1719. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  1720. rx_p1ks = data->tkip->rx_uni;
  1721. ieee80211_get_key_tx_seq(key, &seq);
  1722. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  1723. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  1724. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  1725. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  1726. memcpy(data->tkip->mic_keys.tx,
  1727. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  1728. IWLAGN_MIC_KEY_SIZE);
  1729. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  1730. } else {
  1731. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  1732. rx_p1ks = data->tkip->rx_multi;
  1733. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  1734. }
  1735. /*
  1736. * For non-QoS this relies on the fact that both the uCode and
  1737. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  1738. * for checking the IV in the frames.
  1739. */
  1740. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  1741. ieee80211_get_key_rx_seq(key, i, &seq);
  1742. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  1743. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  1744. /* wrapping isn't allowed, AP must rekey */
  1745. if (seq.tkip.iv32 > cur_rx_iv32)
  1746. cur_rx_iv32 = seq.tkip.iv32;
  1747. }
  1748. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  1749. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  1750. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  1751. cur_rx_iv32 + 1, p1k);
  1752. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  1753. memcpy(rx_mic_key,
  1754. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  1755. IWLAGN_MIC_KEY_SIZE);
  1756. data->use_tkip = true;
  1757. data->use_rsc_tsc = true;
  1758. break;
  1759. case WLAN_CIPHER_SUITE_CCMP:
  1760. if (sta) {
  1761. u8 *pn = seq.ccmp.pn;
  1762. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  1763. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  1764. ieee80211_get_key_tx_seq(key, &seq);
  1765. aes_tx_sc->pn = cpu_to_le64(
  1766. (u64)pn[5] |
  1767. ((u64)pn[4] << 8) |
  1768. ((u64)pn[3] << 16) |
  1769. ((u64)pn[2] << 24) |
  1770. ((u64)pn[1] << 32) |
  1771. ((u64)pn[0] << 40));
  1772. } else
  1773. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  1774. /*
  1775. * For non-QoS this relies on the fact that both the uCode and
  1776. * mac80211 use TID 0 for checking the IV in the frames.
  1777. */
  1778. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  1779. u8 *pn = seq.ccmp.pn;
  1780. ieee80211_get_key_rx_seq(key, i, &seq);
  1781. aes_sc->pn = cpu_to_le64(
  1782. (u64)pn[5] |
  1783. ((u64)pn[4] << 8) |
  1784. ((u64)pn[3] << 16) |
  1785. ((u64)pn[2] << 24) |
  1786. ((u64)pn[1] << 32) |
  1787. ((u64)pn[0] << 40));
  1788. }
  1789. data->use_rsc_tsc = true;
  1790. break;
  1791. }
  1792. mutex_unlock(&priv->shrd->mutex);
  1793. }
  1794. static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
  1795. struct cfg80211_wowlan *wowlan)
  1796. {
  1797. struct iwl_priv *priv = hw->priv;
  1798. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  1799. struct iwl_rxon_cmd rxon;
  1800. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1801. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  1802. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  1803. struct wowlan_key_data key_data = {
  1804. .ctx = ctx,
  1805. .bssid = ctx->active.bssid_addr,
  1806. .use_rsc_tsc = false,
  1807. .tkip = &tkip_cmd,
  1808. .use_tkip = false,
  1809. };
  1810. int ret, i;
  1811. u16 seq;
  1812. if (WARN_ON(!wowlan))
  1813. return -EINVAL;
  1814. mutex_lock(&priv->shrd->mutex);
  1815. /* Don't attempt WoWLAN when not associated, tear down instead. */
  1816. if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
  1817. !iwl_is_associated_ctx(ctx)) {
  1818. ret = 1;
  1819. goto out;
  1820. }
  1821. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  1822. if (!key_data.rsc_tsc) {
  1823. ret = -ENOMEM;
  1824. goto out;
  1825. }
  1826. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  1827. /*
  1828. * We know the last used seqno, and the uCode expects to know that
  1829. * one, it will increment before TX.
  1830. */
  1831. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  1832. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  1833. /*
  1834. * For QoS counters, we store the one to use next, so subtract 0x10
  1835. * since the uCode will add 0x10 before using the value.
  1836. */
  1837. for (i = 0; i < 8; i++) {
  1838. seq = priv->shrd->tid_data[IWL_AP_ID][i].seq_number;
  1839. seq -= 0x10;
  1840. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  1841. }
  1842. if (wowlan->disconnect)
  1843. wakeup_filter_cmd.enabled |=
  1844. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  1845. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  1846. if (wowlan->magic_pkt)
  1847. wakeup_filter_cmd.enabled |=
  1848. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  1849. if (wowlan->gtk_rekey_failure)
  1850. wakeup_filter_cmd.enabled |=
  1851. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  1852. if (wowlan->eap_identity_req)
  1853. wakeup_filter_cmd.enabled |=
  1854. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  1855. if (wowlan->four_way_handshake)
  1856. wakeup_filter_cmd.enabled |=
  1857. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  1858. if (wowlan->rfkill_release)
  1859. wakeup_filter_cmd.enabled |=
  1860. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_RFKILL);
  1861. if (wowlan->n_patterns)
  1862. wakeup_filter_cmd.enabled |=
  1863. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  1864. iwl_scan_cancel_timeout(priv, 200);
  1865. memcpy(&rxon, &ctx->active, sizeof(rxon));
  1866. iwl_trans_stop_device(trans(priv));
  1867. priv->shrd->wowlan = true;
  1868. ret = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_wowlan,
  1869. IWL_UCODE_WOWLAN);
  1870. if (ret)
  1871. goto error;
  1872. /* now configure WoWLAN ucode */
  1873. ret = iwl_alive_start(priv);
  1874. if (ret)
  1875. goto error;
  1876. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  1877. ret = iwlagn_commit_rxon(priv, ctx);
  1878. if (ret)
  1879. goto error;
  1880. ret = iwl_power_update_mode(priv, true);
  1881. if (ret)
  1882. goto error;
  1883. if (!iwlagn_mod_params.sw_crypto) {
  1884. /* mark all keys clear */
  1885. priv->ucode_key_table = 0;
  1886. ctx->key_mapping_keys = 0;
  1887. /*
  1888. * This needs to be unlocked due to lock ordering
  1889. * constraints. Since we're in the suspend path
  1890. * that isn't really a problem though.
  1891. */
  1892. mutex_unlock(&priv->shrd->mutex);
  1893. ieee80211_iter_keys(priv->hw, ctx->vif,
  1894. iwlagn_wowlan_program_keys,
  1895. &key_data);
  1896. mutex_lock(&priv->shrd->mutex);
  1897. if (key_data.error) {
  1898. ret = -EIO;
  1899. goto error;
  1900. }
  1901. if (key_data.use_rsc_tsc) {
  1902. struct iwl_host_cmd rsc_tsc_cmd = {
  1903. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  1904. .flags = CMD_SYNC,
  1905. .data[0] = key_data.rsc_tsc,
  1906. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1907. .len[0] = sizeof(*key_data.rsc_tsc),
  1908. };
  1909. ret = iwl_trans_send_cmd(trans(priv), &rsc_tsc_cmd);
  1910. if (ret)
  1911. goto error;
  1912. }
  1913. if (key_data.use_tkip) {
  1914. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1915. REPLY_WOWLAN_TKIP_PARAMS,
  1916. CMD_SYNC, sizeof(tkip_cmd),
  1917. &tkip_cmd);
  1918. if (ret)
  1919. goto error;
  1920. }
  1921. if (priv->have_rekey_data) {
  1922. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1923. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  1924. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1925. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  1926. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1927. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  1928. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1929. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  1930. CMD_SYNC, sizeof(kek_kck_cmd),
  1931. &kek_kck_cmd);
  1932. if (ret)
  1933. goto error;
  1934. }
  1935. }
  1936. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_WOWLAN_WAKEUP_FILTER,
  1937. CMD_SYNC, sizeof(wakeup_filter_cmd),
  1938. &wakeup_filter_cmd);
  1939. if (ret)
  1940. goto error;
  1941. ret = iwlagn_send_patterns(priv, wowlan);
  1942. if (ret)
  1943. goto error;
  1944. device_set_wakeup_enable(priv->bus->dev, true);
  1945. /* Now let the ucode operate on its own */
  1946. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
  1947. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  1948. goto out;
  1949. error:
  1950. priv->shrd->wowlan = false;
  1951. iwlagn_prepare_restart(priv);
  1952. ieee80211_restart_hw(priv->hw);
  1953. out:
  1954. mutex_unlock(&priv->shrd->mutex);
  1955. kfree(key_data.rsc_tsc);
  1956. return ret;
  1957. }
  1958. static int iwlagn_mac_resume(struct ieee80211_hw *hw)
  1959. {
  1960. struct iwl_priv *priv = hw->priv;
  1961. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1962. struct ieee80211_vif *vif;
  1963. unsigned long flags;
  1964. u32 base, status = 0xffffffff;
  1965. int ret = -EIO;
  1966. mutex_lock(&priv->shrd->mutex);
  1967. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
  1968. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  1969. base = priv->device_pointers.error_event_table;
  1970. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  1971. spin_lock_irqsave(&bus(priv)->reg_lock, flags);
  1972. ret = iwl_grab_nic_access_silent(bus(priv));
  1973. if (ret == 0) {
  1974. iwl_write32(bus(priv), HBUS_TARG_MEM_RADDR, base);
  1975. status = iwl_read32(bus(priv), HBUS_TARG_MEM_RDAT);
  1976. iwl_release_nic_access(bus(priv));
  1977. }
  1978. spin_unlock_irqrestore(&bus(priv)->reg_lock, flags);
  1979. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1980. if (ret == 0) {
  1981. if (!priv->wowlan_sram)
  1982. priv->wowlan_sram =
  1983. kzalloc(priv->ucode_wowlan.data.len,
  1984. GFP_KERNEL);
  1985. if (priv->wowlan_sram)
  1986. _iwl_read_targ_mem_words(
  1987. bus(priv), 0x800000, priv->wowlan_sram,
  1988. priv->ucode_wowlan.data.len / 4);
  1989. }
  1990. #endif
  1991. }
  1992. /* we'll clear ctx->vif during iwlagn_prepare_restart() */
  1993. vif = ctx->vif;
  1994. priv->shrd->wowlan = false;
  1995. device_set_wakeup_enable(priv->bus->dev, false);
  1996. iwlagn_prepare_restart(priv);
  1997. memset((void *)&ctx->active, 0, sizeof(ctx->active));
  1998. iwl_connection_init_rx_config(priv, ctx);
  1999. iwlagn_set_rxon_chain(priv, ctx);
  2000. mutex_unlock(&priv->shrd->mutex);
  2001. ieee80211_resume_disconnect(vif);
  2002. return 1;
  2003. }
  2004. #endif
  2005. static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2006. {
  2007. struct iwl_priv *priv = hw->priv;
  2008. IWL_DEBUG_MACDUMP(priv, "enter\n");
  2009. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2010. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2011. if (iwlagn_tx_skb(priv, skb))
  2012. dev_kfree_skb_any(skb);
  2013. IWL_DEBUG_MACDUMP(priv, "leave\n");
  2014. }
  2015. static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
  2016. struct ieee80211_vif *vif,
  2017. struct ieee80211_key_conf *keyconf,
  2018. struct ieee80211_sta *sta,
  2019. u32 iv32, u16 *phase1key)
  2020. {
  2021. struct iwl_priv *priv = hw->priv;
  2022. iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
  2023. }
  2024. static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2025. struct ieee80211_vif *vif,
  2026. struct ieee80211_sta *sta,
  2027. struct ieee80211_key_conf *key)
  2028. {
  2029. struct iwl_priv *priv = hw->priv;
  2030. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2031. struct iwl_rxon_context *ctx = vif_priv->ctx;
  2032. int ret;
  2033. bool is_default_wep_key = false;
  2034. IWL_DEBUG_MAC80211(priv, "enter\n");
  2035. if (iwlagn_mod_params.sw_crypto) {
  2036. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  2037. return -EOPNOTSUPP;
  2038. }
  2039. /*
  2040. * We could program these keys into the hardware as well, but we
  2041. * don't expect much multicast traffic in IBSS and having keys
  2042. * for more stations is probably more useful.
  2043. *
  2044. * Mark key TX-only and return 0.
  2045. */
  2046. if (vif->type == NL80211_IFTYPE_ADHOC &&
  2047. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  2048. key->hw_key_idx = WEP_INVALID_OFFSET;
  2049. return 0;
  2050. }
  2051. /* If they key was TX-only, accept deletion */
  2052. if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
  2053. return 0;
  2054. mutex_lock(&priv->shrd->mutex);
  2055. iwl_scan_cancel_timeout(priv, 100);
  2056. BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
  2057. /*
  2058. * If we are getting WEP group key and we didn't receive any key mapping
  2059. * so far, we are in legacy wep mode (group key only), otherwise we are
  2060. * in 1X mode.
  2061. * In legacy wep mode, we use another host command to the uCode.
  2062. */
  2063. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2064. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  2065. if (cmd == SET_KEY)
  2066. is_default_wep_key = !ctx->key_mapping_keys;
  2067. else
  2068. is_default_wep_key =
  2069. key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
  2070. }
  2071. switch (cmd) {
  2072. case SET_KEY:
  2073. if (is_default_wep_key) {
  2074. ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
  2075. break;
  2076. }
  2077. ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
  2078. if (ret) {
  2079. /*
  2080. * can't add key for RX, but we don't need it
  2081. * in the device for TX so still return 0
  2082. */
  2083. ret = 0;
  2084. key->hw_key_idx = WEP_INVALID_OFFSET;
  2085. }
  2086. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  2087. break;
  2088. case DISABLE_KEY:
  2089. if (is_default_wep_key)
  2090. ret = iwl_remove_default_wep_key(priv, ctx, key);
  2091. else
  2092. ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
  2093. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  2094. break;
  2095. default:
  2096. ret = -EINVAL;
  2097. }
  2098. mutex_unlock(&priv->shrd->mutex);
  2099. IWL_DEBUG_MAC80211(priv, "leave\n");
  2100. return ret;
  2101. }
  2102. static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
  2103. struct ieee80211_vif *vif,
  2104. enum ieee80211_ampdu_mlme_action action,
  2105. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  2106. u8 buf_size)
  2107. {
  2108. struct iwl_priv *priv = hw->priv;
  2109. int ret = -EINVAL;
  2110. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  2111. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  2112. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  2113. sta->addr, tid);
  2114. if (!(priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE))
  2115. return -EACCES;
  2116. mutex_lock(&priv->shrd->mutex);
  2117. switch (action) {
  2118. case IEEE80211_AMPDU_RX_START:
  2119. IWL_DEBUG_HT(priv, "start Rx\n");
  2120. ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
  2121. break;
  2122. case IEEE80211_AMPDU_RX_STOP:
  2123. IWL_DEBUG_HT(priv, "stop Rx\n");
  2124. ret = iwl_sta_rx_agg_stop(priv, sta, tid);
  2125. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  2126. ret = 0;
  2127. break;
  2128. case IEEE80211_AMPDU_TX_START:
  2129. IWL_DEBUG_HT(priv, "start Tx\n");
  2130. ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
  2131. if (ret == 0) {
  2132. priv->agg_tids_count++;
  2133. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  2134. priv->agg_tids_count);
  2135. }
  2136. break;
  2137. case IEEE80211_AMPDU_TX_STOP:
  2138. IWL_DEBUG_HT(priv, "stop Tx\n");
  2139. ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
  2140. if ((ret == 0) && (priv->agg_tids_count > 0)) {
  2141. priv->agg_tids_count--;
  2142. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  2143. priv->agg_tids_count);
  2144. }
  2145. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  2146. ret = 0;
  2147. if (priv->cfg->ht_params &&
  2148. priv->cfg->ht_params->use_rts_for_aggregation) {
  2149. /*
  2150. * switch off RTS/CTS if it was previously enabled
  2151. */
  2152. sta_priv->lq_sta.lq.general_params.flags &=
  2153. ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  2154. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  2155. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  2156. }
  2157. break;
  2158. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2159. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  2160. iwl_trans_tx_agg_setup(trans(priv), ctx->ctxid, iwl_sta_id(sta),
  2161. tid, buf_size);
  2162. /*
  2163. * If the limit is 0, then it wasn't initialised yet,
  2164. * use the default. We can do that since we take the
  2165. * minimum below, and we don't want to go above our
  2166. * default due to hardware restrictions.
  2167. */
  2168. if (sta_priv->max_agg_bufsize == 0)
  2169. sta_priv->max_agg_bufsize =
  2170. LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2171. /*
  2172. * Even though in theory the peer could have different
  2173. * aggregation reorder buffer sizes for different sessions,
  2174. * our ucode doesn't allow for that and has a global limit
  2175. * for each station. Therefore, use the minimum of all the
  2176. * aggregation sessions and our default value.
  2177. */
  2178. sta_priv->max_agg_bufsize =
  2179. min(sta_priv->max_agg_bufsize, buf_size);
  2180. if (priv->cfg->ht_params &&
  2181. priv->cfg->ht_params->use_rts_for_aggregation) {
  2182. /*
  2183. * switch to RTS/CTS if it is the prefer protection
  2184. * method for HT traffic
  2185. */
  2186. sta_priv->lq_sta.lq.general_params.flags |=
  2187. LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  2188. }
  2189. sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
  2190. sta_priv->max_agg_bufsize;
  2191. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  2192. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  2193. IWL_INFO(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
  2194. sta->addr, tid);
  2195. ret = 0;
  2196. break;
  2197. }
  2198. mutex_unlock(&priv->shrd->mutex);
  2199. return ret;
  2200. }
  2201. static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
  2202. struct ieee80211_vif *vif,
  2203. struct ieee80211_sta *sta)
  2204. {
  2205. struct iwl_priv *priv = hw->priv;
  2206. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  2207. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2208. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  2209. int ret;
  2210. u8 sta_id;
  2211. IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
  2212. sta->addr);
  2213. mutex_lock(&priv->shrd->mutex);
  2214. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  2215. sta->addr);
  2216. sta_priv->common.sta_id = IWL_INVALID_STATION;
  2217. atomic_set(&sta_priv->pending_frames, 0);
  2218. if (vif->type == NL80211_IFTYPE_AP)
  2219. sta_priv->client = true;
  2220. ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
  2221. is_ap, sta, &sta_id);
  2222. if (ret) {
  2223. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  2224. sta->addr, ret);
  2225. /* Should we return success if return code is EEXIST ? */
  2226. mutex_unlock(&priv->shrd->mutex);
  2227. return ret;
  2228. }
  2229. sta_priv->common.sta_id = sta_id;
  2230. /* Initialize rate scaling */
  2231. IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
  2232. sta->addr);
  2233. iwl_rs_rate_init(priv, sta, sta_id);
  2234. mutex_unlock(&priv->shrd->mutex);
  2235. return 0;
  2236. }
  2237. static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
  2238. struct ieee80211_channel_switch *ch_switch)
  2239. {
  2240. struct iwl_priv *priv = hw->priv;
  2241. const struct iwl_channel_info *ch_info;
  2242. struct ieee80211_conf *conf = &hw->conf;
  2243. struct ieee80211_channel *channel = ch_switch->channel;
  2244. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  2245. /*
  2246. * MULTI-FIXME
  2247. * When we add support for multiple interfaces, we need to
  2248. * revisit this. The channel switch command in the device
  2249. * only affects the BSS context, but what does that really
  2250. * mean? And what if we get a CSA on the second interface?
  2251. * This needs a lot of work.
  2252. */
  2253. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2254. u16 ch;
  2255. IWL_DEBUG_MAC80211(priv, "enter\n");
  2256. mutex_lock(&priv->shrd->mutex);
  2257. if (iwl_is_rfkill(priv->shrd))
  2258. goto out;
  2259. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  2260. test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  2261. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
  2262. goto out;
  2263. if (!iwl_is_associated_ctx(ctx))
  2264. goto out;
  2265. if (!priv->cfg->lib->set_channel_switch)
  2266. goto out;
  2267. ch = channel->hw_value;
  2268. if (le16_to_cpu(ctx->active.channel) == ch)
  2269. goto out;
  2270. ch_info = iwl_get_channel_info(priv, channel->band, ch);
  2271. if (!is_channel_valid(ch_info)) {
  2272. IWL_DEBUG_MAC80211(priv, "invalid channel\n");
  2273. goto out;
  2274. }
  2275. spin_lock_irq(&priv->shrd->lock);
  2276. priv->current_ht_config.smps = conf->smps_mode;
  2277. /* Configure HT40 channels */
  2278. ctx->ht.enabled = conf_is_ht(conf);
  2279. if (ctx->ht.enabled) {
  2280. if (conf_is_ht40_minus(conf)) {
  2281. ctx->ht.extension_chan_offset =
  2282. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2283. ctx->ht.is_40mhz = true;
  2284. } else if (conf_is_ht40_plus(conf)) {
  2285. ctx->ht.extension_chan_offset =
  2286. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2287. ctx->ht.is_40mhz = true;
  2288. } else {
  2289. ctx->ht.extension_chan_offset =
  2290. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2291. ctx->ht.is_40mhz = false;
  2292. }
  2293. } else
  2294. ctx->ht.is_40mhz = false;
  2295. if ((le16_to_cpu(ctx->staging.channel) != ch))
  2296. ctx->staging.flags = 0;
  2297. iwl_set_rxon_channel(priv, channel, ctx);
  2298. iwl_set_rxon_ht(priv, ht_conf);
  2299. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  2300. spin_unlock_irq(&priv->shrd->lock);
  2301. iwl_set_rate(priv);
  2302. /*
  2303. * at this point, staging_rxon has the
  2304. * configuration for channel switch
  2305. */
  2306. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  2307. priv->switch_channel = cpu_to_le16(ch);
  2308. if (priv->cfg->lib->set_channel_switch(priv, ch_switch)) {
  2309. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  2310. priv->switch_channel = 0;
  2311. ieee80211_chswitch_done(ctx->vif, false);
  2312. }
  2313. out:
  2314. mutex_unlock(&priv->shrd->mutex);
  2315. IWL_DEBUG_MAC80211(priv, "leave\n");
  2316. }
  2317. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  2318. unsigned int changed_flags,
  2319. unsigned int *total_flags,
  2320. u64 multicast)
  2321. {
  2322. struct iwl_priv *priv = hw->priv;
  2323. __le32 filter_or = 0, filter_nand = 0;
  2324. struct iwl_rxon_context *ctx;
  2325. #define CHK(test, flag) do { \
  2326. if (*total_flags & (test)) \
  2327. filter_or |= (flag); \
  2328. else \
  2329. filter_nand |= (flag); \
  2330. } while (0)
  2331. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  2332. changed_flags, *total_flags);
  2333. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  2334. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  2335. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  2336. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  2337. #undef CHK
  2338. mutex_lock(&priv->shrd->mutex);
  2339. for_each_context(priv, ctx) {
  2340. ctx->staging.filter_flags &= ~filter_nand;
  2341. ctx->staging.filter_flags |= filter_or;
  2342. /*
  2343. * Not committing directly because hardware can perform a scan,
  2344. * but we'll eventually commit the filter flags change anyway.
  2345. */
  2346. }
  2347. mutex_unlock(&priv->shrd->mutex);
  2348. /*
  2349. * Receiving all multicast frames is always enabled by the
  2350. * default flags setup in iwl_connection_init_rx_config()
  2351. * since we currently do not support programming multicast
  2352. * filters into the device.
  2353. */
  2354. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  2355. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  2356. }
  2357. static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
  2358. {
  2359. struct iwl_priv *priv = hw->priv;
  2360. mutex_lock(&priv->shrd->mutex);
  2361. IWL_DEBUG_MAC80211(priv, "enter\n");
  2362. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  2363. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  2364. goto done;
  2365. }
  2366. if (iwl_is_rfkill(priv->shrd)) {
  2367. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  2368. goto done;
  2369. }
  2370. /*
  2371. * mac80211 will not push any more frames for transmit
  2372. * until the flush is completed
  2373. */
  2374. if (drop) {
  2375. IWL_DEBUG_MAC80211(priv, "send flush command\n");
  2376. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  2377. IWL_ERR(priv, "flush request fail\n");
  2378. goto done;
  2379. }
  2380. }
  2381. IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
  2382. iwl_trans_wait_tx_queue_empty(trans(priv));
  2383. done:
  2384. mutex_unlock(&priv->shrd->mutex);
  2385. IWL_DEBUG_MAC80211(priv, "leave\n");
  2386. }
  2387. void iwlagn_disable_roc(struct iwl_priv *priv)
  2388. {
  2389. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  2390. lockdep_assert_held(&priv->shrd->mutex);
  2391. if (!priv->hw_roc_setup)
  2392. return;
  2393. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  2394. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2395. priv->hw_roc_channel = NULL;
  2396. memset(ctx->staging.node_addr, 0, ETH_ALEN);
  2397. iwlagn_commit_rxon(priv, ctx);
  2398. ctx->is_active = false;
  2399. priv->hw_roc_setup = false;
  2400. }
  2401. static void iwlagn_disable_roc_work(struct work_struct *work)
  2402. {
  2403. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  2404. hw_roc_disable_work.work);
  2405. mutex_lock(&priv->shrd->mutex);
  2406. iwlagn_disable_roc(priv);
  2407. mutex_unlock(&priv->shrd->mutex);
  2408. }
  2409. static int iwl_mac_remain_on_channel(struct ieee80211_hw *hw,
  2410. struct ieee80211_channel *channel,
  2411. enum nl80211_channel_type channel_type,
  2412. int duration)
  2413. {
  2414. struct iwl_priv *priv = hw->priv;
  2415. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  2416. int err = 0;
  2417. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  2418. return -EOPNOTSUPP;
  2419. if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
  2420. return -EOPNOTSUPP;
  2421. mutex_lock(&priv->shrd->mutex);
  2422. /*
  2423. * TODO: Remove this hack! Firmware needs to be updated
  2424. * to allow longer off-channel periods in scanning for
  2425. * this use case, based on a flag (and we'll need an API
  2426. * flag in the firmware when it has that).
  2427. */
  2428. if (iwl_is_associated(priv, IWL_RXON_CTX_BSS) && duration > 80)
  2429. duration = 80;
  2430. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  2431. err = -EBUSY;
  2432. goto out;
  2433. }
  2434. priv->hw_roc_channel = channel;
  2435. priv->hw_roc_chantype = channel_type;
  2436. priv->hw_roc_duration = duration;
  2437. cancel_delayed_work(&priv->hw_roc_disable_work);
  2438. if (!ctx->is_active) {
  2439. ctx->is_active = true;
  2440. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  2441. memcpy(ctx->staging.node_addr,
  2442. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  2443. ETH_ALEN);
  2444. memcpy(ctx->staging.bssid_addr,
  2445. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  2446. ETH_ALEN);
  2447. err = iwlagn_commit_rxon(priv, ctx);
  2448. if (err)
  2449. goto out;
  2450. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
  2451. RXON_FILTER_PROMISC_MSK |
  2452. RXON_FILTER_CTL2HOST_MSK;
  2453. err = iwlagn_commit_rxon(priv, ctx);
  2454. if (err) {
  2455. iwlagn_disable_roc(priv);
  2456. goto out;
  2457. }
  2458. priv->hw_roc_setup = true;
  2459. }
  2460. err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
  2461. if (err)
  2462. iwlagn_disable_roc(priv);
  2463. out:
  2464. mutex_unlock(&priv->shrd->mutex);
  2465. return err;
  2466. }
  2467. static int iwl_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
  2468. {
  2469. struct iwl_priv *priv = hw->priv;
  2470. if (!(priv->shrd->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  2471. return -EOPNOTSUPP;
  2472. mutex_lock(&priv->shrd->mutex);
  2473. iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
  2474. iwlagn_disable_roc(priv);
  2475. mutex_unlock(&priv->shrd->mutex);
  2476. return 0;
  2477. }
  2478. static int iwl_mac_tx_sync(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  2479. const u8 *bssid, enum ieee80211_tx_sync_type type)
  2480. {
  2481. struct iwl_priv *priv = hw->priv;
  2482. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2483. struct iwl_rxon_context *ctx = vif_priv->ctx;
  2484. int ret;
  2485. u8 sta_id;
  2486. mutex_lock(&priv->shrd->mutex);
  2487. if (iwl_is_associated_ctx(ctx)) {
  2488. ret = 0;
  2489. goto out;
  2490. }
  2491. if (ctx->preauth_bssid || test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  2492. ret = -EBUSY;
  2493. goto out;
  2494. }
  2495. ret = iwl_add_station_common(priv, ctx, bssid, true, NULL, &sta_id);
  2496. if (ret)
  2497. goto out;
  2498. if (WARN_ON(sta_id != ctx->ap_sta_id)) {
  2499. ret = -EIO;
  2500. goto out_remove_sta;
  2501. }
  2502. memcpy(ctx->bssid, bssid, ETH_ALEN);
  2503. ctx->preauth_bssid = true;
  2504. ret = iwlagn_commit_rxon(priv, ctx);
  2505. if (ret == 0)
  2506. goto out;
  2507. out_remove_sta:
  2508. iwl_remove_station(priv, sta_id, bssid);
  2509. out:
  2510. mutex_unlock(&priv->shrd->mutex);
  2511. return ret;
  2512. }
  2513. static void iwl_mac_finish_tx_sync(struct ieee80211_hw *hw,
  2514. struct ieee80211_vif *vif,
  2515. const u8 *bssid,
  2516. enum ieee80211_tx_sync_type type)
  2517. {
  2518. struct iwl_priv *priv = hw->priv;
  2519. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2520. struct iwl_rxon_context *ctx = vif_priv->ctx;
  2521. mutex_lock(&priv->shrd->mutex);
  2522. if (iwl_is_associated_ctx(ctx))
  2523. goto out;
  2524. iwl_remove_station(priv, ctx->ap_sta_id, bssid);
  2525. ctx->preauth_bssid = false;
  2526. /* no need to commit */
  2527. out:
  2528. mutex_unlock(&priv->shrd->mutex);
  2529. }
  2530. /*****************************************************************************
  2531. *
  2532. * driver setup and teardown
  2533. *
  2534. *****************************************************************************/
  2535. static void iwl_setup_deferred_work(struct iwl_priv *priv)
  2536. {
  2537. priv->shrd->workqueue = create_singlethread_workqueue(DRV_NAME);
  2538. init_waitqueue_head(&priv->shrd->wait_command_queue);
  2539. INIT_WORK(&priv->restart, iwl_bg_restart);
  2540. INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
  2541. INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
  2542. INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
  2543. INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
  2544. INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
  2545. INIT_DELAYED_WORK(&priv->hw_roc_disable_work,
  2546. iwlagn_disable_roc_work);
  2547. iwl_setup_scan_deferred_work(priv);
  2548. if (priv->cfg->lib->bt_setup_deferred_work)
  2549. priv->cfg->lib->bt_setup_deferred_work(priv);
  2550. init_timer(&priv->statistics_periodic);
  2551. priv->statistics_periodic.data = (unsigned long)priv;
  2552. priv->statistics_periodic.function = iwl_bg_statistics_periodic;
  2553. init_timer(&priv->ucode_trace);
  2554. priv->ucode_trace.data = (unsigned long)priv;
  2555. priv->ucode_trace.function = iwl_bg_ucode_trace;
  2556. init_timer(&priv->watchdog);
  2557. priv->watchdog.data = (unsigned long)priv;
  2558. priv->watchdog.function = iwl_bg_watchdog;
  2559. }
  2560. static void iwl_cancel_deferred_work(struct iwl_priv *priv)
  2561. {
  2562. if (priv->cfg->lib->cancel_deferred_work)
  2563. priv->cfg->lib->cancel_deferred_work(priv);
  2564. cancel_work_sync(&priv->run_time_calib_work);
  2565. cancel_work_sync(&priv->beacon_update);
  2566. iwl_cancel_scan_deferred_work(priv);
  2567. cancel_work_sync(&priv->bt_full_concurrency);
  2568. cancel_work_sync(&priv->bt_runtime_config);
  2569. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  2570. del_timer_sync(&priv->statistics_periodic);
  2571. del_timer_sync(&priv->ucode_trace);
  2572. }
  2573. static void iwl_init_hw_rates(struct iwl_priv *priv,
  2574. struct ieee80211_rate *rates)
  2575. {
  2576. int i;
  2577. for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
  2578. rates[i].bitrate = iwl_rates[i].ieee * 5;
  2579. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  2580. rates[i].hw_value_short = i;
  2581. rates[i].flags = 0;
  2582. if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
  2583. /*
  2584. * If CCK != 1M then set short preamble rate flag.
  2585. */
  2586. rates[i].flags |=
  2587. (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  2588. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  2589. }
  2590. }
  2591. }
  2592. static int iwl_init_drv(struct iwl_priv *priv)
  2593. {
  2594. int ret;
  2595. spin_lock_init(&priv->shrd->sta_lock);
  2596. mutex_init(&priv->shrd->mutex);
  2597. priv->ieee_channels = NULL;
  2598. priv->ieee_rates = NULL;
  2599. priv->band = IEEE80211_BAND_2GHZ;
  2600. priv->iw_mode = NL80211_IFTYPE_STATION;
  2601. priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  2602. priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  2603. priv->agg_tids_count = 0;
  2604. /* initialize force reset */
  2605. priv->force_reset[IWL_RF_RESET].reset_duration =
  2606. IWL_DELAY_NEXT_FORCE_RF_RESET;
  2607. priv->force_reset[IWL_FW_RESET].reset_duration =
  2608. IWL_DELAY_NEXT_FORCE_FW_RELOAD;
  2609. priv->rx_statistics_jiffies = jiffies;
  2610. /* Choose which receivers/antennas to use */
  2611. iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
  2612. iwl_init_scan_params(priv);
  2613. /* init bt coex */
  2614. if (priv->cfg->bt_params &&
  2615. priv->cfg->bt_params->advanced_bt_coexist) {
  2616. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  2617. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  2618. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  2619. priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
  2620. priv->bt_duration = BT_DURATION_LIMIT_DEF;
  2621. priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
  2622. }
  2623. ret = iwl_init_channel_map(priv);
  2624. if (ret) {
  2625. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  2626. goto err;
  2627. }
  2628. ret = iwl_init_geos(priv);
  2629. if (ret) {
  2630. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  2631. goto err_free_channel_map;
  2632. }
  2633. iwl_init_hw_rates(priv, priv->ieee_rates);
  2634. return 0;
  2635. err_free_channel_map:
  2636. iwl_free_channel_map(priv);
  2637. err:
  2638. return ret;
  2639. }
  2640. static void iwl_uninit_drv(struct iwl_priv *priv)
  2641. {
  2642. iwl_calib_free_results(priv);
  2643. iwl_free_geos(priv);
  2644. iwl_free_channel_map(priv);
  2645. if (priv->tx_cmd_pool)
  2646. kmem_cache_destroy(priv->tx_cmd_pool);
  2647. kfree(priv->scan_cmd);
  2648. kfree(priv->beacon_cmd);
  2649. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2650. kfree(priv->wowlan_sram);
  2651. #endif
  2652. }
  2653. static void iwl_mac_rssi_callback(struct ieee80211_hw *hw,
  2654. enum ieee80211_rssi_event rssi_event)
  2655. {
  2656. struct iwl_priv *priv = hw->priv;
  2657. mutex_lock(&priv->shrd->mutex);
  2658. if (priv->cfg->bt_params &&
  2659. priv->cfg->bt_params->advanced_bt_coexist) {
  2660. if (rssi_event == RSSI_EVENT_LOW)
  2661. priv->bt_enable_pspoll = true;
  2662. else if (rssi_event == RSSI_EVENT_HIGH)
  2663. priv->bt_enable_pspoll = false;
  2664. iwlagn_send_advance_bt_config(priv);
  2665. } else {
  2666. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  2667. "ignoring RSSI callback\n");
  2668. }
  2669. mutex_unlock(&priv->shrd->mutex);
  2670. }
  2671. struct ieee80211_ops iwlagn_hw_ops = {
  2672. .tx = iwlagn_mac_tx,
  2673. .start = iwlagn_mac_start,
  2674. .stop = iwlagn_mac_stop,
  2675. #ifdef CONFIG_PM
  2676. .suspend = iwlagn_mac_suspend,
  2677. .resume = iwlagn_mac_resume,
  2678. #endif
  2679. .add_interface = iwl_mac_add_interface,
  2680. .remove_interface = iwl_mac_remove_interface,
  2681. .change_interface = iwl_mac_change_interface,
  2682. .config = iwlagn_mac_config,
  2683. .configure_filter = iwlagn_configure_filter,
  2684. .set_key = iwlagn_mac_set_key,
  2685. .update_tkip_key = iwlagn_mac_update_tkip_key,
  2686. .set_rekey_data = iwlagn_mac_set_rekey_data,
  2687. .conf_tx = iwl_mac_conf_tx,
  2688. .bss_info_changed = iwlagn_bss_info_changed,
  2689. .ampdu_action = iwlagn_mac_ampdu_action,
  2690. .hw_scan = iwl_mac_hw_scan,
  2691. .sta_notify = iwlagn_mac_sta_notify,
  2692. .sta_add = iwlagn_mac_sta_add,
  2693. .sta_remove = iwl_mac_sta_remove,
  2694. .channel_switch = iwlagn_mac_channel_switch,
  2695. .flush = iwlagn_mac_flush,
  2696. .tx_last_beacon = iwl_mac_tx_last_beacon,
  2697. .remain_on_channel = iwl_mac_remain_on_channel,
  2698. .cancel_remain_on_channel = iwl_mac_cancel_remain_on_channel,
  2699. .rssi_callback = iwl_mac_rssi_callback,
  2700. CFG80211_TESTMODE_CMD(iwl_testmode_cmd)
  2701. CFG80211_TESTMODE_DUMP(iwl_testmode_dump)
  2702. .tx_sync = iwl_mac_tx_sync,
  2703. .finish_tx_sync = iwl_mac_finish_tx_sync,
  2704. };
  2705. static u32 iwl_hw_detect(struct iwl_priv *priv)
  2706. {
  2707. return iwl_read32(bus(priv), CSR_HW_REV);
  2708. }
  2709. /* Size of one Rx buffer in host DRAM */
  2710. #define IWL_RX_BUF_SIZE_4K (4 * 1024)
  2711. #define IWL_RX_BUF_SIZE_8K (8 * 1024)
  2712. static int iwl_set_hw_params(struct iwl_priv *priv)
  2713. {
  2714. if (iwlagn_mod_params.amsdu_size_8K)
  2715. hw_params(priv).rx_page_order =
  2716. get_order(IWL_RX_BUF_SIZE_8K);
  2717. else
  2718. hw_params(priv).rx_page_order =
  2719. get_order(IWL_RX_BUF_SIZE_4K);
  2720. if (iwlagn_mod_params.disable_11n)
  2721. priv->cfg->sku &= ~EEPROM_SKU_CAP_11N_ENABLE;
  2722. hw_params(priv).num_ampdu_queues =
  2723. priv->cfg->base_params->num_of_ampdu_queues;
  2724. hw_params(priv).shadow_reg_enable =
  2725. priv->cfg->base_params->shadow_reg_enable;
  2726. hw_params(priv).sku = priv->cfg->sku;
  2727. hw_params(priv).wd_timeout = priv->cfg->base_params->wd_timeout;
  2728. /* Device-specific setup */
  2729. return priv->cfg->lib->set_hw_params(priv);
  2730. }
  2731. /* This function both allocates and initializes hw and priv. */
  2732. static struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg)
  2733. {
  2734. struct iwl_priv *priv;
  2735. /* mac80211 allocates memory for this device instance, including
  2736. * space for this driver's private structure */
  2737. struct ieee80211_hw *hw;
  2738. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
  2739. if (hw == NULL) {
  2740. pr_err("%s: Can not allocate network device\n",
  2741. cfg->name);
  2742. goto out;
  2743. }
  2744. priv = hw->priv;
  2745. priv->hw = hw;
  2746. out:
  2747. return hw;
  2748. }
  2749. int iwl_probe(struct iwl_bus *bus, const struct iwl_trans_ops *trans_ops,
  2750. struct iwl_cfg *cfg)
  2751. {
  2752. int err = 0;
  2753. struct iwl_priv *priv;
  2754. struct ieee80211_hw *hw;
  2755. u16 num_mac;
  2756. u32 hw_rev;
  2757. /************************
  2758. * 1. Allocating HW data
  2759. ************************/
  2760. hw = iwl_alloc_all(cfg);
  2761. if (!hw) {
  2762. err = -ENOMEM;
  2763. goto out;
  2764. }
  2765. priv = hw->priv;
  2766. priv->bus = bus;
  2767. priv->shrd = &priv->_shrd;
  2768. priv->shrd->bus = bus;
  2769. priv->shrd->priv = priv;
  2770. priv->shrd->hw = hw;
  2771. bus_set_drv_data(priv->bus, priv->shrd);
  2772. priv->shrd->trans = trans_ops->alloc(priv->shrd);
  2773. if (priv->shrd->trans == NULL) {
  2774. err = -ENOMEM;
  2775. goto out_free_traffic_mem;
  2776. }
  2777. /* At this point both hw and priv are allocated. */
  2778. SET_IEEE80211_DEV(hw, priv->bus->dev);
  2779. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  2780. priv->cfg = cfg;
  2781. /* is antenna coupling more than 35dB ? */
  2782. priv->bt_ant_couple_ok =
  2783. (iwlagn_mod_params.ant_coupling >
  2784. IWL_BT_ANTENNA_COUPLING_THRESHOLD) ?
  2785. true : false;
  2786. /* enable/disable bt channel inhibition */
  2787. priv->bt_ch_announce = iwlagn_mod_params.bt_ch_announce;
  2788. IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
  2789. (priv->bt_ch_announce) ? "On" : "Off");
  2790. if (iwl_alloc_traffic_mem(priv))
  2791. IWL_ERR(priv, "Not enough memory to generate traffic log\n");
  2792. /* these spin locks will be used in apm_ops.init and EEPROM access
  2793. * we should init now
  2794. */
  2795. spin_lock_init(&bus(priv)->reg_lock);
  2796. spin_lock_init(&priv->shrd->lock);
  2797. /*
  2798. * stop and reset the on-board processor just in case it is in a
  2799. * strange state ... like being left stranded by a primary kernel
  2800. * and this is now the kdump kernel trying to start up
  2801. */
  2802. iwl_write32(bus(priv), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2803. /***********************
  2804. * 3. Read REV register
  2805. ***********************/
  2806. hw_rev = iwl_hw_detect(priv);
  2807. IWL_INFO(priv, "Detected %s, REV=0x%X\n",
  2808. priv->cfg->name, hw_rev);
  2809. err = iwl_trans_request_irq(trans(priv));
  2810. if (err)
  2811. goto out_free_trans;
  2812. if (iwl_trans_prepare_card_hw(trans(priv))) {
  2813. err = -EIO;
  2814. IWL_WARN(priv, "Failed, HW not ready\n");
  2815. goto out_free_trans;
  2816. }
  2817. /*****************
  2818. * 4. Read EEPROM
  2819. *****************/
  2820. /* Read the EEPROM */
  2821. err = iwl_eeprom_init(priv, hw_rev);
  2822. if (err) {
  2823. IWL_ERR(priv, "Unable to init EEPROM\n");
  2824. goto out_free_trans;
  2825. }
  2826. err = iwl_eeprom_check_version(priv);
  2827. if (err)
  2828. goto out_free_eeprom;
  2829. err = iwl_eeprom_check_sku(priv);
  2830. if (err)
  2831. goto out_free_eeprom;
  2832. /* extract MAC Address */
  2833. iwl_eeprom_get_mac(priv, priv->addresses[0].addr);
  2834. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
  2835. priv->hw->wiphy->addresses = priv->addresses;
  2836. priv->hw->wiphy->n_addresses = 1;
  2837. num_mac = iwl_eeprom_query16(priv, EEPROM_NUM_MAC_ADDRESS);
  2838. if (num_mac > 1) {
  2839. memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
  2840. ETH_ALEN);
  2841. priv->addresses[1].addr[5]++;
  2842. priv->hw->wiphy->n_addresses++;
  2843. }
  2844. /************************
  2845. * 5. Setup HW constants
  2846. ************************/
  2847. if (iwl_set_hw_params(priv)) {
  2848. err = -ENOENT;
  2849. IWL_ERR(priv, "failed to set hw parameters\n");
  2850. goto out_free_eeprom;
  2851. }
  2852. /*******************
  2853. * 6. Setup priv
  2854. *******************/
  2855. err = iwl_init_drv(priv);
  2856. if (err)
  2857. goto out_free_eeprom;
  2858. /* At this point both hw and priv are initialized. */
  2859. /********************
  2860. * 7. Setup services
  2861. ********************/
  2862. iwl_setup_deferred_work(priv);
  2863. iwl_setup_rx_handlers(priv);
  2864. iwl_testmode_init(priv);
  2865. /*********************************************
  2866. * 8. Enable interrupts
  2867. *********************************************/
  2868. iwl_enable_rfkill_int(priv);
  2869. /* If platform's RF_KILL switch is NOT set to KILL */
  2870. if (iwl_read32(bus(priv),
  2871. CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2872. clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  2873. else
  2874. set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  2875. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  2876. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
  2877. iwl_power_initialize(priv);
  2878. iwl_tt_initialize(priv);
  2879. init_completion(&priv->firmware_loading_complete);
  2880. err = iwl_request_firmware(priv, true);
  2881. if (err)
  2882. goto out_destroy_workqueue;
  2883. return 0;
  2884. out_destroy_workqueue:
  2885. destroy_workqueue(priv->shrd->workqueue);
  2886. priv->shrd->workqueue = NULL;
  2887. iwl_uninit_drv(priv);
  2888. out_free_eeprom:
  2889. iwl_eeprom_free(priv);
  2890. out_free_trans:
  2891. iwl_trans_free(trans(priv));
  2892. out_free_traffic_mem:
  2893. iwl_free_traffic_mem(priv);
  2894. ieee80211_free_hw(priv->hw);
  2895. out:
  2896. return err;
  2897. }
  2898. void __devexit iwl_remove(struct iwl_priv * priv)
  2899. {
  2900. wait_for_completion(&priv->firmware_loading_complete);
  2901. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  2902. iwl_dbgfs_unregister(priv);
  2903. sysfs_remove_group(&priv->bus->dev->kobj,
  2904. &iwl_attribute_group);
  2905. /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
  2906. * to be called and iwl_down since we are removing the device
  2907. * we need to set STATUS_EXIT_PENDING bit.
  2908. */
  2909. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  2910. iwl_testmode_cleanup(priv);
  2911. iwl_leds_exit(priv);
  2912. if (priv->shrd->mac80211_registered) {
  2913. ieee80211_unregister_hw(priv->hw);
  2914. priv->shrd->mac80211_registered = 0;
  2915. }
  2916. iwl_tt_exit(priv);
  2917. /*This will stop the queues, move the device to low power state */
  2918. iwl_trans_stop_device(trans(priv));
  2919. iwl_dealloc_ucode(priv);
  2920. iwl_eeprom_free(priv);
  2921. /*netif_stop_queue(dev); */
  2922. flush_workqueue(priv->shrd->workqueue);
  2923. /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
  2924. * priv->shrd->workqueue... so we can't take down the workqueue
  2925. * until now... */
  2926. destroy_workqueue(priv->shrd->workqueue);
  2927. priv->shrd->workqueue = NULL;
  2928. iwl_free_traffic_mem(priv);
  2929. iwl_trans_free(trans(priv));
  2930. bus_set_drv_data(priv->bus, NULL);
  2931. iwl_uninit_drv(priv);
  2932. dev_kfree_skb(priv->beacon_skb);
  2933. ieee80211_free_hw(priv->hw);
  2934. }
  2935. /*****************************************************************************
  2936. *
  2937. * driver and module entry point
  2938. *
  2939. *****************************************************************************/
  2940. static int __init iwl_init(void)
  2941. {
  2942. int ret;
  2943. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  2944. pr_info(DRV_COPYRIGHT "\n");
  2945. ret = iwlagn_rate_control_register();
  2946. if (ret) {
  2947. pr_err("Unable to register rate control algorithm: %d\n", ret);
  2948. return ret;
  2949. }
  2950. ret = iwl_pci_register_driver();
  2951. if (ret)
  2952. goto error_register;
  2953. return ret;
  2954. error_register:
  2955. iwlagn_rate_control_unregister();
  2956. return ret;
  2957. }
  2958. static void __exit iwl_exit(void)
  2959. {
  2960. iwl_pci_unregister_driver();
  2961. iwlagn_rate_control_unregister();
  2962. }
  2963. module_exit(iwl_exit);
  2964. module_init(iwl_init);
  2965. #ifdef CONFIG_IWLWIFI_DEBUG
  2966. module_param_named(debug, iwlagn_mod_params.debug_level, uint,
  2967. S_IRUGO | S_IWUSR);
  2968. MODULE_PARM_DESC(debug, "debug output mask");
  2969. #endif
  2970. module_param_named(swcrypto, iwlagn_mod_params.sw_crypto, int, S_IRUGO);
  2971. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  2972. module_param_named(queues_num, iwlagn_mod_params.num_of_queues, int, S_IRUGO);
  2973. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  2974. module_param_named(11n_disable, iwlagn_mod_params.disable_11n, int, S_IRUGO);
  2975. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  2976. module_param_named(amsdu_size_8K, iwlagn_mod_params.amsdu_size_8K,
  2977. int, S_IRUGO);
  2978. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  2979. module_param_named(fw_restart, iwlagn_mod_params.restart_fw, int, S_IRUGO);
  2980. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  2981. module_param_named(ucode_alternative,
  2982. iwlagn_mod_params.wanted_ucode_alternative,
  2983. int, S_IRUGO);
  2984. MODULE_PARM_DESC(ucode_alternative,
  2985. "specify ucode alternative to use from ucode file");
  2986. module_param_named(antenna_coupling, iwlagn_mod_params.ant_coupling,
  2987. int, S_IRUGO);
  2988. MODULE_PARM_DESC(antenna_coupling,
  2989. "specify antenna coupling in dB (defualt: 0 dB)");
  2990. module_param_named(bt_ch_inhibition, iwlagn_mod_params.bt_ch_announce,
  2991. bool, S_IRUGO);
  2992. MODULE_PARM_DESC(bt_ch_inhibition,
  2993. "Enable BT channel inhibition (default: enable)");
  2994. module_param_named(plcp_check, iwlagn_mod_params.plcp_check, bool, S_IRUGO);
  2995. MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
  2996. module_param_named(ack_check, iwlagn_mod_params.ack_check, bool, S_IRUGO);
  2997. MODULE_PARM_DESC(ack_check, "Check ack health (default: 0 [disabled])");
  2998. module_param_named(wd_disable, iwlagn_mod_params.wd_disable, bool, S_IRUGO);
  2999. MODULE_PARM_DESC(wd_disable,
  3000. "Disable stuck queue watchdog timer (default: 0 [enabled])");
  3001. /*
  3002. * set bt_coex_active to true, uCode will do kill/defer
  3003. * every time the priority line is asserted (BT is sending signals on the
  3004. * priority line in the PCIx).
  3005. * set bt_coex_active to false, uCode will ignore the BT activity and
  3006. * perform the normal operation
  3007. *
  3008. * User might experience transmit issue on some platform due to WiFi/BT
  3009. * co-exist problem. The possible behaviors are:
  3010. * Able to scan and finding all the available AP
  3011. * Not able to associate with any AP
  3012. * On those platforms, WiFi communication can be restored by set
  3013. * "bt_coex_active" module parameter to "false"
  3014. *
  3015. * default: bt_coex_active = true (BT_COEX_ENABLE)
  3016. */
  3017. module_param_named(bt_coex_active, iwlagn_mod_params.bt_coex_active,
  3018. bool, S_IRUGO);
  3019. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  3020. module_param_named(led_mode, iwlagn_mod_params.led_mode, int, S_IRUGO);
  3021. MODULE_PARM_DESC(led_mode, "0=system default, "
  3022. "1=On(RF On)/Off(RF Off), 2=blinking (default: 0)");
  3023. module_param_named(power_save, iwlagn_mod_params.power_save,
  3024. bool, S_IRUGO);
  3025. MODULE_PARM_DESC(power_save,
  3026. "enable WiFi power management (default: disable)");
  3027. module_param_named(power_level, iwlagn_mod_params.power_level,
  3028. int, S_IRUGO);
  3029. MODULE_PARM_DESC(power_level,
  3030. "default power save level (range from 1 - 5, default: 1)");
  3031. module_param_named(auto_agg, iwlagn_mod_params.auto_agg,
  3032. bool, S_IRUGO);
  3033. MODULE_PARM_DESC(auto_agg,
  3034. "enable agg w/o check traffic load (default: enable)");
  3035. /*
  3036. * For now, keep using power level 1 instead of automatically
  3037. * adjusting ...
  3038. */
  3039. module_param_named(no_sleep_autoadjust, iwlagn_mod_params.no_sleep_autoadjust,
  3040. bool, S_IRUGO);
  3041. MODULE_PARM_DESC(no_sleep_autoadjust,
  3042. "don't automatically adjust sleep level "
  3043. "according to maximum network latency (default: true)");