iwl-agn.c 97 KB

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