iwl-agn.c 96 KB

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