iwl-agn.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2012 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/delay.h>
  34. #include <linux/sched.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/firmware.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/if_arp.h>
  40. #include <net/mac80211.h>
  41. #include <asm/div64.h>
  42. #include "iwl-eeprom.h"
  43. #include "iwl-wifi.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 capacity, 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(&trans(priv)->reg_lock, reg_flags);
  276. if (iwl_grab_nic_access(trans(priv))) {
  277. spin_unlock_irqrestore(&trans(priv)->reg_lock, reg_flags);
  278. return;
  279. }
  280. /* Set starting address; reads will auto-increment */
  281. iwl_write32(trans(priv), HBUS_TARG_MEM_RADDR, ptr);
  282. rmb();
  283. /*
  284. * Refuse to read more than would have fit into the log from
  285. * the current start_idx. This used to happen due to the race
  286. * described below, but now WARN because the code below should
  287. * prevent it from happening here.
  288. */
  289. if (WARN_ON(num_events > capacity - start_idx))
  290. num_events = capacity - start_idx;
  291. /*
  292. * "time" is actually "data" for mode 0 (no timestamp).
  293. * place event id # at far right for easier visual parsing.
  294. */
  295. for (i = 0; i < num_events; i++) {
  296. ev = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  297. time = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  298. if (mode == 0) {
  299. trace_iwlwifi_dev_ucode_cont_event(priv, 0, time, ev);
  300. } else {
  301. data = iwl_read32(trans(priv), HBUS_TARG_MEM_RDAT);
  302. trace_iwlwifi_dev_ucode_cont_event(priv, time,
  303. data, ev);
  304. }
  305. }
  306. /* Allow device to power down */
  307. iwl_release_nic_access(trans(priv));
  308. spin_unlock_irqrestore(&trans(priv)->reg_lock, reg_flags);
  309. }
  310. static void iwl_continuous_event_trace(struct iwl_priv *priv)
  311. {
  312. u32 capacity; /* event log capacity in # entries */
  313. struct {
  314. u32 capacity;
  315. u32 mode;
  316. u32 wrap_counter;
  317. u32 write_counter;
  318. } __packed read;
  319. u32 base; /* SRAM byte address of event log header */
  320. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  321. u32 num_wraps; /* # times uCode wrapped to top of log */
  322. u32 next_entry; /* index of next entry to be written by uCode */
  323. base = priv->shrd->device_pointers.log_event_table;
  324. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  325. iwl_read_targ_mem_words(trans(priv), base, &read, sizeof(read));
  326. capacity = read.capacity;
  327. mode = read.mode;
  328. num_wraps = read.wrap_counter;
  329. next_entry = read.write_counter;
  330. } else
  331. return;
  332. /*
  333. * Unfortunately, the uCode doesn't use temporary variables.
  334. * Therefore, it can happen that we read next_entry == capacity,
  335. * which really means next_entry == 0.
  336. */
  337. if (unlikely(next_entry == capacity))
  338. next_entry = 0;
  339. /*
  340. * Additionally, the uCode increases the write pointer before
  341. * the wraps counter, so if the write pointer is smaller than
  342. * the old write pointer (wrap occurred) but we read that no
  343. * wrap occurred, we actually read between the next_entry and
  344. * num_wraps update (this does happen in practice!!) -- take
  345. * that into account by increasing num_wraps.
  346. */
  347. if (unlikely(next_entry < priv->event_log.next_entry &&
  348. num_wraps == priv->event_log.num_wraps))
  349. num_wraps++;
  350. if (num_wraps == priv->event_log.num_wraps) {
  351. iwl_print_cont_event_trace(
  352. priv, base, priv->event_log.next_entry,
  353. next_entry - priv->event_log.next_entry,
  354. capacity, mode);
  355. priv->event_log.non_wraps_count++;
  356. } else {
  357. if (num_wraps - priv->event_log.num_wraps > 1)
  358. priv->event_log.wraps_more_count++;
  359. else
  360. priv->event_log.wraps_once_count++;
  361. trace_iwlwifi_dev_ucode_wrap_event(priv,
  362. num_wraps - priv->event_log.num_wraps,
  363. next_entry, priv->event_log.next_entry);
  364. if (next_entry < priv->event_log.next_entry) {
  365. iwl_print_cont_event_trace(
  366. priv, base, priv->event_log.next_entry,
  367. capacity - priv->event_log.next_entry,
  368. capacity, mode);
  369. iwl_print_cont_event_trace(
  370. priv, base, 0, next_entry, capacity, mode);
  371. } else {
  372. iwl_print_cont_event_trace(
  373. priv, base, next_entry,
  374. capacity - next_entry,
  375. capacity, mode);
  376. iwl_print_cont_event_trace(
  377. priv, base, 0, next_entry, capacity, mode);
  378. }
  379. }
  380. priv->event_log.num_wraps = num_wraps;
  381. priv->event_log.next_entry = next_entry;
  382. }
  383. /**
  384. * iwl_bg_ucode_trace - Timer callback to log ucode event
  385. *
  386. * The timer is continually set to execute every
  387. * UCODE_TRACE_PERIOD milliseconds after the last timer expired
  388. * this function is to perform continuous uCode event logging operation
  389. * if enabled
  390. */
  391. static void iwl_bg_ucode_trace(unsigned long data)
  392. {
  393. struct iwl_priv *priv = (struct iwl_priv *)data;
  394. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  395. return;
  396. if (priv->event_log.ucode_trace) {
  397. iwl_continuous_event_trace(priv);
  398. /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
  399. mod_timer(&priv->ucode_trace,
  400. jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
  401. }
  402. }
  403. static void iwl_bg_tx_flush(struct work_struct *work)
  404. {
  405. struct iwl_priv *priv =
  406. container_of(work, struct iwl_priv, tx_flush);
  407. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  408. return;
  409. /* do nothing if rf-kill is on */
  410. if (!iwl_is_ready_rf(priv->shrd))
  411. return;
  412. IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
  413. iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
  414. }
  415. static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
  416. {
  417. int i;
  418. /*
  419. * The default context is always valid,
  420. * the PAN context depends on uCode.
  421. */
  422. priv->shrd->valid_contexts = BIT(IWL_RXON_CTX_BSS);
  423. if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
  424. priv->shrd->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
  425. for (i = 0; i < NUM_IWL_RXON_CTX; i++)
  426. priv->contexts[i].ctxid = i;
  427. priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
  428. priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
  429. priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
  430. priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
  431. priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
  432. priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
  433. priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
  434. priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
  435. priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
  436. BIT(NL80211_IFTYPE_ADHOC);
  437. priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
  438. BIT(NL80211_IFTYPE_STATION);
  439. priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
  440. priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
  441. priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
  442. priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
  443. priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
  444. priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
  445. REPLY_WIPAN_RXON_TIMING;
  446. priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
  447. REPLY_WIPAN_RXON_ASSOC;
  448. priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
  449. priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
  450. priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
  451. priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
  452. priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
  453. priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
  454. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
  455. if (ucode_flags & IWL_UCODE_TLV_FLAGS_P2P)
  456. priv->contexts[IWL_RXON_CTX_PAN].interface_modes |=
  457. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  458. BIT(NL80211_IFTYPE_P2P_GO);
  459. priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
  460. priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
  461. priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
  462. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  463. }
  464. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  465. #define UCODE_EXPERIMENTAL_INDEX 100
  466. #define UCODE_EXPERIMENTAL_TAG "exp"
  467. static int __must_check iwl_request_firmware(struct iwl_priv *priv, bool first)
  468. {
  469. const char *name_pre = cfg(priv)->fw_name_pre;
  470. char tag[8];
  471. if (first) {
  472. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  473. priv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  474. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  475. } else if (priv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  476. #endif
  477. priv->fw_index = cfg(priv)->ucode_api_max;
  478. sprintf(tag, "%d", priv->fw_index);
  479. } else {
  480. priv->fw_index--;
  481. sprintf(tag, "%d", priv->fw_index);
  482. }
  483. if (priv->fw_index < cfg(priv)->ucode_api_min) {
  484. IWL_ERR(priv, "no suitable firmware found!\n");
  485. return -ENOENT;
  486. }
  487. sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  488. IWL_DEBUG_INFO(priv, "attempting to load firmware %s'%s'\n",
  489. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  490. ? "EXPERIMENTAL " : "",
  491. priv->firmware_name);
  492. return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
  493. trans(priv)->dev,
  494. GFP_KERNEL, priv, iwl_ucode_callback);
  495. }
  496. struct iwlagn_firmware_pieces {
  497. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  498. size_t inst_size, data_size, init_size, init_data_size,
  499. wowlan_inst_size, wowlan_data_size;
  500. u32 build;
  501. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  502. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  503. };
  504. static int iwlagn_load_legacy_firmware(struct iwl_priv *priv,
  505. const struct firmware *ucode_raw,
  506. struct iwlagn_firmware_pieces *pieces)
  507. {
  508. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  509. u32 api_ver, hdr_size;
  510. const u8 *src;
  511. priv->ucode_ver = le32_to_cpu(ucode->ver);
  512. api_ver = IWL_UCODE_API(priv->ucode_ver);
  513. switch (api_ver) {
  514. default:
  515. hdr_size = 28;
  516. if (ucode_raw->size < hdr_size) {
  517. IWL_ERR(priv, "File size too small!\n");
  518. return -EINVAL;
  519. }
  520. pieces->build = le32_to_cpu(ucode->u.v2.build);
  521. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  522. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  523. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  524. pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
  525. src = ucode->u.v2.data;
  526. break;
  527. case 0:
  528. case 1:
  529. case 2:
  530. hdr_size = 24;
  531. if (ucode_raw->size < hdr_size) {
  532. IWL_ERR(priv, "File size too small!\n");
  533. return -EINVAL;
  534. }
  535. pieces->build = 0;
  536. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  537. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  538. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  539. pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
  540. src = ucode->u.v1.data;
  541. break;
  542. }
  543. /* Verify size of file vs. image size info in file's header */
  544. if (ucode_raw->size != hdr_size + pieces->inst_size +
  545. pieces->data_size + pieces->init_size +
  546. pieces->init_data_size) {
  547. IWL_ERR(priv,
  548. "uCode file size %d does not match expected size\n",
  549. (int)ucode_raw->size);
  550. return -EINVAL;
  551. }
  552. pieces->inst = src;
  553. src += pieces->inst_size;
  554. pieces->data = src;
  555. src += pieces->data_size;
  556. pieces->init = src;
  557. src += pieces->init_size;
  558. pieces->init_data = src;
  559. src += pieces->init_data_size;
  560. return 0;
  561. }
  562. static int iwlagn_load_firmware(struct iwl_priv *priv,
  563. const struct firmware *ucode_raw,
  564. struct iwlagn_firmware_pieces *pieces,
  565. struct iwlagn_ucode_capabilities *capa)
  566. {
  567. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  568. struct iwl_ucode_tlv *tlv;
  569. size_t len = ucode_raw->size;
  570. const u8 *data;
  571. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  572. int tmp;
  573. u64 alternatives;
  574. u32 tlv_len;
  575. enum iwl_ucode_tlv_type tlv_type;
  576. const u8 *tlv_data;
  577. if (len < sizeof(*ucode)) {
  578. IWL_ERR(priv, "uCode has invalid length: %zd\n", len);
  579. return -EINVAL;
  580. }
  581. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  582. IWL_ERR(priv, "invalid uCode magic: 0X%x\n",
  583. le32_to_cpu(ucode->magic));
  584. return -EINVAL;
  585. }
  586. /*
  587. * Check which alternatives are present, and "downgrade"
  588. * when the chosen alternative is not present, warning
  589. * the user when that happens. Some files may not have
  590. * any alternatives, so don't warn in that case.
  591. */
  592. alternatives = le64_to_cpu(ucode->alternatives);
  593. tmp = wanted_alternative;
  594. if (wanted_alternative > 63)
  595. wanted_alternative = 63;
  596. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  597. wanted_alternative--;
  598. if (wanted_alternative && wanted_alternative != tmp)
  599. IWL_WARN(priv,
  600. "uCode alternative %d not available, choosing %d\n",
  601. tmp, wanted_alternative);
  602. priv->ucode_ver = le32_to_cpu(ucode->ver);
  603. pieces->build = le32_to_cpu(ucode->build);
  604. data = ucode->data;
  605. len -= sizeof(*ucode);
  606. while (len >= sizeof(*tlv)) {
  607. u16 tlv_alt;
  608. len -= sizeof(*tlv);
  609. tlv = (void *)data;
  610. tlv_len = le32_to_cpu(tlv->length);
  611. tlv_type = le16_to_cpu(tlv->type);
  612. tlv_alt = le16_to_cpu(tlv->alternative);
  613. tlv_data = tlv->data;
  614. if (len < tlv_len) {
  615. IWL_ERR(priv, "invalid TLV len: %zd/%u\n",
  616. len, tlv_len);
  617. return -EINVAL;
  618. }
  619. len -= ALIGN(tlv_len, 4);
  620. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  621. /*
  622. * Alternative 0 is always valid.
  623. *
  624. * Skip alternative TLVs that are not selected.
  625. */
  626. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  627. continue;
  628. switch (tlv_type) {
  629. case IWL_UCODE_TLV_INST:
  630. pieces->inst = tlv_data;
  631. pieces->inst_size = tlv_len;
  632. break;
  633. case IWL_UCODE_TLV_DATA:
  634. pieces->data = tlv_data;
  635. pieces->data_size = tlv_len;
  636. break;
  637. case IWL_UCODE_TLV_INIT:
  638. pieces->init = tlv_data;
  639. pieces->init_size = tlv_len;
  640. break;
  641. case IWL_UCODE_TLV_INIT_DATA:
  642. pieces->init_data = tlv_data;
  643. pieces->init_data_size = tlv_len;
  644. break;
  645. case IWL_UCODE_TLV_BOOT:
  646. IWL_ERR(priv, "Found unexpected BOOT ucode\n");
  647. break;
  648. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  649. if (tlv_len != sizeof(u32))
  650. goto invalid_tlv_len;
  651. capa->max_probe_length =
  652. le32_to_cpup((__le32 *)tlv_data);
  653. break;
  654. case IWL_UCODE_TLV_PAN:
  655. if (tlv_len)
  656. goto invalid_tlv_len;
  657. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  658. break;
  659. case IWL_UCODE_TLV_FLAGS:
  660. /* must be at least one u32 */
  661. if (tlv_len < sizeof(u32))
  662. goto invalid_tlv_len;
  663. /* and a proper number of u32s */
  664. if (tlv_len % sizeof(u32))
  665. goto invalid_tlv_len;
  666. /*
  667. * This driver only reads the first u32 as
  668. * right now no more features are defined,
  669. * if that changes then either the driver
  670. * will not work with the new firmware, or
  671. * it'll not take advantage of new features.
  672. */
  673. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  674. break;
  675. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  676. if (tlv_len != sizeof(u32))
  677. goto invalid_tlv_len;
  678. pieces->init_evtlog_ptr =
  679. le32_to_cpup((__le32 *)tlv_data);
  680. break;
  681. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  682. if (tlv_len != sizeof(u32))
  683. goto invalid_tlv_len;
  684. pieces->init_evtlog_size =
  685. le32_to_cpup((__le32 *)tlv_data);
  686. break;
  687. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  688. if (tlv_len != sizeof(u32))
  689. goto invalid_tlv_len;
  690. pieces->init_errlog_ptr =
  691. le32_to_cpup((__le32 *)tlv_data);
  692. break;
  693. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  694. if (tlv_len != sizeof(u32))
  695. goto invalid_tlv_len;
  696. pieces->inst_evtlog_ptr =
  697. le32_to_cpup((__le32 *)tlv_data);
  698. break;
  699. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  700. if (tlv_len != sizeof(u32))
  701. goto invalid_tlv_len;
  702. pieces->inst_evtlog_size =
  703. le32_to_cpup((__le32 *)tlv_data);
  704. break;
  705. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  706. if (tlv_len != sizeof(u32))
  707. goto invalid_tlv_len;
  708. pieces->inst_errlog_ptr =
  709. le32_to_cpup((__le32 *)tlv_data);
  710. break;
  711. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  712. if (tlv_len)
  713. goto invalid_tlv_len;
  714. priv->enhance_sensitivity_table = true;
  715. break;
  716. case IWL_UCODE_TLV_WOWLAN_INST:
  717. pieces->wowlan_inst = tlv_data;
  718. pieces->wowlan_inst_size = tlv_len;
  719. break;
  720. case IWL_UCODE_TLV_WOWLAN_DATA:
  721. pieces->wowlan_data = tlv_data;
  722. pieces->wowlan_data_size = tlv_len;
  723. break;
  724. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  725. if (tlv_len != sizeof(u32))
  726. goto invalid_tlv_len;
  727. capa->standard_phy_calibration_size =
  728. le32_to_cpup((__le32 *)tlv_data);
  729. break;
  730. default:
  731. IWL_DEBUG_INFO(priv, "unknown TLV: %d\n", tlv_type);
  732. break;
  733. }
  734. }
  735. if (len) {
  736. IWL_ERR(priv, "invalid TLV after parsing: %zd\n", len);
  737. iwl_print_hex_dump(priv, IWL_DL_FW, (u8 *)data, len);
  738. return -EINVAL;
  739. }
  740. return 0;
  741. invalid_tlv_len:
  742. IWL_ERR(priv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  743. iwl_print_hex_dump(priv, IWL_DL_FW, tlv_data, tlv_len);
  744. return -EINVAL;
  745. }
  746. /**
  747. * iwl_ucode_callback - callback when firmware was loaded
  748. *
  749. * If loaded successfully, copies the firmware into buffers
  750. * for the card to fetch (via DMA).
  751. */
  752. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  753. {
  754. struct iwl_priv *priv = context;
  755. struct iwl_ucode_header *ucode;
  756. int err;
  757. struct iwlagn_firmware_pieces pieces;
  758. const unsigned int api_max = cfg(priv)->ucode_api_max;
  759. unsigned int api_ok = cfg(priv)->ucode_api_ok;
  760. const unsigned int api_min = cfg(priv)->ucode_api_min;
  761. u32 api_ver;
  762. char buildstr[25];
  763. u32 build;
  764. struct iwlagn_ucode_capabilities ucode_capa = {
  765. .max_probe_length = 200,
  766. .standard_phy_calibration_size =
  767. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE,
  768. };
  769. if (!api_ok)
  770. api_ok = api_max;
  771. memset(&pieces, 0, sizeof(pieces));
  772. if (!ucode_raw) {
  773. if (priv->fw_index <= api_ok)
  774. IWL_ERR(priv,
  775. "request for firmware file '%s' failed.\n",
  776. priv->firmware_name);
  777. goto try_again;
  778. }
  779. IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
  780. priv->firmware_name, ucode_raw->size);
  781. /* Make sure that we got at least the API version number */
  782. if (ucode_raw->size < 4) {
  783. IWL_ERR(priv, "File size way too small!\n");
  784. goto try_again;
  785. }
  786. /* Data from ucode file: header followed by uCode images */
  787. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  788. if (ucode->ver)
  789. err = iwlagn_load_legacy_firmware(priv, ucode_raw, &pieces);
  790. else
  791. err = iwlagn_load_firmware(priv, ucode_raw, &pieces,
  792. &ucode_capa);
  793. if (err)
  794. goto try_again;
  795. api_ver = IWL_UCODE_API(priv->ucode_ver);
  796. build = pieces.build;
  797. /*
  798. * api_ver should match the api version forming part of the
  799. * firmware filename ... but we don't check for that and only rely
  800. * on the API version read from firmware header from here on forward
  801. */
  802. /* no api version check required for experimental uCode */
  803. if (priv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  804. if (api_ver < api_min || api_ver > api_max) {
  805. IWL_ERR(priv,
  806. "Driver unable to support your firmware API. "
  807. "Driver supports v%u, firmware is v%u.\n",
  808. api_max, api_ver);
  809. goto try_again;
  810. }
  811. if (api_ver < api_ok) {
  812. if (api_ok != api_max)
  813. IWL_ERR(priv, "Firmware has old API version, "
  814. "expected v%u through v%u, got v%u.\n",
  815. api_ok, api_max, api_ver);
  816. else
  817. IWL_ERR(priv, "Firmware has old API version, "
  818. "expected v%u, got v%u.\n",
  819. api_max, api_ver);
  820. IWL_ERR(priv, "New firmware can be obtained from "
  821. "http://www.intellinuxwireless.org/.\n");
  822. }
  823. }
  824. if (build)
  825. sprintf(buildstr, " build %u%s", build,
  826. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  827. ? " (EXP)" : "");
  828. else
  829. buildstr[0] = '\0';
  830. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u%s\n",
  831. IWL_UCODE_MAJOR(priv->ucode_ver),
  832. IWL_UCODE_MINOR(priv->ucode_ver),
  833. IWL_UCODE_API(priv->ucode_ver),
  834. IWL_UCODE_SERIAL(priv->ucode_ver),
  835. buildstr);
  836. snprintf(priv->hw->wiphy->fw_version,
  837. sizeof(priv->hw->wiphy->fw_version),
  838. "%u.%u.%u.%u%s",
  839. IWL_UCODE_MAJOR(priv->ucode_ver),
  840. IWL_UCODE_MINOR(priv->ucode_ver),
  841. IWL_UCODE_API(priv->ucode_ver),
  842. IWL_UCODE_SERIAL(priv->ucode_ver),
  843. buildstr);
  844. /*
  845. * For any of the failures below (before allocating pci memory)
  846. * we will try to load a version with a smaller API -- maybe the
  847. * user just got a corrupted version of the latest API.
  848. */
  849. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  850. priv->ucode_ver);
  851. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
  852. pieces.inst_size);
  853. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
  854. pieces.data_size);
  855. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
  856. pieces.init_size);
  857. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
  858. pieces.init_data_size);
  859. /* Verify that uCode images will fit in card's SRAM */
  860. if (pieces.inst_size > hw_params(priv).max_inst_size) {
  861. IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
  862. pieces.inst_size);
  863. goto try_again;
  864. }
  865. if (pieces.data_size > hw_params(priv).max_data_size) {
  866. IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
  867. pieces.data_size);
  868. goto try_again;
  869. }
  870. if (pieces.init_size > hw_params(priv).max_inst_size) {
  871. IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
  872. pieces.init_size);
  873. goto try_again;
  874. }
  875. if (pieces.init_data_size > hw_params(priv).max_data_size) {
  876. IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
  877. pieces.init_data_size);
  878. goto try_again;
  879. }
  880. /* Allocate ucode buffers for card's bus-master loading ... */
  881. /* Runtime instructions and 2 copies of data:
  882. * 1) unmodified from disk
  883. * 2) backup cache for save/restore during power-downs */
  884. if (iwl_alloc_fw_desc(bus(priv), &trans(priv)->ucode_rt.code,
  885. pieces.inst, pieces.inst_size))
  886. goto err_pci_alloc;
  887. if (iwl_alloc_fw_desc(bus(priv), &trans(priv)->ucode_rt.data,
  888. pieces.data, pieces.data_size))
  889. goto err_pci_alloc;
  890. /* Initialization instructions and data */
  891. if (pieces.init_size && pieces.init_data_size) {
  892. if (iwl_alloc_fw_desc(bus(priv), &trans(priv)->ucode_init.code,
  893. pieces.init, pieces.init_size))
  894. goto err_pci_alloc;
  895. if (iwl_alloc_fw_desc(bus(priv), &trans(priv)->ucode_init.data,
  896. pieces.init_data, pieces.init_data_size))
  897. goto err_pci_alloc;
  898. }
  899. /* WoWLAN instructions and data */
  900. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  901. if (iwl_alloc_fw_desc(bus(priv),
  902. &trans(priv)->ucode_wowlan.code,
  903. pieces.wowlan_inst,
  904. pieces.wowlan_inst_size))
  905. goto err_pci_alloc;
  906. if (iwl_alloc_fw_desc(bus(priv),
  907. &trans(priv)->ucode_wowlan.data,
  908. pieces.wowlan_data,
  909. pieces.wowlan_data_size))
  910. goto err_pci_alloc;
  911. }
  912. /* Now that we can no longer fail, copy information */
  913. /*
  914. * The (size - 16) / 12 formula is based on the information recorded
  915. * for each event, which is of mode 1 (including timestamp) for all
  916. * new microcodes that include this information.
  917. */
  918. priv->init_evtlog_ptr = pieces.init_evtlog_ptr;
  919. if (pieces.init_evtlog_size)
  920. priv->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  921. else
  922. priv->init_evtlog_size =
  923. cfg(priv)->base_params->max_event_log_size;
  924. priv->init_errlog_ptr = pieces.init_errlog_ptr;
  925. priv->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  926. if (pieces.inst_evtlog_size)
  927. priv->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  928. else
  929. priv->inst_evtlog_size =
  930. cfg(priv)->base_params->max_event_log_size;
  931. priv->inst_errlog_ptr = pieces.inst_errlog_ptr;
  932. #ifndef CONFIG_IWLWIFI_P2P
  933. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  934. #endif
  935. priv->new_scan_threshold_behaviour =
  936. !!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
  937. if (!(cfg(priv)->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
  938. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  939. /*
  940. * if not PAN, then don't support P2P -- might be a uCode
  941. * packaging bug or due to the eeprom check above
  942. */
  943. if (!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
  944. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
  945. if (ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
  946. priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
  947. priv->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
  948. } else {
  949. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  950. priv->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  951. }
  952. /*
  953. * figure out the offset of chain noise reset and gain commands
  954. * base on the size of standard phy calibration commands table size
  955. */
  956. if (ucode_capa.standard_phy_calibration_size >
  957. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  958. ucode_capa.standard_phy_calibration_size =
  959. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  960. priv->phy_calib_chain_noise_reset_cmd =
  961. ucode_capa.standard_phy_calibration_size;
  962. priv->phy_calib_chain_noise_gain_cmd =
  963. ucode_capa.standard_phy_calibration_size + 1;
  964. /* initialize all valid contexts */
  965. iwl_init_context(priv, ucode_capa.flags);
  966. /**************************************************
  967. * This is still part of probe() in a sense...
  968. *
  969. * 9. Setup and register with mac80211 and debugfs
  970. **************************************************/
  971. err = iwlagn_mac_setup_register(priv, &ucode_capa);
  972. if (err)
  973. goto out_unbind;
  974. err = iwl_dbgfs_register(priv, DRV_NAME);
  975. if (err)
  976. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  977. /* We have our copies now, allow OS release its copies */
  978. release_firmware(ucode_raw);
  979. complete(&priv->firmware_loading_complete);
  980. return;
  981. try_again:
  982. /* try next, if any */
  983. if (iwl_request_firmware(priv, false))
  984. goto out_unbind;
  985. release_firmware(ucode_raw);
  986. return;
  987. err_pci_alloc:
  988. IWL_ERR(priv, "failed to allocate pci memory\n");
  989. iwl_dealloc_ucode(trans(priv));
  990. out_unbind:
  991. complete(&priv->firmware_loading_complete);
  992. device_release_driver(bus(priv)->dev);
  993. release_firmware(ucode_raw);
  994. }
  995. static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
  996. {
  997. struct iwl_ct_kill_config cmd;
  998. struct iwl_ct_kill_throttling_config adv_cmd;
  999. unsigned long flags;
  1000. int ret = 0;
  1001. spin_lock_irqsave(&priv->shrd->lock, flags);
  1002. iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_CLR,
  1003. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  1004. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  1005. priv->thermal_throttle.ct_kill_toggle = false;
  1006. if (cfg(priv)->base_params->support_ct_kill_exit) {
  1007. adv_cmd.critical_temperature_enter =
  1008. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1009. adv_cmd.critical_temperature_exit =
  1010. cpu_to_le32(hw_params(priv).ct_kill_exit_threshold);
  1011. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1012. REPLY_CT_KILL_CONFIG_CMD,
  1013. CMD_SYNC, sizeof(adv_cmd), &adv_cmd);
  1014. if (ret)
  1015. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1016. else
  1017. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1018. "succeeded, critical temperature enter is %d,"
  1019. "exit is %d\n",
  1020. hw_params(priv).ct_kill_threshold,
  1021. hw_params(priv).ct_kill_exit_threshold);
  1022. } else {
  1023. cmd.critical_temperature_R =
  1024. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1025. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1026. REPLY_CT_KILL_CONFIG_CMD,
  1027. CMD_SYNC, sizeof(cmd), &cmd);
  1028. if (ret)
  1029. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1030. else
  1031. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1032. "succeeded, "
  1033. "critical temperature is %d\n",
  1034. hw_params(priv).ct_kill_threshold);
  1035. }
  1036. }
  1037. static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
  1038. {
  1039. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  1040. struct iwl_host_cmd cmd = {
  1041. .id = CALIBRATION_CFG_CMD,
  1042. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  1043. .data = { &calib_cfg_cmd, },
  1044. };
  1045. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  1046. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_RT_CFG_ALL;
  1047. calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
  1048. return iwl_trans_send_cmd(trans(priv), &cmd);
  1049. }
  1050. static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
  1051. {
  1052. struct iwl_tx_ant_config_cmd tx_ant_cmd = {
  1053. .valid = cpu_to_le32(valid_tx_ant),
  1054. };
  1055. if (IWL_UCODE_API(priv->ucode_ver) > 1) {
  1056. IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
  1057. return iwl_trans_send_cmd_pdu(trans(priv),
  1058. TX_ANT_CONFIGURATION_CMD,
  1059. CMD_SYNC,
  1060. sizeof(struct iwl_tx_ant_config_cmd),
  1061. &tx_ant_cmd);
  1062. } else {
  1063. IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
  1064. return -EOPNOTSUPP;
  1065. }
  1066. }
  1067. /**
  1068. * iwl_alive_start - called after REPLY_ALIVE notification received
  1069. * from protocol/runtime uCode (initialization uCode's
  1070. * Alive gets handled by iwl_init_alive_start()).
  1071. */
  1072. int iwl_alive_start(struct iwl_priv *priv)
  1073. {
  1074. int ret = 0;
  1075. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1076. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1077. /* After the ALIVE response, we can send host commands to the uCode */
  1078. set_bit(STATUS_ALIVE, &priv->shrd->status);
  1079. /* Enable watchdog to monitor the driver tx queues */
  1080. iwl_setup_watchdog(priv);
  1081. if (iwl_is_rfkill(priv->shrd))
  1082. return -ERFKILL;
  1083. if (priv->event_log.ucode_trace) {
  1084. /* start collecting data now */
  1085. mod_timer(&priv->ucode_trace, jiffies);
  1086. }
  1087. /* download priority table before any calibration request */
  1088. if (cfg(priv)->bt_params &&
  1089. cfg(priv)->bt_params->advanced_bt_coexist) {
  1090. /* Configure Bluetooth device coexistence support */
  1091. if (cfg(priv)->bt_params->bt_sco_disable)
  1092. priv->bt_enable_pspoll = false;
  1093. else
  1094. priv->bt_enable_pspoll = true;
  1095. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  1096. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  1097. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  1098. iwlagn_send_advance_bt_config(priv);
  1099. priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
  1100. priv->cur_rssi_ctx = NULL;
  1101. iwl_send_prio_tbl(trans(priv));
  1102. /* FIXME: w/a to force change uCode BT state machine */
  1103. ret = iwl_send_bt_env(trans(priv), IWL_BT_COEX_ENV_OPEN,
  1104. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1105. if (ret)
  1106. return ret;
  1107. ret = iwl_send_bt_env(trans(priv), IWL_BT_COEX_ENV_CLOSE,
  1108. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1109. if (ret)
  1110. return ret;
  1111. } else {
  1112. /*
  1113. * default is 2-wire BT coexexistence support
  1114. */
  1115. iwl_send_bt_config(priv);
  1116. }
  1117. /*
  1118. * Perform runtime calibrations, including DC calibration.
  1119. */
  1120. iwlagn_send_calib_cfg_rt(priv, IWL_CALIB_CFG_DC_IDX);
  1121. ieee80211_wake_queues(priv->hw);
  1122. priv->active_rate = IWL_RATES_MASK;
  1123. /* Configure Tx antenna selection based on H/W config */
  1124. iwlagn_send_tx_ant_config(priv, cfg(priv)->valid_tx_ant);
  1125. if (iwl_is_associated_ctx(ctx) && !priv->shrd->wowlan) {
  1126. struct iwl_rxon_cmd *active_rxon =
  1127. (struct iwl_rxon_cmd *)&ctx->active;
  1128. /* apply any changes in staging */
  1129. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1130. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1131. } else {
  1132. struct iwl_rxon_context *tmp;
  1133. /* Initialize our rx_config data */
  1134. for_each_context(priv, tmp)
  1135. iwl_connection_init_rx_config(priv, tmp);
  1136. iwlagn_set_rxon_chain(priv, ctx);
  1137. }
  1138. if (!priv->shrd->wowlan) {
  1139. /* WoWLAN ucode will not reply in the same way, skip it */
  1140. iwl_reset_run_time_calib(priv);
  1141. }
  1142. set_bit(STATUS_READY, &priv->shrd->status);
  1143. /* Configure the adapter for unassociated operation */
  1144. ret = iwlagn_commit_rxon(priv, ctx);
  1145. if (ret)
  1146. return ret;
  1147. /* At this point, the NIC is initialized and operational */
  1148. iwl_rf_kill_ct_config(priv);
  1149. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  1150. return iwl_power_update_mode(priv, true);
  1151. }
  1152. static void iwl_cancel_deferred_work(struct iwl_priv *priv);
  1153. void __iwl_down(struct iwl_priv *priv)
  1154. {
  1155. int exit_pending;
  1156. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  1157. iwl_scan_cancel_timeout(priv, 200);
  1158. /*
  1159. * If active, scanning won't cancel it, so say it expired.
  1160. * No race since we hold the mutex here and a new one
  1161. * can't come in at this time.
  1162. */
  1163. ieee80211_remain_on_channel_expired(priv->hw);
  1164. exit_pending =
  1165. test_and_set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1166. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  1167. * to prevent rearm timer */
  1168. del_timer_sync(&priv->watchdog);
  1169. iwl_clear_ucode_stations(priv, NULL);
  1170. iwl_dealloc_bcast_stations(priv);
  1171. iwl_clear_driver_stations(priv);
  1172. /* reset BT coex data */
  1173. priv->bt_status = 0;
  1174. priv->cur_rssi_ctx = NULL;
  1175. priv->bt_is_sco = 0;
  1176. if (cfg(priv)->bt_params)
  1177. priv->bt_traffic_load =
  1178. cfg(priv)->bt_params->bt_init_traffic_load;
  1179. else
  1180. priv->bt_traffic_load = 0;
  1181. priv->bt_full_concurrent = false;
  1182. priv->bt_ci_compliance = 0;
  1183. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1184. * exiting the module */
  1185. if (!exit_pending)
  1186. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1187. if (priv->mac80211_registered)
  1188. ieee80211_stop_queues(priv->hw);
  1189. iwl_trans_stop_device(trans(priv));
  1190. /* Clear out all status bits but a few that are stable across reset */
  1191. priv->shrd->status &=
  1192. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status) <<
  1193. STATUS_RF_KILL_HW |
  1194. test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) <<
  1195. STATUS_GEO_CONFIGURED |
  1196. test_bit(STATUS_FW_ERROR, &priv->shrd->status) <<
  1197. STATUS_FW_ERROR |
  1198. test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) <<
  1199. STATUS_EXIT_PENDING;
  1200. dev_kfree_skb(priv->beacon_skb);
  1201. priv->beacon_skb = NULL;
  1202. }
  1203. void iwl_down(struct iwl_priv *priv)
  1204. {
  1205. mutex_lock(&priv->shrd->mutex);
  1206. __iwl_down(priv);
  1207. mutex_unlock(&priv->shrd->mutex);
  1208. iwl_cancel_deferred_work(priv);
  1209. }
  1210. /*****************************************************************************
  1211. *
  1212. * Workqueue callbacks
  1213. *
  1214. *****************************************************************************/
  1215. static void iwl_bg_run_time_calib_work(struct work_struct *work)
  1216. {
  1217. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  1218. run_time_calib_work);
  1219. mutex_lock(&priv->shrd->mutex);
  1220. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  1221. test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  1222. mutex_unlock(&priv->shrd->mutex);
  1223. return;
  1224. }
  1225. if (priv->start_calib) {
  1226. iwl_chain_noise_calibration(priv);
  1227. iwl_sensitivity_calibration(priv);
  1228. }
  1229. mutex_unlock(&priv->shrd->mutex);
  1230. }
  1231. void iwlagn_prepare_restart(struct iwl_priv *priv)
  1232. {
  1233. struct iwl_rxon_context *ctx;
  1234. bool bt_full_concurrent;
  1235. u8 bt_ci_compliance;
  1236. u8 bt_load;
  1237. u8 bt_status;
  1238. bool bt_is_sco;
  1239. lockdep_assert_held(&priv->shrd->mutex);
  1240. for_each_context(priv, ctx)
  1241. ctx->vif = NULL;
  1242. priv->is_open = 0;
  1243. /*
  1244. * __iwl_down() will clear the BT status variables,
  1245. * which is correct, but when we restart we really
  1246. * want to keep them so restore them afterwards.
  1247. *
  1248. * The restart process will later pick them up and
  1249. * re-configure the hw when we reconfigure the BT
  1250. * command.
  1251. */
  1252. bt_full_concurrent = priv->bt_full_concurrent;
  1253. bt_ci_compliance = priv->bt_ci_compliance;
  1254. bt_load = priv->bt_traffic_load;
  1255. bt_status = priv->bt_status;
  1256. bt_is_sco = priv->bt_is_sco;
  1257. __iwl_down(priv);
  1258. priv->bt_full_concurrent = bt_full_concurrent;
  1259. priv->bt_ci_compliance = bt_ci_compliance;
  1260. priv->bt_traffic_load = bt_load;
  1261. priv->bt_status = bt_status;
  1262. priv->bt_is_sco = bt_is_sco;
  1263. }
  1264. static void iwl_bg_restart(struct work_struct *data)
  1265. {
  1266. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  1267. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1268. return;
  1269. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->shrd->status)) {
  1270. mutex_lock(&priv->shrd->mutex);
  1271. iwlagn_prepare_restart(priv);
  1272. mutex_unlock(&priv->shrd->mutex);
  1273. iwl_cancel_deferred_work(priv);
  1274. ieee80211_restart_hw(priv->hw);
  1275. } else {
  1276. WARN_ON(1);
  1277. }
  1278. }
  1279. void iwlagn_disable_roc(struct iwl_priv *priv)
  1280. {
  1281. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  1282. lockdep_assert_held(&priv->shrd->mutex);
  1283. if (!priv->hw_roc_setup)
  1284. return;
  1285. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  1286. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1287. priv->hw_roc_channel = NULL;
  1288. memset(ctx->staging.node_addr, 0, ETH_ALEN);
  1289. iwlagn_commit_rxon(priv, ctx);
  1290. ctx->is_active = false;
  1291. priv->hw_roc_setup = false;
  1292. }
  1293. static void iwlagn_disable_roc_work(struct work_struct *work)
  1294. {
  1295. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  1296. hw_roc_disable_work.work);
  1297. mutex_lock(&priv->shrd->mutex);
  1298. iwlagn_disable_roc(priv);
  1299. mutex_unlock(&priv->shrd->mutex);
  1300. }
  1301. /*****************************************************************************
  1302. *
  1303. * driver setup and teardown
  1304. *
  1305. *****************************************************************************/
  1306. static void iwl_setup_deferred_work(struct iwl_priv *priv)
  1307. {
  1308. priv->shrd->workqueue = create_singlethread_workqueue(DRV_NAME);
  1309. init_waitqueue_head(&priv->shrd->wait_command_queue);
  1310. INIT_WORK(&priv->restart, iwl_bg_restart);
  1311. INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
  1312. INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
  1313. INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
  1314. INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
  1315. INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
  1316. INIT_DELAYED_WORK(&priv->hw_roc_disable_work,
  1317. iwlagn_disable_roc_work);
  1318. iwl_setup_scan_deferred_work(priv);
  1319. if (cfg(priv)->lib->bt_setup_deferred_work)
  1320. cfg(priv)->lib->bt_setup_deferred_work(priv);
  1321. init_timer(&priv->statistics_periodic);
  1322. priv->statistics_periodic.data = (unsigned long)priv;
  1323. priv->statistics_periodic.function = iwl_bg_statistics_periodic;
  1324. init_timer(&priv->ucode_trace);
  1325. priv->ucode_trace.data = (unsigned long)priv;
  1326. priv->ucode_trace.function = iwl_bg_ucode_trace;
  1327. init_timer(&priv->watchdog);
  1328. priv->watchdog.data = (unsigned long)priv;
  1329. priv->watchdog.function = iwl_bg_watchdog;
  1330. }
  1331. static void iwl_cancel_deferred_work(struct iwl_priv *priv)
  1332. {
  1333. if (cfg(priv)->lib->cancel_deferred_work)
  1334. cfg(priv)->lib->cancel_deferred_work(priv);
  1335. cancel_work_sync(&priv->run_time_calib_work);
  1336. cancel_work_sync(&priv->beacon_update);
  1337. iwl_cancel_scan_deferred_work(priv);
  1338. cancel_work_sync(&priv->bt_full_concurrency);
  1339. cancel_work_sync(&priv->bt_runtime_config);
  1340. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  1341. del_timer_sync(&priv->statistics_periodic);
  1342. del_timer_sync(&priv->ucode_trace);
  1343. }
  1344. static void iwl_init_hw_rates(struct iwl_priv *priv,
  1345. struct ieee80211_rate *rates)
  1346. {
  1347. int i;
  1348. for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
  1349. rates[i].bitrate = iwl_rates[i].ieee * 5;
  1350. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  1351. rates[i].hw_value_short = i;
  1352. rates[i].flags = 0;
  1353. if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
  1354. /*
  1355. * If CCK != 1M then set short preamble rate flag.
  1356. */
  1357. rates[i].flags |=
  1358. (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  1359. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1360. }
  1361. }
  1362. }
  1363. static int iwl_init_drv(struct iwl_priv *priv)
  1364. {
  1365. int ret;
  1366. spin_lock_init(&priv->shrd->sta_lock);
  1367. mutex_init(&priv->shrd->mutex);
  1368. INIT_LIST_HEAD(&trans(priv)->calib_results);
  1369. priv->ieee_channels = NULL;
  1370. priv->ieee_rates = NULL;
  1371. priv->band = IEEE80211_BAND_2GHZ;
  1372. priv->iw_mode = NL80211_IFTYPE_STATION;
  1373. priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  1374. priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  1375. priv->agg_tids_count = 0;
  1376. /* initialize force reset */
  1377. priv->force_reset[IWL_RF_RESET].reset_duration =
  1378. IWL_DELAY_NEXT_FORCE_RF_RESET;
  1379. priv->force_reset[IWL_FW_RESET].reset_duration =
  1380. IWL_DELAY_NEXT_FORCE_FW_RELOAD;
  1381. priv->rx_statistics_jiffies = jiffies;
  1382. /* Choose which receivers/antennas to use */
  1383. iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
  1384. iwl_init_scan_params(priv);
  1385. /* init bt coex */
  1386. if (cfg(priv)->bt_params &&
  1387. cfg(priv)->bt_params->advanced_bt_coexist) {
  1388. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  1389. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  1390. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  1391. priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
  1392. priv->bt_duration = BT_DURATION_LIMIT_DEF;
  1393. priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
  1394. }
  1395. ret = iwl_init_channel_map(priv);
  1396. if (ret) {
  1397. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  1398. goto err;
  1399. }
  1400. ret = iwl_init_geos(priv);
  1401. if (ret) {
  1402. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  1403. goto err_free_channel_map;
  1404. }
  1405. iwl_init_hw_rates(priv, priv->ieee_rates);
  1406. return 0;
  1407. err_free_channel_map:
  1408. iwl_free_channel_map(priv);
  1409. err:
  1410. return ret;
  1411. }
  1412. static void iwl_uninit_drv(struct iwl_priv *priv)
  1413. {
  1414. iwl_free_geos(priv);
  1415. iwl_free_channel_map(priv);
  1416. if (priv->tx_cmd_pool)
  1417. kmem_cache_destroy(priv->tx_cmd_pool);
  1418. kfree(priv->scan_cmd);
  1419. kfree(priv->beacon_cmd);
  1420. kfree(rcu_dereference_raw(priv->noa_data));
  1421. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1422. kfree(priv->wowlan_sram);
  1423. #endif
  1424. }
  1425. static u32 iwl_hw_detect(struct iwl_priv *priv)
  1426. {
  1427. return iwl_read32(trans(priv), CSR_HW_REV);
  1428. }
  1429. /* Size of one Rx buffer in host DRAM */
  1430. #define IWL_RX_BUF_SIZE_4K (4 * 1024)
  1431. #define IWL_RX_BUF_SIZE_8K (8 * 1024)
  1432. static int iwl_set_hw_params(struct iwl_priv *priv)
  1433. {
  1434. if (iwlagn_mod_params.amsdu_size_8K)
  1435. hw_params(priv).rx_page_order =
  1436. get_order(IWL_RX_BUF_SIZE_8K);
  1437. else
  1438. hw_params(priv).rx_page_order =
  1439. get_order(IWL_RX_BUF_SIZE_4K);
  1440. if (iwlagn_mod_params.disable_11n & IWL_DISABLE_HT_ALL)
  1441. cfg(priv)->sku &= ~EEPROM_SKU_CAP_11N_ENABLE;
  1442. hw_params(priv).num_ampdu_queues =
  1443. cfg(priv)->base_params->num_of_ampdu_queues;
  1444. hw_params(priv).shadow_reg_enable =
  1445. cfg(priv)->base_params->shadow_reg_enable;
  1446. hw_params(priv).sku = cfg(priv)->sku;
  1447. hw_params(priv).wd_timeout = cfg(priv)->base_params->wd_timeout;
  1448. /* Device-specific setup */
  1449. return cfg(priv)->lib->set_hw_params(priv);
  1450. }
  1451. static void iwl_debug_config(struct iwl_priv *priv)
  1452. {
  1453. dev_printk(KERN_INFO, trans(priv)->dev, "CONFIG_IWLWIFI_DEBUG "
  1454. #ifdef CONFIG_IWLWIFI_DEBUG
  1455. "enabled\n");
  1456. #else
  1457. "disabled\n");
  1458. #endif
  1459. dev_printk(KERN_INFO, trans(priv)->dev, "CONFIG_IWLWIFI_DEBUGFS "
  1460. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1461. "enabled\n");
  1462. #else
  1463. "disabled\n");
  1464. #endif
  1465. dev_printk(KERN_INFO, trans(priv)->dev, "CONFIG_IWLWIFI_DEVICE_TRACING "
  1466. #ifdef CONFIG_IWLWIFI_DEVICE_TRACING
  1467. "enabled\n");
  1468. #else
  1469. "disabled\n");
  1470. #endif
  1471. dev_printk(KERN_INFO, trans(priv)->dev, "CONFIG_IWLWIFI_DEVICE_TESTMODE "
  1472. #ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
  1473. "enabled\n");
  1474. #else
  1475. "disabled\n");
  1476. #endif
  1477. dev_printk(KERN_INFO, trans(priv)->dev, "CONFIG_IWLWIFI_P2P "
  1478. #ifdef CONFIG_IWLWIFI_P2P
  1479. "enabled\n");
  1480. #else
  1481. "disabled\n");
  1482. #endif
  1483. }
  1484. int iwl_probe(struct iwl_bus *bus, const struct iwl_trans_ops *trans_ops,
  1485. struct iwl_cfg *cfg)
  1486. {
  1487. int err = 0;
  1488. struct iwl_priv *priv;
  1489. struct ieee80211_hw *hw;
  1490. u16 num_mac;
  1491. u32 hw_rev;
  1492. /************************
  1493. * 1. Allocating HW data
  1494. ************************/
  1495. hw = iwl_alloc_all();
  1496. if (!hw) {
  1497. pr_err("%s: Cannot allocate network device\n", cfg->name);
  1498. err = -ENOMEM;
  1499. goto out;
  1500. }
  1501. priv = hw->priv;
  1502. priv->shrd = bus->shrd;
  1503. priv->shrd->priv = priv;
  1504. /* At this point both hw and priv are allocated. */
  1505. SET_IEEE80211_DEV(hw, bus(priv)->dev);
  1506. /* what debugging capabilities we have */
  1507. iwl_debug_config(priv);
  1508. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  1509. cfg(priv) = cfg;
  1510. /* is antenna coupling more than 35dB ? */
  1511. priv->bt_ant_couple_ok =
  1512. (iwlagn_mod_params.ant_coupling >
  1513. IWL_BT_ANTENNA_COUPLING_THRESHOLD) ?
  1514. true : false;
  1515. /* enable/disable bt channel inhibition */
  1516. priv->bt_ch_announce = iwlagn_mod_params.bt_ch_announce;
  1517. IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
  1518. (priv->bt_ch_announce) ? "On" : "Off");
  1519. if (iwl_alloc_traffic_mem(priv))
  1520. IWL_ERR(priv, "Not enough memory to generate traffic log\n");
  1521. /* these spin locks will be used in apm_ops.init and EEPROM access
  1522. * we should init now
  1523. */
  1524. spin_lock_init(&trans(priv)->reg_lock);
  1525. spin_lock_init(&priv->shrd->lock);
  1526. /*
  1527. * stop and reset the on-board processor just in case it is in a
  1528. * strange state ... like being left stranded by a primary kernel
  1529. * and this is now the kdump kernel trying to start up
  1530. */
  1531. iwl_write32(trans(priv), CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  1532. /***********************
  1533. * 3. Read REV register
  1534. ***********************/
  1535. hw_rev = iwl_hw_detect(priv);
  1536. IWL_INFO(priv, "Detected %s, REV=0x%X\n",
  1537. cfg(priv)->name, hw_rev);
  1538. err = iwl_trans_request_irq(trans(priv));
  1539. if (err)
  1540. goto out_free_traffic_mem;
  1541. if (iwl_trans_prepare_card_hw(trans(priv))) {
  1542. err = -EIO;
  1543. IWL_WARN(priv, "Failed, HW not ready\n");
  1544. goto out_free_traffic_mem;
  1545. }
  1546. /*****************
  1547. * 4. Read EEPROM
  1548. *****************/
  1549. /* switch the NIC on before accessing the EEPROM */
  1550. iwl_apm_init(priv);
  1551. /* Read the EEPROM */
  1552. err = iwl_eeprom_init(priv, hw_rev);
  1553. /* Reset chip to save power until we load uCode during "up". */
  1554. iwl_apm_stop(priv);
  1555. if (err) {
  1556. IWL_ERR(priv, "Unable to init EEPROM\n");
  1557. goto out_free_traffic_mem;
  1558. }
  1559. err = iwl_eeprom_check_version(priv);
  1560. if (err)
  1561. goto out_free_eeprom;
  1562. err = iwl_eeprom_check_sku(priv);
  1563. if (err)
  1564. goto out_free_eeprom;
  1565. /* extract MAC Address */
  1566. iwl_eeprom_get_mac(priv->shrd, priv->addresses[0].addr);
  1567. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
  1568. priv->hw->wiphy->addresses = priv->addresses;
  1569. priv->hw->wiphy->n_addresses = 1;
  1570. num_mac = iwl_eeprom_query16(priv->shrd, EEPROM_NUM_MAC_ADDRESS);
  1571. if (num_mac > 1) {
  1572. memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
  1573. ETH_ALEN);
  1574. priv->addresses[1].addr[5]++;
  1575. priv->hw->wiphy->n_addresses++;
  1576. }
  1577. /************************
  1578. * 5. Setup HW constants
  1579. ************************/
  1580. if (iwl_set_hw_params(priv)) {
  1581. err = -ENOENT;
  1582. IWL_ERR(priv, "failed to set hw parameters\n");
  1583. goto out_free_eeprom;
  1584. }
  1585. /*******************
  1586. * 6. Setup priv
  1587. *******************/
  1588. err = iwl_init_drv(priv);
  1589. if (err)
  1590. goto out_free_eeprom;
  1591. /* At this point both hw and priv are initialized. */
  1592. /********************
  1593. * 7. Setup services
  1594. ********************/
  1595. iwl_setup_deferred_work(priv);
  1596. iwl_setup_rx_handlers(priv);
  1597. iwl_testmode_init(priv);
  1598. /*********************************************
  1599. * 8. Enable interrupts
  1600. *********************************************/
  1601. iwl_enable_rfkill_int(priv);
  1602. /* If platform's RF_KILL switch is NOT set to KILL */
  1603. if (iwl_read32(trans(priv),
  1604. CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  1605. clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  1606. else
  1607. set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  1608. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  1609. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
  1610. iwl_power_initialize(priv);
  1611. iwl_tt_initialize(priv);
  1612. init_completion(&priv->firmware_loading_complete);
  1613. err = iwl_request_firmware(priv, true);
  1614. if (err)
  1615. goto out_destroy_workqueue;
  1616. return 0;
  1617. out_destroy_workqueue:
  1618. destroy_workqueue(priv->shrd->workqueue);
  1619. priv->shrd->workqueue = NULL;
  1620. iwl_uninit_drv(priv);
  1621. out_free_eeprom:
  1622. iwl_eeprom_free(priv->shrd);
  1623. out_free_traffic_mem:
  1624. iwl_free_traffic_mem(priv);
  1625. ieee80211_free_hw(priv->hw);
  1626. out:
  1627. return err;
  1628. }
  1629. void __devexit iwl_remove(struct iwl_priv * priv)
  1630. {
  1631. wait_for_completion(&priv->firmware_loading_complete);
  1632. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  1633. iwl_dbgfs_unregister(priv);
  1634. /* ieee80211_unregister_hw call wil cause iwlagn_mac_stop to
  1635. * to be called and iwl_down since we are removing the device
  1636. * we need to set STATUS_EXIT_PENDING bit.
  1637. */
  1638. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1639. iwl_testmode_cleanup(priv);
  1640. iwlagn_mac_unregister(priv);
  1641. iwl_tt_exit(priv);
  1642. /*This will stop the queues, move the device to low power state */
  1643. iwl_trans_stop_device(trans(priv));
  1644. iwl_dealloc_ucode(trans(priv));
  1645. iwl_eeprom_free(priv->shrd);
  1646. /*netif_stop_queue(dev); */
  1647. flush_workqueue(priv->shrd->workqueue);
  1648. /* ieee80211_unregister_hw calls iwlagn_mac_stop, which flushes
  1649. * priv->shrd->workqueue... so we can't take down the workqueue
  1650. * until now... */
  1651. destroy_workqueue(priv->shrd->workqueue);
  1652. priv->shrd->workqueue = NULL;
  1653. iwl_free_traffic_mem(priv);
  1654. iwl_uninit_drv(priv);
  1655. dev_kfree_skb(priv->beacon_skb);
  1656. ieee80211_free_hw(priv->hw);
  1657. }
  1658. /*****************************************************************************
  1659. *
  1660. * driver and module entry point
  1661. *
  1662. *****************************************************************************/
  1663. static int __init iwl_init(void)
  1664. {
  1665. int ret;
  1666. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  1667. pr_info(DRV_COPYRIGHT "\n");
  1668. ret = iwlagn_rate_control_register();
  1669. if (ret) {
  1670. pr_err("Unable to register rate control algorithm: %d\n", ret);
  1671. return ret;
  1672. }
  1673. ret = iwl_pci_register_driver();
  1674. if (ret)
  1675. goto error_register;
  1676. return ret;
  1677. error_register:
  1678. iwlagn_rate_control_unregister();
  1679. return ret;
  1680. }
  1681. static void __exit iwl_exit(void)
  1682. {
  1683. iwl_pci_unregister_driver();
  1684. iwlagn_rate_control_unregister();
  1685. }
  1686. module_exit(iwl_exit);
  1687. module_init(iwl_init);
  1688. #ifdef CONFIG_IWLWIFI_DEBUG
  1689. module_param_named(debug, iwlagn_mod_params.debug_level, uint,
  1690. S_IRUGO | S_IWUSR);
  1691. MODULE_PARM_DESC(debug, "debug output mask");
  1692. #endif
  1693. module_param_named(swcrypto, iwlagn_mod_params.sw_crypto, int, S_IRUGO);
  1694. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  1695. module_param_named(queues_num, iwlagn_mod_params.num_of_queues, int, S_IRUGO);
  1696. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  1697. module_param_named(11n_disable, iwlagn_mod_params.disable_11n, uint, S_IRUGO);
  1698. MODULE_PARM_DESC(11n_disable,
  1699. "disable 11n functionality, bitmap: 1: full, 2: agg TX, 4: agg RX");
  1700. module_param_named(amsdu_size_8K, iwlagn_mod_params.amsdu_size_8K,
  1701. int, S_IRUGO);
  1702. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  1703. module_param_named(fw_restart, iwlagn_mod_params.restart_fw, int, S_IRUGO);
  1704. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  1705. module_param_named(ucode_alternative,
  1706. iwlagn_mod_params.wanted_ucode_alternative,
  1707. int, S_IRUGO);
  1708. MODULE_PARM_DESC(ucode_alternative,
  1709. "specify ucode alternative to use from ucode file");
  1710. module_param_named(antenna_coupling, iwlagn_mod_params.ant_coupling,
  1711. int, S_IRUGO);
  1712. MODULE_PARM_DESC(antenna_coupling,
  1713. "specify antenna coupling in dB (defualt: 0 dB)");
  1714. module_param_named(bt_ch_inhibition, iwlagn_mod_params.bt_ch_announce,
  1715. bool, S_IRUGO);
  1716. MODULE_PARM_DESC(bt_ch_inhibition,
  1717. "Enable BT channel inhibition (default: enable)");
  1718. module_param_named(plcp_check, iwlagn_mod_params.plcp_check, bool, S_IRUGO);
  1719. MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
  1720. module_param_named(ack_check, iwlagn_mod_params.ack_check, bool, S_IRUGO);
  1721. MODULE_PARM_DESC(ack_check, "Check ack health (default: 0 [disabled])");
  1722. module_param_named(wd_disable, iwlagn_mod_params.wd_disable, int, S_IRUGO);
  1723. MODULE_PARM_DESC(wd_disable,
  1724. "Disable stuck queue watchdog timer 0=system default, "
  1725. "1=disable, 2=enable (default: 0)");
  1726. /*
  1727. * set bt_coex_active to true, uCode will do kill/defer
  1728. * every time the priority line is asserted (BT is sending signals on the
  1729. * priority line in the PCIx).
  1730. * set bt_coex_active to false, uCode will ignore the BT activity and
  1731. * perform the normal operation
  1732. *
  1733. * User might experience transmit issue on some platform due to WiFi/BT
  1734. * co-exist problem. The possible behaviors are:
  1735. * Able to scan and finding all the available AP
  1736. * Not able to associate with any AP
  1737. * On those platforms, WiFi communication can be restored by set
  1738. * "bt_coex_active" module parameter to "false"
  1739. *
  1740. * default: bt_coex_active = true (BT_COEX_ENABLE)
  1741. */
  1742. module_param_named(bt_coex_active, iwlagn_mod_params.bt_coex_active,
  1743. bool, S_IRUGO);
  1744. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  1745. module_param_named(led_mode, iwlagn_mod_params.led_mode, int, S_IRUGO);
  1746. MODULE_PARM_DESC(led_mode, "0=system default, "
  1747. "1=On(RF On)/Off(RF Off), 2=blinking, 3=Off (default: 0)");
  1748. module_param_named(power_save, iwlagn_mod_params.power_save,
  1749. bool, S_IRUGO);
  1750. MODULE_PARM_DESC(power_save,
  1751. "enable WiFi power management (default: disable)");
  1752. module_param_named(power_level, iwlagn_mod_params.power_level,
  1753. int, S_IRUGO);
  1754. MODULE_PARM_DESC(power_level,
  1755. "default power save level (range from 1 - 5, default: 1)");
  1756. module_param_named(auto_agg, iwlagn_mod_params.auto_agg,
  1757. bool, S_IRUGO);
  1758. MODULE_PARM_DESC(auto_agg,
  1759. "enable agg w/o check traffic load (default: enable)");
  1760. /*
  1761. * For now, keep using power level 1 instead of automatically
  1762. * adjusting ...
  1763. */
  1764. module_param_named(no_sleep_autoadjust, iwlagn_mod_params.no_sleep_autoadjust,
  1765. bool, S_IRUGO);
  1766. MODULE_PARM_DESC(no_sleep_autoadjust,
  1767. "don't automatically adjust sleep level "
  1768. "according to maximum network latency (default: true)");