iwl-agn-lib.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  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/etherdevice.h>
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/sched.h>
  34. #include "iwl-dev.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-sta.h"
  41. static inline u32 iwlagn_get_scd_ssn(struct iwlagn_tx_resp *tx_resp)
  42. {
  43. return le32_to_cpup((__le32 *)&tx_resp->status +
  44. tx_resp->frame_count) & MAX_SN;
  45. }
  46. static void iwlagn_count_tx_err_status(struct iwl_priv *priv, u16 status)
  47. {
  48. status &= TX_STATUS_MSK;
  49. switch (status) {
  50. case TX_STATUS_POSTPONE_DELAY:
  51. priv->_agn.reply_tx_stats.pp_delay++;
  52. break;
  53. case TX_STATUS_POSTPONE_FEW_BYTES:
  54. priv->_agn.reply_tx_stats.pp_few_bytes++;
  55. break;
  56. case TX_STATUS_POSTPONE_BT_PRIO:
  57. priv->_agn.reply_tx_stats.pp_bt_prio++;
  58. break;
  59. case TX_STATUS_POSTPONE_QUIET_PERIOD:
  60. priv->_agn.reply_tx_stats.pp_quiet_period++;
  61. break;
  62. case TX_STATUS_POSTPONE_CALC_TTAK:
  63. priv->_agn.reply_tx_stats.pp_calc_ttak++;
  64. break;
  65. case TX_STATUS_FAIL_INTERNAL_CROSSED_RETRY:
  66. priv->_agn.reply_tx_stats.int_crossed_retry++;
  67. break;
  68. case TX_STATUS_FAIL_SHORT_LIMIT:
  69. priv->_agn.reply_tx_stats.short_limit++;
  70. break;
  71. case TX_STATUS_FAIL_LONG_LIMIT:
  72. priv->_agn.reply_tx_stats.long_limit++;
  73. break;
  74. case TX_STATUS_FAIL_FIFO_UNDERRUN:
  75. priv->_agn.reply_tx_stats.fifo_underrun++;
  76. break;
  77. case TX_STATUS_FAIL_DRAIN_FLOW:
  78. priv->_agn.reply_tx_stats.drain_flow++;
  79. break;
  80. case TX_STATUS_FAIL_RFKILL_FLUSH:
  81. priv->_agn.reply_tx_stats.rfkill_flush++;
  82. break;
  83. case TX_STATUS_FAIL_LIFE_EXPIRE:
  84. priv->_agn.reply_tx_stats.life_expire++;
  85. break;
  86. case TX_STATUS_FAIL_DEST_PS:
  87. priv->_agn.reply_tx_stats.dest_ps++;
  88. break;
  89. case TX_STATUS_FAIL_HOST_ABORTED:
  90. priv->_agn.reply_tx_stats.host_abort++;
  91. break;
  92. case TX_STATUS_FAIL_BT_RETRY:
  93. priv->_agn.reply_tx_stats.bt_retry++;
  94. break;
  95. case TX_STATUS_FAIL_STA_INVALID:
  96. priv->_agn.reply_tx_stats.sta_invalid++;
  97. break;
  98. case TX_STATUS_FAIL_FRAG_DROPPED:
  99. priv->_agn.reply_tx_stats.frag_drop++;
  100. break;
  101. case TX_STATUS_FAIL_TID_DISABLE:
  102. priv->_agn.reply_tx_stats.tid_disable++;
  103. break;
  104. case TX_STATUS_FAIL_FIFO_FLUSHED:
  105. priv->_agn.reply_tx_stats.fifo_flush++;
  106. break;
  107. case TX_STATUS_FAIL_INSUFFICIENT_CF_POLL:
  108. priv->_agn.reply_tx_stats.insuff_cf_poll++;
  109. break;
  110. case TX_STATUS_FAIL_PASSIVE_NO_RX:
  111. priv->_agn.reply_tx_stats.fail_hw_drop++;
  112. break;
  113. case TX_STATUS_FAIL_NO_BEACON_ON_RADAR:
  114. priv->_agn.reply_tx_stats.sta_color_mismatch++;
  115. break;
  116. default:
  117. priv->_agn.reply_tx_stats.unknown++;
  118. break;
  119. }
  120. }
  121. static void iwlagn_count_agg_tx_err_status(struct iwl_priv *priv, u16 status)
  122. {
  123. status &= AGG_TX_STATUS_MSK;
  124. switch (status) {
  125. case AGG_TX_STATE_UNDERRUN_MSK:
  126. priv->_agn.reply_agg_tx_stats.underrun++;
  127. break;
  128. case AGG_TX_STATE_BT_PRIO_MSK:
  129. priv->_agn.reply_agg_tx_stats.bt_prio++;
  130. break;
  131. case AGG_TX_STATE_FEW_BYTES_MSK:
  132. priv->_agn.reply_agg_tx_stats.few_bytes++;
  133. break;
  134. case AGG_TX_STATE_ABORT_MSK:
  135. priv->_agn.reply_agg_tx_stats.abort++;
  136. break;
  137. case AGG_TX_STATE_LAST_SENT_TTL_MSK:
  138. priv->_agn.reply_agg_tx_stats.last_sent_ttl++;
  139. break;
  140. case AGG_TX_STATE_LAST_SENT_TRY_CNT_MSK:
  141. priv->_agn.reply_agg_tx_stats.last_sent_try++;
  142. break;
  143. case AGG_TX_STATE_LAST_SENT_BT_KILL_MSK:
  144. priv->_agn.reply_agg_tx_stats.last_sent_bt_kill++;
  145. break;
  146. case AGG_TX_STATE_SCD_QUERY_MSK:
  147. priv->_agn.reply_agg_tx_stats.scd_query++;
  148. break;
  149. case AGG_TX_STATE_TEST_BAD_CRC32_MSK:
  150. priv->_agn.reply_agg_tx_stats.bad_crc32++;
  151. break;
  152. case AGG_TX_STATE_RESPONSE_MSK:
  153. priv->_agn.reply_agg_tx_stats.response++;
  154. break;
  155. case AGG_TX_STATE_DUMP_TX_MSK:
  156. priv->_agn.reply_agg_tx_stats.dump_tx++;
  157. break;
  158. case AGG_TX_STATE_DELAY_TX_MSK:
  159. priv->_agn.reply_agg_tx_stats.delay_tx++;
  160. break;
  161. default:
  162. priv->_agn.reply_agg_tx_stats.unknown++;
  163. break;
  164. }
  165. }
  166. static void iwlagn_set_tx_status(struct iwl_priv *priv,
  167. struct ieee80211_tx_info *info,
  168. struct iwl_rxon_context *ctx,
  169. struct iwlagn_tx_resp *tx_resp,
  170. int txq_id, bool is_agg)
  171. {
  172. u16 status = le16_to_cpu(tx_resp->status.status);
  173. info->status.rates[0].count = tx_resp->failure_frame + 1;
  174. if (is_agg)
  175. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  176. info->flags |= iwl_tx_status_to_mac80211(status);
  177. iwlagn_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
  178. info);
  179. if (!iwl_is_tx_success(status))
  180. iwlagn_count_tx_err_status(priv, status);
  181. if (status == TX_STATUS_FAIL_PASSIVE_NO_RX &&
  182. iwl_is_associated_ctx(ctx) && ctx->vif &&
  183. ctx->vif->type == NL80211_IFTYPE_STATION) {
  184. ctx->last_tx_rejected = true;
  185. iwl_stop_queue(priv, &priv->txq[txq_id]);
  186. }
  187. IWL_DEBUG_TX_REPLY(priv, "TXQ %d status %s (0x%08x) rate_n_flags "
  188. "0x%x retries %d\n",
  189. txq_id,
  190. iwl_get_tx_fail_reason(status), status,
  191. le32_to_cpu(tx_resp->rate_n_flags),
  192. tx_resp->failure_frame);
  193. }
  194. #ifdef CONFIG_IWLWIFI_DEBUG
  195. #define AGG_TX_STATE_FAIL(x) case AGG_TX_STATE_ ## x: return #x
  196. const char *iwl_get_agg_tx_fail_reason(u16 status)
  197. {
  198. status &= AGG_TX_STATUS_MSK;
  199. switch (status) {
  200. case AGG_TX_STATE_TRANSMITTED:
  201. return "SUCCESS";
  202. AGG_TX_STATE_FAIL(UNDERRUN_MSK);
  203. AGG_TX_STATE_FAIL(BT_PRIO_MSK);
  204. AGG_TX_STATE_FAIL(FEW_BYTES_MSK);
  205. AGG_TX_STATE_FAIL(ABORT_MSK);
  206. AGG_TX_STATE_FAIL(LAST_SENT_TTL_MSK);
  207. AGG_TX_STATE_FAIL(LAST_SENT_TRY_CNT_MSK);
  208. AGG_TX_STATE_FAIL(LAST_SENT_BT_KILL_MSK);
  209. AGG_TX_STATE_FAIL(SCD_QUERY_MSK);
  210. AGG_TX_STATE_FAIL(TEST_BAD_CRC32_MSK);
  211. AGG_TX_STATE_FAIL(RESPONSE_MSK);
  212. AGG_TX_STATE_FAIL(DUMP_TX_MSK);
  213. AGG_TX_STATE_FAIL(DELAY_TX_MSK);
  214. }
  215. return "UNKNOWN";
  216. }
  217. #endif /* CONFIG_IWLWIFI_DEBUG */
  218. static int iwlagn_tx_status_reply_tx(struct iwl_priv *priv,
  219. struct iwl_ht_agg *agg,
  220. struct iwlagn_tx_resp *tx_resp,
  221. int txq_id, u16 start_idx)
  222. {
  223. u16 status;
  224. struct agg_tx_status *frame_status = &tx_resp->status;
  225. struct ieee80211_hdr *hdr = NULL;
  226. int i, sh, idx;
  227. u16 seq;
  228. if (agg->wait_for_ba)
  229. IWL_DEBUG_TX_REPLY(priv, "got tx response w/o block-ack\n");
  230. agg->frame_count = tx_resp->frame_count;
  231. agg->start_idx = start_idx;
  232. agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  233. agg->bitmap = 0;
  234. /* # frames attempted by Tx command */
  235. if (agg->frame_count == 1) {
  236. struct iwl_tx_info *txb;
  237. /* Only one frame was attempted; no block-ack will arrive */
  238. idx = start_idx;
  239. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, StartIdx=%d idx=%d\n",
  240. agg->frame_count, agg->start_idx, idx);
  241. txb = &priv->txq[txq_id].txb[idx];
  242. iwlagn_set_tx_status(priv, IEEE80211_SKB_CB(txb->skb),
  243. txb->ctx, tx_resp, txq_id, true);
  244. agg->wait_for_ba = 0;
  245. } else {
  246. /* Two or more frames were attempted; expect block-ack */
  247. u64 bitmap = 0;
  248. /*
  249. * Start is the lowest frame sent. It may not be the first
  250. * frame in the batch; we figure this out dynamically during
  251. * the following loop.
  252. */
  253. int start = agg->start_idx;
  254. /* Construct bit-map of pending frames within Tx window */
  255. for (i = 0; i < agg->frame_count; i++) {
  256. u16 sc;
  257. status = le16_to_cpu(frame_status[i].status);
  258. seq = le16_to_cpu(frame_status[i].sequence);
  259. idx = SEQ_TO_INDEX(seq);
  260. txq_id = SEQ_TO_QUEUE(seq);
  261. if (status & AGG_TX_STATUS_MSK)
  262. iwlagn_count_agg_tx_err_status(priv, status);
  263. if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
  264. AGG_TX_STATE_ABORT_MSK))
  265. continue;
  266. IWL_DEBUG_TX_REPLY(priv, "FrameCnt = %d, txq_id=%d idx=%d\n",
  267. agg->frame_count, txq_id, idx);
  268. IWL_DEBUG_TX_REPLY(priv, "status %s (0x%08x), "
  269. "try-count (0x%08x)\n",
  270. iwl_get_agg_tx_fail_reason(status),
  271. status & AGG_TX_STATUS_MSK,
  272. status & AGG_TX_TRY_MSK);
  273. hdr = iwl_tx_queue_get_hdr(priv, txq_id, idx);
  274. if (!hdr) {
  275. IWL_ERR(priv,
  276. "BUG_ON idx doesn't point to valid skb"
  277. " idx=%d, txq_id=%d\n", idx, txq_id);
  278. return -1;
  279. }
  280. sc = le16_to_cpu(hdr->seq_ctrl);
  281. if (idx != (SEQ_TO_SN(sc) & 0xff)) {
  282. IWL_ERR(priv,
  283. "BUG_ON idx doesn't match seq control"
  284. " idx=%d, seq_idx=%d, seq=%d\n",
  285. idx, SEQ_TO_SN(sc),
  286. hdr->seq_ctrl);
  287. return -1;
  288. }
  289. IWL_DEBUG_TX_REPLY(priv, "AGG Frame i=%d idx %d seq=%d\n",
  290. i, idx, SEQ_TO_SN(sc));
  291. /*
  292. * sh -> how many frames ahead of the starting frame is
  293. * the current one?
  294. *
  295. * Note that all frames sent in the batch must be in a
  296. * 64-frame window, so this number should be in [0,63].
  297. * If outside of this window, then we've found a new
  298. * "first" frame in the batch and need to change start.
  299. */
  300. sh = idx - start;
  301. /*
  302. * If >= 64, out of window. start must be at the front
  303. * of the circular buffer, idx must be near the end of
  304. * the buffer, and idx is the new "first" frame. Shift
  305. * the indices around.
  306. */
  307. if (sh >= 64) {
  308. /* Shift bitmap by start - idx, wrapped */
  309. sh = 0x100 - idx + start;
  310. bitmap = bitmap << sh;
  311. /* Now idx is the new start so sh = 0 */
  312. sh = 0;
  313. start = idx;
  314. /*
  315. * If <= -64 then wraps the 256-pkt circular buffer
  316. * (e.g., start = 255 and idx = 0, sh should be 1)
  317. */
  318. } else if (sh <= -64) {
  319. sh = 0x100 - start + idx;
  320. /*
  321. * If < 0 but > -64, out of window. idx is before start
  322. * but not wrapped. Shift the indices around.
  323. */
  324. } else if (sh < 0) {
  325. /* Shift by how far start is ahead of idx */
  326. sh = start - idx;
  327. bitmap = bitmap << sh;
  328. /* Now idx is the new start so sh = 0 */
  329. start = idx;
  330. sh = 0;
  331. }
  332. /* Sequence number start + sh was sent in this batch */
  333. bitmap |= 1ULL << sh;
  334. IWL_DEBUG_TX_REPLY(priv, "start=%d bitmap=0x%llx\n",
  335. start, (unsigned long long)bitmap);
  336. }
  337. /*
  338. * Store the bitmap and possibly the new start, if we wrapped
  339. * the buffer above
  340. */
  341. agg->bitmap = bitmap;
  342. agg->start_idx = start;
  343. IWL_DEBUG_TX_REPLY(priv, "Frames %d start_idx=%d bitmap=0x%llx\n",
  344. agg->frame_count, agg->start_idx,
  345. (unsigned long long)agg->bitmap);
  346. if (bitmap)
  347. agg->wait_for_ba = 1;
  348. }
  349. return 0;
  350. }
  351. void iwl_check_abort_status(struct iwl_priv *priv,
  352. u8 frame_count, u32 status)
  353. {
  354. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  355. IWL_ERR(priv, "Tx flush command to flush out all frames\n");
  356. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  357. queue_work(priv->workqueue, &priv->tx_flush);
  358. }
  359. }
  360. static void iwlagn_rx_reply_tx(struct iwl_priv *priv,
  361. struct iwl_rx_mem_buffer *rxb)
  362. {
  363. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  364. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  365. int txq_id = SEQ_TO_QUEUE(sequence);
  366. int index = SEQ_TO_INDEX(sequence);
  367. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  368. struct ieee80211_tx_info *info;
  369. struct iwlagn_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  370. struct iwl_tx_info *txb;
  371. u32 status = le16_to_cpu(tx_resp->status.status);
  372. int tid;
  373. int sta_id;
  374. int freed;
  375. unsigned long flags;
  376. if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
  377. IWL_ERR(priv, "%s: Read index for DMA queue txq_id (%d) "
  378. "index %d is out of range [0-%d] %d %d\n", __func__,
  379. txq_id, index, txq->q.n_bd, txq->q.write_ptr,
  380. txq->q.read_ptr);
  381. return;
  382. }
  383. txq->time_stamp = jiffies;
  384. txb = &txq->txb[txq->q.read_ptr];
  385. info = IEEE80211_SKB_CB(txb->skb);
  386. memset(&info->status, 0, sizeof(info->status));
  387. tid = (tx_resp->ra_tid & IWLAGN_TX_RES_TID_MSK) >>
  388. IWLAGN_TX_RES_TID_POS;
  389. sta_id = (tx_resp->ra_tid & IWLAGN_TX_RES_RA_MSK) >>
  390. IWLAGN_TX_RES_RA_POS;
  391. spin_lock_irqsave(&priv->sta_lock, flags);
  392. if (txq->sched_retry) {
  393. const u32 scd_ssn = iwlagn_get_scd_ssn(tx_resp);
  394. struct iwl_ht_agg *agg;
  395. agg = &priv->stations[sta_id].tid[tid].agg;
  396. /*
  397. * If the BT kill count is non-zero, we'll get this
  398. * notification again.
  399. */
  400. if (tx_resp->bt_kill_count && tx_resp->frame_count == 1 &&
  401. priv->cfg->bt_params &&
  402. priv->cfg->bt_params->advanced_bt_coexist) {
  403. IWL_DEBUG_COEX(priv, "receive reply tx with bt_kill\n");
  404. }
  405. iwlagn_tx_status_reply_tx(priv, agg, tx_resp, txq_id, index);
  406. /* check if BAR is needed */
  407. if ((tx_resp->frame_count == 1) && !iwl_is_tx_success(status))
  408. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  409. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  410. index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  411. IWL_DEBUG_TX_REPLY(priv, "Retry scheduler reclaim "
  412. "scd_ssn=%d idx=%d txq=%d swq=%d\n",
  413. scd_ssn , index, txq_id, txq->swq_id);
  414. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  415. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  416. if (priv->mac80211_registered &&
  417. (iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  418. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  419. iwl_wake_queue(priv, txq);
  420. }
  421. } else {
  422. iwlagn_set_tx_status(priv, info, txb->ctx, tx_resp,
  423. txq_id, false);
  424. freed = iwlagn_tx_queue_reclaim(priv, txq_id, index);
  425. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  426. if (priv->mac80211_registered &&
  427. iwl_queue_space(&txq->q) > txq->q.low_mark &&
  428. status != TX_STATUS_FAIL_PASSIVE_NO_RX)
  429. iwl_wake_queue(priv, txq);
  430. }
  431. iwlagn_txq_check_empty(priv, sta_id, tid, txq_id);
  432. iwl_check_abort_status(priv, tx_resp->frame_count, status);
  433. spin_unlock_irqrestore(&priv->sta_lock, flags);
  434. }
  435. void iwlagn_rx_handler_setup(struct iwl_priv *priv)
  436. {
  437. /* init calibration handlers */
  438. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  439. iwlagn_rx_calib_result;
  440. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  441. /* set up notification wait support */
  442. spin_lock_init(&priv->_agn.notif_wait_lock);
  443. INIT_LIST_HEAD(&priv->_agn.notif_waits);
  444. init_waitqueue_head(&priv->_agn.notif_waitq);
  445. }
  446. void iwlagn_setup_deferred_work(struct iwl_priv *priv)
  447. {
  448. /*
  449. * nothing need to be done here anymore
  450. * still keep for future use if needed
  451. */
  452. }
  453. int iwlagn_hw_valid_rtc_data_addr(u32 addr)
  454. {
  455. return (addr >= IWLAGN_RTC_DATA_LOWER_BOUND) &&
  456. (addr < IWLAGN_RTC_DATA_UPPER_BOUND);
  457. }
  458. int iwlagn_send_tx_power(struct iwl_priv *priv)
  459. {
  460. struct iwlagn_tx_power_dbm_cmd tx_power_cmd;
  461. u8 tx_ant_cfg_cmd;
  462. if (WARN_ONCE(test_bit(STATUS_SCAN_HW, &priv->status),
  463. "TX Power requested while scanning!\n"))
  464. return -EAGAIN;
  465. /* half dBm need to multiply */
  466. tx_power_cmd.global_lmt = (s8)(2 * priv->tx_power_user_lmt);
  467. if (priv->tx_power_lmt_in_half_dbm &&
  468. priv->tx_power_lmt_in_half_dbm < tx_power_cmd.global_lmt) {
  469. /*
  470. * For the newer devices which using enhanced/extend tx power
  471. * table in EEPROM, the format is in half dBm. driver need to
  472. * convert to dBm format before report to mac80211.
  473. * By doing so, there is a possibility of 1/2 dBm resolution
  474. * lost. driver will perform "round-up" operation before
  475. * reporting, but it will cause 1/2 dBm tx power over the
  476. * regulatory limit. Perform the checking here, if the
  477. * "tx_power_user_lmt" is higher than EEPROM value (in
  478. * half-dBm format), lower the tx power based on EEPROM
  479. */
  480. tx_power_cmd.global_lmt = priv->tx_power_lmt_in_half_dbm;
  481. }
  482. tx_power_cmd.flags = IWLAGN_TX_POWER_NO_CLOSED;
  483. tx_power_cmd.srv_chan_lmt = IWLAGN_TX_POWER_AUTO;
  484. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  485. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD_V1;
  486. else
  487. tx_ant_cfg_cmd = REPLY_TX_POWER_DBM_CMD;
  488. return iwl_send_cmd_pdu(priv, tx_ant_cfg_cmd, sizeof(tx_power_cmd),
  489. &tx_power_cmd);
  490. }
  491. void iwlagn_temperature(struct iwl_priv *priv)
  492. {
  493. /* store temperature from correct statistics (in Celsius) */
  494. priv->temperature = le32_to_cpu(priv->statistics.common.temperature);
  495. iwl_tt_handler(priv);
  496. }
  497. u16 iwlagn_eeprom_calib_version(struct iwl_priv *priv)
  498. {
  499. struct iwl_eeprom_calib_hdr {
  500. u8 version;
  501. u8 pa_type;
  502. u16 voltage;
  503. } *hdr;
  504. hdr = (struct iwl_eeprom_calib_hdr *)iwl_eeprom_query_addr(priv,
  505. EEPROM_CALIB_ALL);
  506. return hdr->version;
  507. }
  508. /*
  509. * EEPROM
  510. */
  511. static u32 eeprom_indirect_address(const struct iwl_priv *priv, u32 address)
  512. {
  513. u16 offset = 0;
  514. if ((address & INDIRECT_ADDRESS) == 0)
  515. return address;
  516. switch (address & INDIRECT_TYPE_MSK) {
  517. case INDIRECT_HOST:
  518. offset = iwl_eeprom_query16(priv, EEPROM_LINK_HOST);
  519. break;
  520. case INDIRECT_GENERAL:
  521. offset = iwl_eeprom_query16(priv, EEPROM_LINK_GENERAL);
  522. break;
  523. case INDIRECT_REGULATORY:
  524. offset = iwl_eeprom_query16(priv, EEPROM_LINK_REGULATORY);
  525. break;
  526. case INDIRECT_TXP_LIMIT:
  527. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT);
  528. break;
  529. case INDIRECT_TXP_LIMIT_SIZE:
  530. offset = iwl_eeprom_query16(priv, EEPROM_LINK_TXP_LIMIT_SIZE);
  531. break;
  532. case INDIRECT_CALIBRATION:
  533. offset = iwl_eeprom_query16(priv, EEPROM_LINK_CALIBRATION);
  534. break;
  535. case INDIRECT_PROCESS_ADJST:
  536. offset = iwl_eeprom_query16(priv, EEPROM_LINK_PROCESS_ADJST);
  537. break;
  538. case INDIRECT_OTHERS:
  539. offset = iwl_eeprom_query16(priv, EEPROM_LINK_OTHERS);
  540. break;
  541. default:
  542. IWL_ERR(priv, "illegal indirect type: 0x%X\n",
  543. address & INDIRECT_TYPE_MSK);
  544. break;
  545. }
  546. /* translate the offset from words to byte */
  547. return (address & ADDRESS_MSK) + (offset << 1);
  548. }
  549. const u8 *iwlagn_eeprom_query_addr(const struct iwl_priv *priv,
  550. size_t offset)
  551. {
  552. u32 address = eeprom_indirect_address(priv, offset);
  553. BUG_ON(address >= priv->cfg->base_params->eeprom_size);
  554. return &priv->eeprom[address];
  555. }
  556. struct iwl_mod_params iwlagn_mod_params = {
  557. .amsdu_size_8K = 1,
  558. .restart_fw = 1,
  559. .plcp_check = true,
  560. .bt_coex_active = true,
  561. .no_sleep_autoadjust = true,
  562. .power_level = IWL_POWER_INDEX_1,
  563. /* the rest are 0 by default */
  564. };
  565. void iwlagn_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  566. {
  567. unsigned long flags;
  568. int i;
  569. spin_lock_irqsave(&rxq->lock, flags);
  570. INIT_LIST_HEAD(&rxq->rx_free);
  571. INIT_LIST_HEAD(&rxq->rx_used);
  572. /* Fill the rx_used queue with _all_ of the Rx buffers */
  573. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  574. /* In the reset function, these buffers may have been allocated
  575. * to an SKB, so we need to unmap and free potential storage */
  576. if (rxq->pool[i].page != NULL) {
  577. dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
  578. PAGE_SIZE << priv->hw_params.rx_page_order,
  579. DMA_FROM_DEVICE);
  580. __iwl_free_pages(priv, rxq->pool[i].page);
  581. rxq->pool[i].page = NULL;
  582. }
  583. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  584. }
  585. for (i = 0; i < RX_QUEUE_SIZE; i++)
  586. rxq->queue[i] = NULL;
  587. /* Set us so that we have processed and used all buffers, but have
  588. * not restocked the Rx queue with fresh buffers */
  589. rxq->read = rxq->write = 0;
  590. rxq->write_actual = 0;
  591. rxq->free_count = 0;
  592. spin_unlock_irqrestore(&rxq->lock, flags);
  593. }
  594. int iwlagn_rx_init(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  595. {
  596. u32 rb_size;
  597. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  598. u32 rb_timeout = 0; /* FIXME: RX_RB_TIMEOUT for all devices? */
  599. rb_timeout = RX_RB_TIMEOUT;
  600. if (iwlagn_mod_params.amsdu_size_8K)
  601. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  602. else
  603. rb_size = FH_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  604. /* Stop Rx DMA */
  605. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  606. /* Reset driver's Rx queue write index */
  607. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  608. /* Tell device where to find RBD circular buffer in DRAM */
  609. iwl_write_direct32(priv, FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  610. (u32)(rxq->bd_dma >> 8));
  611. /* Tell device where in DRAM to update its Rx status */
  612. iwl_write_direct32(priv, FH_RSCSR_CHNL0_STTS_WPTR_REG,
  613. rxq->rb_stts_dma >> 4);
  614. /* Enable Rx DMA
  615. * FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY is set because of HW bug in
  616. * the credit mechanism in 5000 HW RX FIFO
  617. * Direct rx interrupts to hosts
  618. * Rx buffer size 4 or 8k
  619. * RB timeout 0x10
  620. * 256 RBDs
  621. */
  622. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG,
  623. FH_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  624. FH_RCSR_CHNL0_RX_IGNORE_RXF_EMPTY |
  625. FH_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  626. FH_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  627. rb_size|
  628. (rb_timeout << FH_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS)|
  629. (rfdnlog << FH_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  630. /* Set interrupt coalescing timer to default (2048 usecs) */
  631. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_TIMEOUT_DEF);
  632. return 0;
  633. }
  634. static void iwlagn_set_pwr_vmain(struct iwl_priv *priv)
  635. {
  636. /*
  637. * (for documentation purposes)
  638. * to set power to V_AUX, do:
  639. if (pci_pme_capable(priv->pci_dev, PCI_D3cold))
  640. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  641. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  642. ~APMG_PS_CTRL_MSK_PWR_SRC);
  643. */
  644. iwl_set_bits_mask_prph(priv, APMG_PS_CTRL_REG,
  645. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  646. ~APMG_PS_CTRL_MSK_PWR_SRC);
  647. }
  648. int iwlagn_hw_nic_init(struct iwl_priv *priv)
  649. {
  650. unsigned long flags;
  651. struct iwl_rx_queue *rxq = &priv->rxq;
  652. int ret;
  653. /* nic_init */
  654. spin_lock_irqsave(&priv->lock, flags);
  655. priv->cfg->ops->lib->apm_ops.init(priv);
  656. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  657. iwl_write8(priv, CSR_INT_COALESCING, IWL_HOST_INT_CALIB_TIMEOUT_DEF);
  658. spin_unlock_irqrestore(&priv->lock, flags);
  659. iwlagn_set_pwr_vmain(priv);
  660. priv->cfg->ops->lib->apm_ops.config(priv);
  661. /* Allocate the RX queue, or reset if it is already allocated */
  662. if (!rxq->bd) {
  663. ret = iwl_rx_queue_alloc(priv);
  664. if (ret) {
  665. IWL_ERR(priv, "Unable to initialize Rx queue\n");
  666. return -ENOMEM;
  667. }
  668. } else
  669. iwlagn_rx_queue_reset(priv, rxq);
  670. iwlagn_rx_replenish(priv);
  671. iwlagn_rx_init(priv, rxq);
  672. spin_lock_irqsave(&priv->lock, flags);
  673. rxq->need_update = 1;
  674. iwl_rx_queue_update_write_ptr(priv, rxq);
  675. spin_unlock_irqrestore(&priv->lock, flags);
  676. /* Allocate or reset and init all Tx and Command queues */
  677. if (!priv->txq) {
  678. ret = iwlagn_txq_ctx_alloc(priv);
  679. if (ret)
  680. return ret;
  681. } else
  682. iwlagn_txq_ctx_reset(priv);
  683. if (priv->cfg->base_params->shadow_reg_enable) {
  684. /* enable shadow regs in HW */
  685. iwl_set_bit(priv, CSR_MAC_SHADOW_REG_CTRL,
  686. 0x800FFFFF);
  687. }
  688. set_bit(STATUS_INIT, &priv->status);
  689. return 0;
  690. }
  691. /**
  692. * iwlagn_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  693. */
  694. static inline __le32 iwlagn_dma_addr2rbd_ptr(struct iwl_priv *priv,
  695. dma_addr_t dma_addr)
  696. {
  697. return cpu_to_le32((u32)(dma_addr >> 8));
  698. }
  699. /**
  700. * iwlagn_rx_queue_restock - refill RX queue from pre-allocated pool
  701. *
  702. * If there are slots in the RX queue that need to be restocked,
  703. * and we have free pre-allocated buffers, fill the ranks as much
  704. * as we can, pulling from rx_free.
  705. *
  706. * This moves the 'write' index forward to catch up with 'processed', and
  707. * also updates the memory address in the firmware to reference the new
  708. * target buffer.
  709. */
  710. void iwlagn_rx_queue_restock(struct iwl_priv *priv)
  711. {
  712. struct iwl_rx_queue *rxq = &priv->rxq;
  713. struct list_head *element;
  714. struct iwl_rx_mem_buffer *rxb;
  715. unsigned long flags;
  716. spin_lock_irqsave(&rxq->lock, flags);
  717. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  718. /* The overwritten rxb must be a used one */
  719. rxb = rxq->queue[rxq->write];
  720. BUG_ON(rxb && rxb->page);
  721. /* Get next free Rx buffer, remove from free list */
  722. element = rxq->rx_free.next;
  723. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  724. list_del(element);
  725. /* Point to Rx buffer via next RBD in circular buffer */
  726. rxq->bd[rxq->write] = iwlagn_dma_addr2rbd_ptr(priv,
  727. rxb->page_dma);
  728. rxq->queue[rxq->write] = rxb;
  729. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  730. rxq->free_count--;
  731. }
  732. spin_unlock_irqrestore(&rxq->lock, flags);
  733. /* If the pre-allocated buffer pool is dropping low, schedule to
  734. * refill it */
  735. if (rxq->free_count <= RX_LOW_WATERMARK)
  736. queue_work(priv->workqueue, &priv->rx_replenish);
  737. /* If we've added more space for the firmware to place data, tell it.
  738. * Increment device's write pointer in multiples of 8. */
  739. if (rxq->write_actual != (rxq->write & ~0x7)) {
  740. spin_lock_irqsave(&rxq->lock, flags);
  741. rxq->need_update = 1;
  742. spin_unlock_irqrestore(&rxq->lock, flags);
  743. iwl_rx_queue_update_write_ptr(priv, rxq);
  744. }
  745. }
  746. /**
  747. * iwlagn_rx_replenish - Move all used packet from rx_used to rx_free
  748. *
  749. * When moving to rx_free an SKB is allocated for the slot.
  750. *
  751. * Also restock the Rx queue via iwl_rx_queue_restock.
  752. * This is called as a scheduled work item (except for during initialization)
  753. */
  754. void iwlagn_rx_allocate(struct iwl_priv *priv, gfp_t priority)
  755. {
  756. struct iwl_rx_queue *rxq = &priv->rxq;
  757. struct list_head *element;
  758. struct iwl_rx_mem_buffer *rxb;
  759. struct page *page;
  760. unsigned long flags;
  761. gfp_t gfp_mask = priority;
  762. while (1) {
  763. spin_lock_irqsave(&rxq->lock, flags);
  764. if (list_empty(&rxq->rx_used)) {
  765. spin_unlock_irqrestore(&rxq->lock, flags);
  766. return;
  767. }
  768. spin_unlock_irqrestore(&rxq->lock, flags);
  769. if (rxq->free_count > RX_LOW_WATERMARK)
  770. gfp_mask |= __GFP_NOWARN;
  771. if (priv->hw_params.rx_page_order > 0)
  772. gfp_mask |= __GFP_COMP;
  773. /* Alloc a new receive buffer */
  774. page = alloc_pages(gfp_mask, priv->hw_params.rx_page_order);
  775. if (!page) {
  776. if (net_ratelimit())
  777. IWL_DEBUG_INFO(priv, "alloc_pages failed, "
  778. "order: %d\n",
  779. priv->hw_params.rx_page_order);
  780. if ((rxq->free_count <= RX_LOW_WATERMARK) &&
  781. net_ratelimit())
  782. IWL_CRIT(priv, "Failed to alloc_pages with %s. Only %u free buffers remaining.\n",
  783. priority == GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  784. rxq->free_count);
  785. /* We don't reschedule replenish work here -- we will
  786. * call the restock method and if it still needs
  787. * more buffers it will schedule replenish */
  788. return;
  789. }
  790. spin_lock_irqsave(&rxq->lock, flags);
  791. if (list_empty(&rxq->rx_used)) {
  792. spin_unlock_irqrestore(&rxq->lock, flags);
  793. __free_pages(page, priv->hw_params.rx_page_order);
  794. return;
  795. }
  796. element = rxq->rx_used.next;
  797. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  798. list_del(element);
  799. spin_unlock_irqrestore(&rxq->lock, flags);
  800. BUG_ON(rxb->page);
  801. rxb->page = page;
  802. /* Get physical address of the RB */
  803. rxb->page_dma = dma_map_page(priv->bus.dev, page, 0,
  804. PAGE_SIZE << priv->hw_params.rx_page_order,
  805. DMA_FROM_DEVICE);
  806. /* dma address must be no more than 36 bits */
  807. BUG_ON(rxb->page_dma & ~DMA_BIT_MASK(36));
  808. /* and also 256 byte aligned! */
  809. BUG_ON(rxb->page_dma & DMA_BIT_MASK(8));
  810. spin_lock_irqsave(&rxq->lock, flags);
  811. list_add_tail(&rxb->list, &rxq->rx_free);
  812. rxq->free_count++;
  813. spin_unlock_irqrestore(&rxq->lock, flags);
  814. }
  815. }
  816. void iwlagn_rx_replenish(struct iwl_priv *priv)
  817. {
  818. unsigned long flags;
  819. iwlagn_rx_allocate(priv, GFP_KERNEL);
  820. spin_lock_irqsave(&priv->lock, flags);
  821. iwlagn_rx_queue_restock(priv);
  822. spin_unlock_irqrestore(&priv->lock, flags);
  823. }
  824. void iwlagn_rx_replenish_now(struct iwl_priv *priv)
  825. {
  826. iwlagn_rx_allocate(priv, GFP_ATOMIC);
  827. iwlagn_rx_queue_restock(priv);
  828. }
  829. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  830. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  831. * This free routine walks the list of POOL entries and if SKB is set to
  832. * non NULL it is unmapped and freed
  833. */
  834. void iwlagn_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  835. {
  836. int i;
  837. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  838. if (rxq->pool[i].page != NULL) {
  839. dma_unmap_page(priv->bus.dev, rxq->pool[i].page_dma,
  840. PAGE_SIZE << priv->hw_params.rx_page_order,
  841. DMA_FROM_DEVICE);
  842. __iwl_free_pages(priv, rxq->pool[i].page);
  843. rxq->pool[i].page = NULL;
  844. }
  845. }
  846. dma_free_coherent(priv->bus.dev, 4 * RX_QUEUE_SIZE,
  847. rxq->bd, rxq->bd_dma);
  848. dma_free_coherent(priv->bus.dev,
  849. sizeof(struct iwl_rb_status),
  850. rxq->rb_stts, rxq->rb_stts_dma);
  851. rxq->bd = NULL;
  852. rxq->rb_stts = NULL;
  853. }
  854. int iwlagn_rxq_stop(struct iwl_priv *priv)
  855. {
  856. /* stop Rx DMA */
  857. iwl_write_direct32(priv, FH_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  858. iwl_poll_direct_bit(priv, FH_MEM_RSSR_RX_STATUS_REG,
  859. FH_RSSR_CHNL0_RX_STATUS_CHNL_IDLE, 1000);
  860. return 0;
  861. }
  862. int iwlagn_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  863. {
  864. int idx = 0;
  865. int band_offset = 0;
  866. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  867. if (rate_n_flags & RATE_MCS_HT_MSK) {
  868. idx = (rate_n_flags & 0xff);
  869. return idx;
  870. /* Legacy rate format, search for match in table */
  871. } else {
  872. if (band == IEEE80211_BAND_5GHZ)
  873. band_offset = IWL_FIRST_OFDM_RATE;
  874. for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
  875. if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
  876. return idx - band_offset;
  877. }
  878. return -1;
  879. }
  880. static int iwl_get_single_channel_for_scan(struct iwl_priv *priv,
  881. struct ieee80211_vif *vif,
  882. enum ieee80211_band band,
  883. struct iwl_scan_channel *scan_ch)
  884. {
  885. const struct ieee80211_supported_band *sband;
  886. u16 passive_dwell = 0;
  887. u16 active_dwell = 0;
  888. int added = 0;
  889. u16 channel = 0;
  890. sband = iwl_get_hw_mode(priv, band);
  891. if (!sband) {
  892. IWL_ERR(priv, "invalid band\n");
  893. return added;
  894. }
  895. active_dwell = iwl_get_active_dwell_time(priv, band, 0);
  896. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  897. if (passive_dwell <= active_dwell)
  898. passive_dwell = active_dwell + 1;
  899. channel = iwl_get_single_channel_number(priv, band);
  900. if (channel) {
  901. scan_ch->channel = cpu_to_le16(channel);
  902. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  903. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  904. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  905. /* Set txpower levels to defaults */
  906. scan_ch->dsp_atten = 110;
  907. if (band == IEEE80211_BAND_5GHZ)
  908. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  909. else
  910. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  911. added++;
  912. } else
  913. IWL_ERR(priv, "no valid channel found\n");
  914. return added;
  915. }
  916. static int iwl_get_channels_for_scan(struct iwl_priv *priv,
  917. struct ieee80211_vif *vif,
  918. enum ieee80211_band band,
  919. u8 is_active, u8 n_probes,
  920. struct iwl_scan_channel *scan_ch)
  921. {
  922. struct ieee80211_channel *chan;
  923. const struct ieee80211_supported_band *sband;
  924. const struct iwl_channel_info *ch_info;
  925. u16 passive_dwell = 0;
  926. u16 active_dwell = 0;
  927. int added, i;
  928. u16 channel;
  929. sband = iwl_get_hw_mode(priv, band);
  930. if (!sband)
  931. return 0;
  932. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  933. passive_dwell = iwl_get_passive_dwell_time(priv, band, vif);
  934. if (passive_dwell <= active_dwell)
  935. passive_dwell = active_dwell + 1;
  936. for (i = 0, added = 0; i < priv->scan_request->n_channels; i++) {
  937. chan = priv->scan_request->channels[i];
  938. if (chan->band != band)
  939. continue;
  940. channel = chan->hw_value;
  941. scan_ch->channel = cpu_to_le16(channel);
  942. ch_info = iwl_get_channel_info(priv, band, channel);
  943. if (!is_channel_valid(ch_info)) {
  944. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  945. channel);
  946. continue;
  947. }
  948. if (!is_active || is_channel_passive(ch_info) ||
  949. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  950. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  951. else
  952. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  953. if (n_probes)
  954. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  955. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  956. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  957. /* Set txpower levels to defaults */
  958. scan_ch->dsp_atten = 110;
  959. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  960. * power level:
  961. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  962. */
  963. if (band == IEEE80211_BAND_5GHZ)
  964. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  965. else
  966. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  967. IWL_DEBUG_SCAN(priv, "Scanning ch=%d prob=0x%X [%s %d]\n",
  968. channel, le32_to_cpu(scan_ch->type),
  969. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  970. "ACTIVE" : "PASSIVE",
  971. (scan_ch->type & SCAN_CHANNEL_TYPE_ACTIVE) ?
  972. active_dwell : passive_dwell);
  973. scan_ch++;
  974. added++;
  975. }
  976. IWL_DEBUG_SCAN(priv, "total channels to scan %d\n", added);
  977. return added;
  978. }
  979. static int iwl_fill_offch_tx(struct iwl_priv *priv, void *data, size_t maxlen)
  980. {
  981. struct sk_buff *skb = priv->_agn.offchan_tx_skb;
  982. if (skb->len < maxlen)
  983. maxlen = skb->len;
  984. memcpy(data, skb->data, maxlen);
  985. return maxlen;
  986. }
  987. int iwlagn_request_scan(struct iwl_priv *priv, struct ieee80211_vif *vif)
  988. {
  989. struct iwl_host_cmd cmd = {
  990. .id = REPLY_SCAN_CMD,
  991. .len = { sizeof(struct iwl_scan_cmd), },
  992. };
  993. struct iwl_scan_cmd *scan;
  994. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  995. u32 rate_flags = 0;
  996. u16 cmd_len;
  997. u16 rx_chain = 0;
  998. enum ieee80211_band band;
  999. u8 n_probes = 0;
  1000. u8 rx_ant = priv->hw_params.valid_rx_ant;
  1001. u8 rate;
  1002. bool is_active = false;
  1003. int chan_mod;
  1004. u8 active_chains;
  1005. u8 scan_tx_antennas = priv->hw_params.valid_tx_ant;
  1006. int ret;
  1007. lockdep_assert_held(&priv->mutex);
  1008. if (vif)
  1009. ctx = iwl_rxon_ctx_from_vif(vif);
  1010. if (!priv->scan_cmd) {
  1011. priv->scan_cmd = kmalloc(sizeof(struct iwl_scan_cmd) +
  1012. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  1013. if (!priv->scan_cmd) {
  1014. IWL_DEBUG_SCAN(priv,
  1015. "fail to allocate memory for scan\n");
  1016. return -ENOMEM;
  1017. }
  1018. }
  1019. scan = priv->scan_cmd;
  1020. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  1021. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  1022. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  1023. if (priv->scan_type != IWL_SCAN_OFFCH_TX &&
  1024. iwl_is_any_associated(priv)) {
  1025. u16 interval = 0;
  1026. u32 extra;
  1027. u32 suspend_time = 100;
  1028. u32 scan_suspend_time = 100;
  1029. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  1030. switch (priv->scan_type) {
  1031. case IWL_SCAN_OFFCH_TX:
  1032. WARN_ON(1);
  1033. break;
  1034. case IWL_SCAN_RADIO_RESET:
  1035. interval = 0;
  1036. break;
  1037. case IWL_SCAN_NORMAL:
  1038. interval = vif->bss_conf.beacon_int;
  1039. break;
  1040. }
  1041. scan->suspend_time = 0;
  1042. scan->max_out_time = cpu_to_le32(200 * 1024);
  1043. if (!interval)
  1044. interval = suspend_time;
  1045. extra = (suspend_time / interval) << 22;
  1046. scan_suspend_time = (extra |
  1047. ((suspend_time % interval) * 1024));
  1048. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  1049. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  1050. scan_suspend_time, interval);
  1051. } else if (priv->scan_type == IWL_SCAN_OFFCH_TX) {
  1052. scan->suspend_time = 0;
  1053. scan->max_out_time =
  1054. cpu_to_le32(1024 * priv->_agn.offchan_tx_timeout);
  1055. }
  1056. switch (priv->scan_type) {
  1057. case IWL_SCAN_RADIO_RESET:
  1058. IWL_DEBUG_SCAN(priv, "Start internal passive scan.\n");
  1059. break;
  1060. case IWL_SCAN_NORMAL:
  1061. if (priv->scan_request->n_ssids) {
  1062. int i, p = 0;
  1063. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  1064. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  1065. /* always does wildcard anyway */
  1066. if (!priv->scan_request->ssids[i].ssid_len)
  1067. continue;
  1068. scan->direct_scan[p].id = WLAN_EID_SSID;
  1069. scan->direct_scan[p].len =
  1070. priv->scan_request->ssids[i].ssid_len;
  1071. memcpy(scan->direct_scan[p].ssid,
  1072. priv->scan_request->ssids[i].ssid,
  1073. priv->scan_request->ssids[i].ssid_len);
  1074. n_probes++;
  1075. p++;
  1076. }
  1077. is_active = true;
  1078. } else
  1079. IWL_DEBUG_SCAN(priv, "Start passive scan.\n");
  1080. break;
  1081. case IWL_SCAN_OFFCH_TX:
  1082. IWL_DEBUG_SCAN(priv, "Start offchannel TX scan.\n");
  1083. break;
  1084. }
  1085. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  1086. scan->tx_cmd.sta_id = ctx->bcast_sta_id;
  1087. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1088. switch (priv->scan_band) {
  1089. case IEEE80211_BAND_2GHZ:
  1090. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  1091. chan_mod = le32_to_cpu(
  1092. priv->contexts[IWL_RXON_CTX_BSS].active.flags &
  1093. RXON_FLG_CHANNEL_MODE_MSK)
  1094. >> RXON_FLG_CHANNEL_MODE_POS;
  1095. if (chan_mod == CHANNEL_MODE_PURE_40) {
  1096. rate = IWL_RATE_6M_PLCP;
  1097. } else {
  1098. rate = IWL_RATE_1M_PLCP;
  1099. rate_flags = RATE_MCS_CCK_MSK;
  1100. }
  1101. /*
  1102. * Internal scans are passive, so we can indiscriminately set
  1103. * the BT ignore flag on 2.4 GHz since it applies to TX only.
  1104. */
  1105. if (priv->cfg->bt_params &&
  1106. priv->cfg->bt_params->advanced_bt_coexist)
  1107. scan->tx_cmd.tx_flags |= TX_CMD_FLG_IGNORE_BT;
  1108. break;
  1109. case IEEE80211_BAND_5GHZ:
  1110. rate = IWL_RATE_6M_PLCP;
  1111. break;
  1112. default:
  1113. IWL_WARN(priv, "Invalid scan band\n");
  1114. return -EIO;
  1115. }
  1116. /*
  1117. * If active scanning is requested but a certain channel is
  1118. * marked passive, we can do active scanning if we detect
  1119. * transmissions.
  1120. *
  1121. * There is an issue with some firmware versions that triggers
  1122. * a sysassert on a "good CRC threshold" of zero (== disabled),
  1123. * on a radar channel even though this means that we should NOT
  1124. * send probes.
  1125. *
  1126. * The "good CRC threshold" is the number of frames that we
  1127. * need to receive during our dwell time on a channel before
  1128. * sending out probes -- setting this to a huge value will
  1129. * mean we never reach it, but at the same time work around
  1130. * the aforementioned issue. Thus use IWL_GOOD_CRC_TH_NEVER
  1131. * here instead of IWL_GOOD_CRC_TH_DISABLED.
  1132. *
  1133. * This was fixed in later versions along with some other
  1134. * scan changes, and the threshold behaves as a flag in those
  1135. * versions.
  1136. */
  1137. if (priv->new_scan_threshold_behaviour)
  1138. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1139. IWL_GOOD_CRC_TH_DISABLED;
  1140. else
  1141. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH_DEFAULT :
  1142. IWL_GOOD_CRC_TH_NEVER;
  1143. band = priv->scan_band;
  1144. if (priv->cfg->scan_rx_antennas[band])
  1145. rx_ant = priv->cfg->scan_rx_antennas[band];
  1146. if (band == IEEE80211_BAND_2GHZ &&
  1147. priv->cfg->bt_params &&
  1148. priv->cfg->bt_params->advanced_bt_coexist) {
  1149. /* transmit 2.4 GHz probes only on first antenna */
  1150. scan_tx_antennas = first_antenna(scan_tx_antennas);
  1151. }
  1152. priv->scan_tx_ant[band] = iwl_toggle_tx_ant(priv, priv->scan_tx_ant[band],
  1153. scan_tx_antennas);
  1154. rate_flags |= iwl_ant_idx_to_flags(priv->scan_tx_ant[band]);
  1155. scan->tx_cmd.rate_n_flags = iwl_hw_set_rate_n_flags(rate, rate_flags);
  1156. /* In power save mode use one chain, otherwise use all chains */
  1157. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  1158. /* rx_ant has been set to all valid chains previously */
  1159. active_chains = rx_ant &
  1160. ((u8)(priv->chain_noise_data.active_chains));
  1161. if (!active_chains)
  1162. active_chains = rx_ant;
  1163. IWL_DEBUG_SCAN(priv, "chain_noise_data.active_chains: %u\n",
  1164. priv->chain_noise_data.active_chains);
  1165. rx_ant = first_antenna(active_chains);
  1166. }
  1167. if (priv->cfg->bt_params &&
  1168. priv->cfg->bt_params->advanced_bt_coexist &&
  1169. priv->bt_full_concurrent) {
  1170. /* operated as 1x1 in full concurrency mode */
  1171. rx_ant = first_antenna(rx_ant);
  1172. }
  1173. /* MIMO is not used here, but value is required */
  1174. rx_chain |= priv->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  1175. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  1176. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  1177. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  1178. scan->rx_chain = cpu_to_le16(rx_chain);
  1179. switch (priv->scan_type) {
  1180. case IWL_SCAN_NORMAL:
  1181. cmd_len = iwl_fill_probe_req(priv,
  1182. (struct ieee80211_mgmt *)scan->data,
  1183. vif->addr,
  1184. priv->scan_request->ie,
  1185. priv->scan_request->ie_len,
  1186. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1187. break;
  1188. case IWL_SCAN_RADIO_RESET:
  1189. /* use bcast addr, will not be transmitted but must be valid */
  1190. cmd_len = iwl_fill_probe_req(priv,
  1191. (struct ieee80211_mgmt *)scan->data,
  1192. iwl_bcast_addr, NULL, 0,
  1193. IWL_MAX_SCAN_SIZE - sizeof(*scan));
  1194. break;
  1195. case IWL_SCAN_OFFCH_TX:
  1196. cmd_len = iwl_fill_offch_tx(priv, scan->data,
  1197. IWL_MAX_SCAN_SIZE
  1198. - sizeof(*scan)
  1199. - sizeof(struct iwl_scan_channel));
  1200. scan->scan_flags |= IWL_SCAN_FLAGS_ACTION_FRAME_TX;
  1201. break;
  1202. default:
  1203. BUG();
  1204. }
  1205. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  1206. scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
  1207. RXON_FILTER_BCON_AWARE_MSK);
  1208. switch (priv->scan_type) {
  1209. case IWL_SCAN_RADIO_RESET:
  1210. scan->channel_count =
  1211. iwl_get_single_channel_for_scan(priv, vif, band,
  1212. (void *)&scan->data[cmd_len]);
  1213. break;
  1214. case IWL_SCAN_NORMAL:
  1215. scan->channel_count =
  1216. iwl_get_channels_for_scan(priv, vif, band,
  1217. is_active, n_probes,
  1218. (void *)&scan->data[cmd_len]);
  1219. break;
  1220. case IWL_SCAN_OFFCH_TX: {
  1221. struct iwl_scan_channel *scan_ch;
  1222. scan->channel_count = 1;
  1223. scan_ch = (void *)&scan->data[cmd_len];
  1224. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  1225. scan_ch->channel =
  1226. cpu_to_le16(priv->_agn.offchan_tx_chan->hw_value);
  1227. scan_ch->active_dwell =
  1228. cpu_to_le16(priv->_agn.offchan_tx_timeout);
  1229. scan_ch->passive_dwell = 0;
  1230. /* Set txpower levels to defaults */
  1231. scan_ch->dsp_atten = 110;
  1232. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1233. * power level:
  1234. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1235. */
  1236. if (priv->_agn.offchan_tx_chan->band == IEEE80211_BAND_5GHZ)
  1237. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1238. else
  1239. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  1240. }
  1241. break;
  1242. }
  1243. if (scan->channel_count == 0) {
  1244. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  1245. return -EIO;
  1246. }
  1247. cmd.len[0] += le16_to_cpu(scan->tx_cmd.len) +
  1248. scan->channel_count * sizeof(struct iwl_scan_channel);
  1249. cmd.data[0] = scan;
  1250. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  1251. scan->len = cpu_to_le16(cmd.len[0]);
  1252. /* set scan bit here for PAN params */
  1253. set_bit(STATUS_SCAN_HW, &priv->status);
  1254. if (priv->cfg->ops->hcmd->set_pan_params) {
  1255. ret = priv->cfg->ops->hcmd->set_pan_params(priv);
  1256. if (ret)
  1257. return ret;
  1258. }
  1259. ret = iwl_send_cmd_sync(priv, &cmd);
  1260. if (ret) {
  1261. clear_bit(STATUS_SCAN_HW, &priv->status);
  1262. if (priv->cfg->ops->hcmd->set_pan_params)
  1263. priv->cfg->ops->hcmd->set_pan_params(priv);
  1264. }
  1265. return ret;
  1266. }
  1267. int iwlagn_manage_ibss_station(struct iwl_priv *priv,
  1268. struct ieee80211_vif *vif, bool add)
  1269. {
  1270. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1271. if (add)
  1272. return iwlagn_add_bssid_station(priv, vif_priv->ctx,
  1273. vif->bss_conf.bssid,
  1274. &vif_priv->ibss_bssid_sta_id);
  1275. return iwl_remove_station(priv, vif_priv->ibss_bssid_sta_id,
  1276. vif->bss_conf.bssid);
  1277. }
  1278. void iwl_free_tfds_in_queue(struct iwl_priv *priv,
  1279. int sta_id, int tid, int freed)
  1280. {
  1281. lockdep_assert_held(&priv->sta_lock);
  1282. if (priv->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  1283. priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  1284. else {
  1285. IWL_DEBUG_TX(priv, "free more than tfds_in_queue (%u:%d)\n",
  1286. priv->stations[sta_id].tid[tid].tfds_in_queue,
  1287. freed);
  1288. priv->stations[sta_id].tid[tid].tfds_in_queue = 0;
  1289. }
  1290. }
  1291. #define IWL_FLUSH_WAIT_MS 2000
  1292. int iwlagn_wait_tx_queue_empty(struct iwl_priv *priv)
  1293. {
  1294. struct iwl_tx_queue *txq;
  1295. struct iwl_queue *q;
  1296. int cnt;
  1297. unsigned long now = jiffies;
  1298. int ret = 0;
  1299. /* waiting for all the tx frames complete might take a while */
  1300. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  1301. if (cnt == priv->cmd_queue)
  1302. continue;
  1303. txq = &priv->txq[cnt];
  1304. q = &txq->q;
  1305. while (q->read_ptr != q->write_ptr && !time_after(jiffies,
  1306. now + msecs_to_jiffies(IWL_FLUSH_WAIT_MS)))
  1307. msleep(1);
  1308. if (q->read_ptr != q->write_ptr) {
  1309. IWL_ERR(priv, "fail to flush all tx fifo queues\n");
  1310. ret = -ETIMEDOUT;
  1311. break;
  1312. }
  1313. }
  1314. return ret;
  1315. }
  1316. #define IWL_TX_QUEUE_MSK 0xfffff
  1317. /**
  1318. * iwlagn_txfifo_flush: send REPLY_TXFIFO_FLUSH command to uCode
  1319. *
  1320. * pre-requirements:
  1321. * 1. acquire mutex before calling
  1322. * 2. make sure rf is on and not in exit state
  1323. */
  1324. int iwlagn_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1325. {
  1326. struct iwl_txfifo_flush_cmd flush_cmd;
  1327. struct iwl_host_cmd cmd = {
  1328. .id = REPLY_TXFIFO_FLUSH,
  1329. .len = { sizeof(struct iwl_txfifo_flush_cmd), },
  1330. .flags = CMD_SYNC,
  1331. .data = { &flush_cmd, },
  1332. };
  1333. might_sleep();
  1334. memset(&flush_cmd, 0, sizeof(flush_cmd));
  1335. if (flush_control & BIT(IWL_RXON_CTX_BSS))
  1336. flush_cmd.fifo_control = IWL_SCD_VO_MSK | IWL_SCD_VI_MSK |
  1337. IWL_SCD_BE_MSK | IWL_SCD_BK_MSK |
  1338. IWL_SCD_MGMT_MSK;
  1339. if ((flush_control & BIT(IWL_RXON_CTX_PAN)) &&
  1340. (priv->valid_contexts != BIT(IWL_RXON_CTX_BSS)))
  1341. flush_cmd.fifo_control |= IWL_PAN_SCD_VO_MSK |
  1342. IWL_PAN_SCD_VI_MSK | IWL_PAN_SCD_BE_MSK |
  1343. IWL_PAN_SCD_BK_MSK | IWL_PAN_SCD_MGMT_MSK |
  1344. IWL_PAN_SCD_MULTICAST_MSK;
  1345. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  1346. flush_cmd.fifo_control |= IWL_AGG_TX_QUEUE_MSK;
  1347. IWL_DEBUG_INFO(priv, "fifo queue control: 0X%x\n",
  1348. flush_cmd.fifo_control);
  1349. flush_cmd.flush_control = cpu_to_le16(flush_control);
  1350. return iwl_send_cmd(priv, &cmd);
  1351. }
  1352. void iwlagn_dev_txfifo_flush(struct iwl_priv *priv, u16 flush_control)
  1353. {
  1354. mutex_lock(&priv->mutex);
  1355. ieee80211_stop_queues(priv->hw);
  1356. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  1357. IWL_ERR(priv, "flush request fail\n");
  1358. goto done;
  1359. }
  1360. IWL_DEBUG_INFO(priv, "wait transmit/flush all frames\n");
  1361. iwlagn_wait_tx_queue_empty(priv);
  1362. done:
  1363. ieee80211_wake_queues(priv->hw);
  1364. mutex_unlock(&priv->mutex);
  1365. }
  1366. /*
  1367. * BT coex
  1368. */
  1369. /*
  1370. * Macros to access the lookup table.
  1371. *
  1372. * The lookup table has 7 inputs: bt3_prio, bt3_txrx, bt_rf_act, wifi_req,
  1373. * wifi_prio, wifi_txrx and wifi_sh_ant_req.
  1374. *
  1375. * It has three outputs: WLAN_ACTIVE, WLAN_KILL and ANT_SWITCH
  1376. *
  1377. * The format is that "registers" 8 through 11 contain the WLAN_ACTIVE bits
  1378. * one after another in 32-bit registers, and "registers" 0 through 7 contain
  1379. * the WLAN_KILL and ANT_SWITCH bits interleaved (in that order).
  1380. *
  1381. * These macros encode that format.
  1382. */
  1383. #define LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, wifi_req, wifi_prio, \
  1384. wifi_txrx, wifi_sh_ant_req) \
  1385. (bt3_prio | (bt3_txrx << 1) | (bt_rf_act << 2) | (wifi_req << 3) | \
  1386. (wifi_prio << 4) | (wifi_txrx << 5) | (wifi_sh_ant_req << 6))
  1387. #define LUT_PTA_WLAN_ACTIVE_OP(lut, op, val) \
  1388. lut[8 + ((val) >> 5)] op (cpu_to_le32(BIT((val) & 0x1f)))
  1389. #define LUT_TEST_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1390. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1391. (!!(LUT_PTA_WLAN_ACTIVE_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, \
  1392. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1393. wifi_sh_ant_req))))
  1394. #define LUT_SET_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1395. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1396. LUT_PTA_WLAN_ACTIVE_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, \
  1397. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1398. wifi_sh_ant_req))
  1399. #define LUT_CLEAR_PTA_WLAN_ACTIVE(lut, bt3_prio, bt3_txrx, bt_rf_act, \
  1400. wifi_req, wifi_prio, wifi_txrx, \
  1401. wifi_sh_ant_req) \
  1402. LUT_PTA_WLAN_ACTIVE_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, \
  1403. bt_rf_act, wifi_req, wifi_prio, wifi_txrx, \
  1404. wifi_sh_ant_req))
  1405. #define LUT_WLAN_KILL_OP(lut, op, val) \
  1406. lut[(val) >> 4] op (cpu_to_le32(BIT(((val) << 1) & 0x1e)))
  1407. #define LUT_TEST_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1408. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1409. (!!(LUT_WLAN_KILL_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1410. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))))
  1411. #define LUT_SET_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1412. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1413. LUT_WLAN_KILL_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1414. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1415. #define LUT_CLEAR_WLAN_KILL(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1416. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1417. LUT_WLAN_KILL_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1418. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1419. #define LUT_ANT_SWITCH_OP(lut, op, val) \
  1420. lut[(val) >> 4] op (cpu_to_le32(BIT((((val) << 1) & 0x1e) + 1)))
  1421. #define LUT_TEST_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1422. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1423. (!!(LUT_ANT_SWITCH_OP(lut, &, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1424. wifi_req, wifi_prio, wifi_txrx, \
  1425. wifi_sh_ant_req))))
  1426. #define LUT_SET_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1427. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1428. LUT_ANT_SWITCH_OP(lut, |=, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1429. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1430. #define LUT_CLEAR_ANT_SWITCH(lut, bt3_prio, bt3_txrx, bt_rf_act, wifi_req, \
  1431. wifi_prio, wifi_txrx, wifi_sh_ant_req) \
  1432. LUT_ANT_SWITCH_OP(lut, &= ~, LUT_VALUE(bt3_prio, bt3_txrx, bt_rf_act, \
  1433. wifi_req, wifi_prio, wifi_txrx, wifi_sh_ant_req))
  1434. static const __le32 iwlagn_def_3w_lookup[12] = {
  1435. cpu_to_le32(0xaaaaaaaa),
  1436. cpu_to_le32(0xaaaaaaaa),
  1437. cpu_to_le32(0xaeaaaaaa),
  1438. cpu_to_le32(0xaaaaaaaa),
  1439. cpu_to_le32(0xcc00ff28),
  1440. cpu_to_le32(0x0000aaaa),
  1441. cpu_to_le32(0xcc00aaaa),
  1442. cpu_to_le32(0x0000aaaa),
  1443. cpu_to_le32(0xc0004000),
  1444. cpu_to_le32(0x00004000),
  1445. cpu_to_le32(0xf0005000),
  1446. cpu_to_le32(0xf0005000),
  1447. };
  1448. static const __le32 iwlagn_concurrent_lookup[12] = {
  1449. cpu_to_le32(0xaaaaaaaa),
  1450. cpu_to_le32(0xaaaaaaaa),
  1451. cpu_to_le32(0xaaaaaaaa),
  1452. cpu_to_le32(0xaaaaaaaa),
  1453. cpu_to_le32(0xaaaaaaaa),
  1454. cpu_to_le32(0xaaaaaaaa),
  1455. cpu_to_le32(0xaaaaaaaa),
  1456. cpu_to_le32(0xaaaaaaaa),
  1457. cpu_to_le32(0x00000000),
  1458. cpu_to_le32(0x00000000),
  1459. cpu_to_le32(0x00000000),
  1460. cpu_to_le32(0x00000000),
  1461. };
  1462. void iwlagn_send_advance_bt_config(struct iwl_priv *priv)
  1463. {
  1464. struct iwl_basic_bt_cmd basic = {
  1465. .max_kill = IWLAGN_BT_MAX_KILL_DEFAULT,
  1466. .bt3_timer_t7_value = IWLAGN_BT3_T7_DEFAULT,
  1467. .bt3_prio_sample_time = IWLAGN_BT3_PRIO_SAMPLE_DEFAULT,
  1468. .bt3_timer_t2_value = IWLAGN_BT3_T2_DEFAULT,
  1469. };
  1470. struct iwl6000_bt_cmd bt_cmd_6000;
  1471. struct iwl2000_bt_cmd bt_cmd_2000;
  1472. int ret;
  1473. BUILD_BUG_ON(sizeof(iwlagn_def_3w_lookup) !=
  1474. sizeof(basic.bt3_lookup_table));
  1475. if (priv->cfg->bt_params) {
  1476. if (priv->cfg->bt_params->bt_session_2) {
  1477. bt_cmd_2000.prio_boost = cpu_to_le32(
  1478. priv->cfg->bt_params->bt_prio_boost);
  1479. bt_cmd_2000.tx_prio_boost = 0;
  1480. bt_cmd_2000.rx_prio_boost = 0;
  1481. } else {
  1482. bt_cmd_6000.prio_boost =
  1483. priv->cfg->bt_params->bt_prio_boost;
  1484. bt_cmd_6000.tx_prio_boost = 0;
  1485. bt_cmd_6000.rx_prio_boost = 0;
  1486. }
  1487. } else {
  1488. IWL_ERR(priv, "failed to construct BT Coex Config\n");
  1489. return;
  1490. }
  1491. basic.kill_ack_mask = priv->kill_ack_mask;
  1492. basic.kill_cts_mask = priv->kill_cts_mask;
  1493. basic.valid = priv->bt_valid;
  1494. /*
  1495. * Configure BT coex mode to "no coexistence" when the
  1496. * user disabled BT coexistence, we have no interface
  1497. * (might be in monitor mode), or the interface is in
  1498. * IBSS mode (no proper uCode support for coex then).
  1499. */
  1500. if (!iwlagn_mod_params.bt_coex_active ||
  1501. priv->iw_mode == NL80211_IFTYPE_ADHOC) {
  1502. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_DISABLED;
  1503. } else {
  1504. basic.flags = IWLAGN_BT_FLAG_COEX_MODE_3W <<
  1505. IWLAGN_BT_FLAG_COEX_MODE_SHIFT;
  1506. if (priv->cfg->bt_params &&
  1507. priv->cfg->bt_params->bt_sco_disable)
  1508. basic.flags |= IWLAGN_BT_FLAG_SYNC_2_BT_DISABLE;
  1509. if (priv->bt_ch_announce)
  1510. basic.flags |= IWLAGN_BT_FLAG_CHANNEL_INHIBITION;
  1511. IWL_DEBUG_COEX(priv, "BT coex flag: 0X%x\n", basic.flags);
  1512. }
  1513. priv->bt_enable_flag = basic.flags;
  1514. if (priv->bt_full_concurrent)
  1515. memcpy(basic.bt3_lookup_table, iwlagn_concurrent_lookup,
  1516. sizeof(iwlagn_concurrent_lookup));
  1517. else
  1518. memcpy(basic.bt3_lookup_table, iwlagn_def_3w_lookup,
  1519. sizeof(iwlagn_def_3w_lookup));
  1520. IWL_DEBUG_COEX(priv, "BT coex %s in %s mode\n",
  1521. basic.flags ? "active" : "disabled",
  1522. priv->bt_full_concurrent ?
  1523. "full concurrency" : "3-wire");
  1524. if (priv->cfg->bt_params->bt_session_2) {
  1525. memcpy(&bt_cmd_2000.basic, &basic,
  1526. sizeof(basic));
  1527. ret = iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1528. sizeof(bt_cmd_2000), &bt_cmd_2000);
  1529. } else {
  1530. memcpy(&bt_cmd_6000.basic, &basic,
  1531. sizeof(basic));
  1532. ret = iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1533. sizeof(bt_cmd_6000), &bt_cmd_6000);
  1534. }
  1535. if (ret)
  1536. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1537. }
  1538. static void iwlagn_bt_traffic_change_work(struct work_struct *work)
  1539. {
  1540. struct iwl_priv *priv =
  1541. container_of(work, struct iwl_priv, bt_traffic_change_work);
  1542. struct iwl_rxon_context *ctx;
  1543. int smps_request = -1;
  1544. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1545. /* bt coex disabled */
  1546. return;
  1547. }
  1548. /*
  1549. * Note: bt_traffic_load can be overridden by scan complete and
  1550. * coex profile notifications. Ignore that since only bad consequence
  1551. * can be not matching debug print with actual state.
  1552. */
  1553. IWL_DEBUG_COEX(priv, "BT traffic load changes: %d\n",
  1554. priv->bt_traffic_load);
  1555. switch (priv->bt_traffic_load) {
  1556. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1557. if (priv->bt_status)
  1558. smps_request = IEEE80211_SMPS_DYNAMIC;
  1559. else
  1560. smps_request = IEEE80211_SMPS_AUTOMATIC;
  1561. break;
  1562. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1563. smps_request = IEEE80211_SMPS_DYNAMIC;
  1564. break;
  1565. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1566. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1567. smps_request = IEEE80211_SMPS_STATIC;
  1568. break;
  1569. default:
  1570. IWL_ERR(priv, "Invalid BT traffic load: %d\n",
  1571. priv->bt_traffic_load);
  1572. break;
  1573. }
  1574. mutex_lock(&priv->mutex);
  1575. /*
  1576. * We can not send command to firmware while scanning. When the scan
  1577. * complete we will schedule this work again. We do check with mutex
  1578. * locked to prevent new scan request to arrive. We do not check
  1579. * STATUS_SCANNING to avoid race when queue_work two times from
  1580. * different notifications, but quit and not perform any work at all.
  1581. */
  1582. if (test_bit(STATUS_SCAN_HW, &priv->status))
  1583. goto out;
  1584. if (priv->cfg->ops->lib->update_chain_flags)
  1585. priv->cfg->ops->lib->update_chain_flags(priv);
  1586. if (smps_request != -1) {
  1587. priv->current_ht_config.smps = smps_request;
  1588. for_each_context(priv, ctx) {
  1589. if (ctx->vif && ctx->vif->type == NL80211_IFTYPE_STATION)
  1590. ieee80211_request_smps(ctx->vif, smps_request);
  1591. }
  1592. }
  1593. out:
  1594. mutex_unlock(&priv->mutex);
  1595. }
  1596. static void iwlagn_print_uartmsg(struct iwl_priv *priv,
  1597. struct iwl_bt_uart_msg *uart_msg)
  1598. {
  1599. IWL_DEBUG_COEX(priv, "Message Type = 0x%X, SSN = 0x%X, "
  1600. "Update Req = 0x%X",
  1601. (BT_UART_MSG_FRAME1MSGTYPE_MSK & uart_msg->frame1) >>
  1602. BT_UART_MSG_FRAME1MSGTYPE_POS,
  1603. (BT_UART_MSG_FRAME1SSN_MSK & uart_msg->frame1) >>
  1604. BT_UART_MSG_FRAME1SSN_POS,
  1605. (BT_UART_MSG_FRAME1UPDATEREQ_MSK & uart_msg->frame1) >>
  1606. BT_UART_MSG_FRAME1UPDATEREQ_POS);
  1607. IWL_DEBUG_COEX(priv, "Open connections = 0x%X, Traffic load = 0x%X, "
  1608. "Chl_SeqN = 0x%X, In band = 0x%X",
  1609. (BT_UART_MSG_FRAME2OPENCONNECTIONS_MSK & uart_msg->frame2) >>
  1610. BT_UART_MSG_FRAME2OPENCONNECTIONS_POS,
  1611. (BT_UART_MSG_FRAME2TRAFFICLOAD_MSK & uart_msg->frame2) >>
  1612. BT_UART_MSG_FRAME2TRAFFICLOAD_POS,
  1613. (BT_UART_MSG_FRAME2CHLSEQN_MSK & uart_msg->frame2) >>
  1614. BT_UART_MSG_FRAME2CHLSEQN_POS,
  1615. (BT_UART_MSG_FRAME2INBAND_MSK & uart_msg->frame2) >>
  1616. BT_UART_MSG_FRAME2INBAND_POS);
  1617. IWL_DEBUG_COEX(priv, "SCO/eSCO = 0x%X, Sniff = 0x%X, A2DP = 0x%X, "
  1618. "ACL = 0x%X, Master = 0x%X, OBEX = 0x%X",
  1619. (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3) >>
  1620. BT_UART_MSG_FRAME3SCOESCO_POS,
  1621. (BT_UART_MSG_FRAME3SNIFF_MSK & uart_msg->frame3) >>
  1622. BT_UART_MSG_FRAME3SNIFF_POS,
  1623. (BT_UART_MSG_FRAME3A2DP_MSK & uart_msg->frame3) >>
  1624. BT_UART_MSG_FRAME3A2DP_POS,
  1625. (BT_UART_MSG_FRAME3ACL_MSK & uart_msg->frame3) >>
  1626. BT_UART_MSG_FRAME3ACL_POS,
  1627. (BT_UART_MSG_FRAME3MASTER_MSK & uart_msg->frame3) >>
  1628. BT_UART_MSG_FRAME3MASTER_POS,
  1629. (BT_UART_MSG_FRAME3OBEX_MSK & uart_msg->frame3) >>
  1630. BT_UART_MSG_FRAME3OBEX_POS);
  1631. IWL_DEBUG_COEX(priv, "Idle duration = 0x%X",
  1632. (BT_UART_MSG_FRAME4IDLEDURATION_MSK & uart_msg->frame4) >>
  1633. BT_UART_MSG_FRAME4IDLEDURATION_POS);
  1634. IWL_DEBUG_COEX(priv, "Tx Activity = 0x%X, Rx Activity = 0x%X, "
  1635. "eSCO Retransmissions = 0x%X",
  1636. (BT_UART_MSG_FRAME5TXACTIVITY_MSK & uart_msg->frame5) >>
  1637. BT_UART_MSG_FRAME5TXACTIVITY_POS,
  1638. (BT_UART_MSG_FRAME5RXACTIVITY_MSK & uart_msg->frame5) >>
  1639. BT_UART_MSG_FRAME5RXACTIVITY_POS,
  1640. (BT_UART_MSG_FRAME5ESCORETRANSMIT_MSK & uart_msg->frame5) >>
  1641. BT_UART_MSG_FRAME5ESCORETRANSMIT_POS);
  1642. IWL_DEBUG_COEX(priv, "Sniff Interval = 0x%X, Discoverable = 0x%X",
  1643. (BT_UART_MSG_FRAME6SNIFFINTERVAL_MSK & uart_msg->frame6) >>
  1644. BT_UART_MSG_FRAME6SNIFFINTERVAL_POS,
  1645. (BT_UART_MSG_FRAME6DISCOVERABLE_MSK & uart_msg->frame6) >>
  1646. BT_UART_MSG_FRAME6DISCOVERABLE_POS);
  1647. IWL_DEBUG_COEX(priv, "Sniff Activity = 0x%X, Page = "
  1648. "0x%X, Inquiry = 0x%X, Connectable = 0x%X",
  1649. (BT_UART_MSG_FRAME7SNIFFACTIVITY_MSK & uart_msg->frame7) >>
  1650. BT_UART_MSG_FRAME7SNIFFACTIVITY_POS,
  1651. (BT_UART_MSG_FRAME7PAGE_MSK & uart_msg->frame7) >>
  1652. BT_UART_MSG_FRAME7PAGE_POS,
  1653. (BT_UART_MSG_FRAME7INQUIRY_MSK & uart_msg->frame7) >>
  1654. BT_UART_MSG_FRAME7INQUIRY_POS,
  1655. (BT_UART_MSG_FRAME7CONNECTABLE_MSK & uart_msg->frame7) >>
  1656. BT_UART_MSG_FRAME7CONNECTABLE_POS);
  1657. }
  1658. static void iwlagn_set_kill_msk(struct iwl_priv *priv,
  1659. struct iwl_bt_uart_msg *uart_msg)
  1660. {
  1661. u8 kill_msk;
  1662. static const __le32 bt_kill_ack_msg[2] = {
  1663. IWLAGN_BT_KILL_ACK_MASK_DEFAULT,
  1664. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1665. static const __le32 bt_kill_cts_msg[2] = {
  1666. IWLAGN_BT_KILL_CTS_MASK_DEFAULT,
  1667. IWLAGN_BT_KILL_ACK_CTS_MASK_SCO };
  1668. kill_msk = (BT_UART_MSG_FRAME3SCOESCO_MSK & uart_msg->frame3)
  1669. ? 1 : 0;
  1670. if (priv->kill_ack_mask != bt_kill_ack_msg[kill_msk] ||
  1671. priv->kill_cts_mask != bt_kill_cts_msg[kill_msk]) {
  1672. priv->bt_valid |= IWLAGN_BT_VALID_KILL_ACK_MASK;
  1673. priv->kill_ack_mask = bt_kill_ack_msg[kill_msk];
  1674. priv->bt_valid |= IWLAGN_BT_VALID_KILL_CTS_MASK;
  1675. priv->kill_cts_mask = bt_kill_cts_msg[kill_msk];
  1676. /* schedule to send runtime bt_config */
  1677. queue_work(priv->workqueue, &priv->bt_runtime_config);
  1678. }
  1679. }
  1680. void iwlagn_bt_coex_profile_notif(struct iwl_priv *priv,
  1681. struct iwl_rx_mem_buffer *rxb)
  1682. {
  1683. unsigned long flags;
  1684. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1685. struct iwl_bt_coex_profile_notif *coex = &pkt->u.bt_coex_profile_notif;
  1686. struct iwl_bt_uart_msg *uart_msg = &coex->last_bt_uart_msg;
  1687. if (priv->bt_enable_flag == IWLAGN_BT_FLAG_COEX_MODE_DISABLED) {
  1688. /* bt coex disabled */
  1689. return;
  1690. }
  1691. IWL_DEBUG_COEX(priv, "BT Coex notification:\n");
  1692. IWL_DEBUG_COEX(priv, " status: %d\n", coex->bt_status);
  1693. IWL_DEBUG_COEX(priv, " traffic load: %d\n", coex->bt_traffic_load);
  1694. IWL_DEBUG_COEX(priv, " CI compliance: %d\n",
  1695. coex->bt_ci_compliance);
  1696. iwlagn_print_uartmsg(priv, uart_msg);
  1697. priv->last_bt_traffic_load = priv->bt_traffic_load;
  1698. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  1699. if (priv->bt_status != coex->bt_status ||
  1700. priv->last_bt_traffic_load != coex->bt_traffic_load) {
  1701. if (coex->bt_status) {
  1702. /* BT on */
  1703. if (!priv->bt_ch_announce)
  1704. priv->bt_traffic_load =
  1705. IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1706. else
  1707. priv->bt_traffic_load =
  1708. coex->bt_traffic_load;
  1709. } else {
  1710. /* BT off */
  1711. priv->bt_traffic_load =
  1712. IWL_BT_COEX_TRAFFIC_LOAD_NONE;
  1713. }
  1714. priv->bt_status = coex->bt_status;
  1715. queue_work(priv->workqueue,
  1716. &priv->bt_traffic_change_work);
  1717. }
  1718. }
  1719. iwlagn_set_kill_msk(priv, uart_msg);
  1720. /* FIXME: based on notification, adjust the prio_boost */
  1721. spin_lock_irqsave(&priv->lock, flags);
  1722. priv->bt_ci_compliance = coex->bt_ci_compliance;
  1723. spin_unlock_irqrestore(&priv->lock, flags);
  1724. }
  1725. void iwlagn_bt_rx_handler_setup(struct iwl_priv *priv)
  1726. {
  1727. iwlagn_rx_handler_setup(priv);
  1728. priv->rx_handlers[REPLY_BT_COEX_PROFILE_NOTIF] =
  1729. iwlagn_bt_coex_profile_notif;
  1730. }
  1731. void iwlagn_bt_setup_deferred_work(struct iwl_priv *priv)
  1732. {
  1733. iwlagn_setup_deferred_work(priv);
  1734. INIT_WORK(&priv->bt_traffic_change_work,
  1735. iwlagn_bt_traffic_change_work);
  1736. }
  1737. void iwlagn_bt_cancel_deferred_work(struct iwl_priv *priv)
  1738. {
  1739. cancel_work_sync(&priv->bt_traffic_change_work);
  1740. }
  1741. static bool is_single_rx_stream(struct iwl_priv *priv)
  1742. {
  1743. return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  1744. priv->current_ht_config.single_chain_sufficient;
  1745. }
  1746. #define IWL_NUM_RX_CHAINS_MULTIPLE 3
  1747. #define IWL_NUM_RX_CHAINS_SINGLE 2
  1748. #define IWL_NUM_IDLE_CHAINS_DUAL 2
  1749. #define IWL_NUM_IDLE_CHAINS_SINGLE 1
  1750. /*
  1751. * Determine how many receiver/antenna chains to use.
  1752. *
  1753. * More provides better reception via diversity. Fewer saves power
  1754. * at the expense of throughput, but only when not in powersave to
  1755. * start with.
  1756. *
  1757. * MIMO (dual stream) requires at least 2, but works better with 3.
  1758. * This does not determine *which* chains to use, just how many.
  1759. */
  1760. static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
  1761. {
  1762. if (priv->cfg->bt_params &&
  1763. priv->cfg->bt_params->advanced_bt_coexist &&
  1764. (priv->bt_full_concurrent ||
  1765. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1766. /*
  1767. * only use chain 'A' in bt high traffic load or
  1768. * full concurrency mode
  1769. */
  1770. return IWL_NUM_RX_CHAINS_SINGLE;
  1771. }
  1772. /* # of Rx chains to use when expecting MIMO. */
  1773. if (is_single_rx_stream(priv))
  1774. return IWL_NUM_RX_CHAINS_SINGLE;
  1775. else
  1776. return IWL_NUM_RX_CHAINS_MULTIPLE;
  1777. }
  1778. /*
  1779. * When we are in power saving mode, unless device support spatial
  1780. * multiplexing power save, use the active count for rx chain count.
  1781. */
  1782. static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
  1783. {
  1784. /* # Rx chains when idling, depending on SMPS mode */
  1785. switch (priv->current_ht_config.smps) {
  1786. case IEEE80211_SMPS_STATIC:
  1787. case IEEE80211_SMPS_DYNAMIC:
  1788. return IWL_NUM_IDLE_CHAINS_SINGLE;
  1789. case IEEE80211_SMPS_OFF:
  1790. return active_cnt;
  1791. default:
  1792. WARN(1, "invalid SMPS mode %d",
  1793. priv->current_ht_config.smps);
  1794. return active_cnt;
  1795. }
  1796. }
  1797. /* up to 4 chains */
  1798. static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
  1799. {
  1800. u8 res;
  1801. res = (chain_bitmap & BIT(0)) >> 0;
  1802. res += (chain_bitmap & BIT(1)) >> 1;
  1803. res += (chain_bitmap & BIT(2)) >> 2;
  1804. res += (chain_bitmap & BIT(3)) >> 3;
  1805. return res;
  1806. }
  1807. /**
  1808. * iwlagn_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  1809. *
  1810. * Selects how many and which Rx receivers/antennas/chains to use.
  1811. * This should not be used for scan command ... it puts data in wrong place.
  1812. */
  1813. void iwlagn_set_rxon_chain(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1814. {
  1815. bool is_single = is_single_rx_stream(priv);
  1816. bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
  1817. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  1818. u32 active_chains;
  1819. u16 rx_chain;
  1820. /* Tell uCode which antennas are actually connected.
  1821. * Before first association, we assume all antennas are connected.
  1822. * Just after first association, iwl_chain_noise_calibration()
  1823. * checks which antennas actually *are* connected. */
  1824. if (priv->chain_noise_data.active_chains)
  1825. active_chains = priv->chain_noise_data.active_chains;
  1826. else
  1827. active_chains = priv->hw_params.valid_rx_ant;
  1828. if (priv->cfg->bt_params &&
  1829. priv->cfg->bt_params->advanced_bt_coexist &&
  1830. (priv->bt_full_concurrent ||
  1831. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)) {
  1832. /*
  1833. * only use chain 'A' in bt high traffic load or
  1834. * full concurrency mode
  1835. */
  1836. active_chains = first_antenna(active_chains);
  1837. }
  1838. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  1839. /* How many receivers should we use? */
  1840. active_rx_cnt = iwl_get_active_rx_chain_count(priv);
  1841. idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
  1842. /* correct rx chain count according hw settings
  1843. * and chain noise calibration
  1844. */
  1845. valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
  1846. if (valid_rx_cnt < active_rx_cnt)
  1847. active_rx_cnt = valid_rx_cnt;
  1848. if (valid_rx_cnt < idle_rx_cnt)
  1849. idle_rx_cnt = valid_rx_cnt;
  1850. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  1851. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  1852. ctx->staging.rx_chain = cpu_to_le16(rx_chain);
  1853. if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
  1854. ctx->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1855. else
  1856. ctx->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1857. IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
  1858. ctx->staging.rx_chain,
  1859. active_rx_cnt, idle_rx_cnt);
  1860. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  1861. active_rx_cnt < idle_rx_cnt);
  1862. }
  1863. u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant, u8 valid)
  1864. {
  1865. int i;
  1866. u8 ind = ant;
  1867. if (priv->band == IEEE80211_BAND_2GHZ &&
  1868. priv->bt_traffic_load >= IWL_BT_COEX_TRAFFIC_LOAD_HIGH)
  1869. return 0;
  1870. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  1871. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  1872. if (valid & BIT(ind))
  1873. return ind;
  1874. }
  1875. return ant;
  1876. }
  1877. static const char *get_csr_string(int cmd)
  1878. {
  1879. switch (cmd) {
  1880. IWL_CMD(CSR_HW_IF_CONFIG_REG);
  1881. IWL_CMD(CSR_INT_COALESCING);
  1882. IWL_CMD(CSR_INT);
  1883. IWL_CMD(CSR_INT_MASK);
  1884. IWL_CMD(CSR_FH_INT_STATUS);
  1885. IWL_CMD(CSR_GPIO_IN);
  1886. IWL_CMD(CSR_RESET);
  1887. IWL_CMD(CSR_GP_CNTRL);
  1888. IWL_CMD(CSR_HW_REV);
  1889. IWL_CMD(CSR_EEPROM_REG);
  1890. IWL_CMD(CSR_EEPROM_GP);
  1891. IWL_CMD(CSR_OTP_GP_REG);
  1892. IWL_CMD(CSR_GIO_REG);
  1893. IWL_CMD(CSR_GP_UCODE_REG);
  1894. IWL_CMD(CSR_GP_DRIVER_REG);
  1895. IWL_CMD(CSR_UCODE_DRV_GP1);
  1896. IWL_CMD(CSR_UCODE_DRV_GP2);
  1897. IWL_CMD(CSR_LED_REG);
  1898. IWL_CMD(CSR_DRAM_INT_TBL_REG);
  1899. IWL_CMD(CSR_GIO_CHICKEN_BITS);
  1900. IWL_CMD(CSR_ANA_PLL_CFG);
  1901. IWL_CMD(CSR_HW_REV_WA_REG);
  1902. IWL_CMD(CSR_DBG_HPET_MEM_REG);
  1903. default:
  1904. return "UNKNOWN";
  1905. }
  1906. }
  1907. void iwl_dump_csr(struct iwl_priv *priv)
  1908. {
  1909. int i;
  1910. static const u32 csr_tbl[] = {
  1911. CSR_HW_IF_CONFIG_REG,
  1912. CSR_INT_COALESCING,
  1913. CSR_INT,
  1914. CSR_INT_MASK,
  1915. CSR_FH_INT_STATUS,
  1916. CSR_GPIO_IN,
  1917. CSR_RESET,
  1918. CSR_GP_CNTRL,
  1919. CSR_HW_REV,
  1920. CSR_EEPROM_REG,
  1921. CSR_EEPROM_GP,
  1922. CSR_OTP_GP_REG,
  1923. CSR_GIO_REG,
  1924. CSR_GP_UCODE_REG,
  1925. CSR_GP_DRIVER_REG,
  1926. CSR_UCODE_DRV_GP1,
  1927. CSR_UCODE_DRV_GP2,
  1928. CSR_LED_REG,
  1929. CSR_DRAM_INT_TBL_REG,
  1930. CSR_GIO_CHICKEN_BITS,
  1931. CSR_ANA_PLL_CFG,
  1932. CSR_HW_REV_WA_REG,
  1933. CSR_DBG_HPET_MEM_REG
  1934. };
  1935. IWL_ERR(priv, "CSR values:\n");
  1936. IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
  1937. "CSR_INT_PERIODIC_REG)\n");
  1938. for (i = 0; i < ARRAY_SIZE(csr_tbl); i++) {
  1939. IWL_ERR(priv, " %25s: 0X%08x\n",
  1940. get_csr_string(csr_tbl[i]),
  1941. iwl_read32(priv, csr_tbl[i]));
  1942. }
  1943. }
  1944. static const char *get_fh_string(int cmd)
  1945. {
  1946. switch (cmd) {
  1947. IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
  1948. IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
  1949. IWL_CMD(FH_RSCSR_CHNL0_WPTR);
  1950. IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
  1951. IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
  1952. IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
  1953. IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1954. IWL_CMD(FH_TSSR_TX_STATUS_REG);
  1955. IWL_CMD(FH_TSSR_TX_ERROR_REG);
  1956. default:
  1957. return "UNKNOWN";
  1958. }
  1959. }
  1960. int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
  1961. {
  1962. int i;
  1963. #ifdef CONFIG_IWLWIFI_DEBUG
  1964. int pos = 0;
  1965. size_t bufsz = 0;
  1966. #endif
  1967. static const u32 fh_tbl[] = {
  1968. FH_RSCSR_CHNL0_STTS_WPTR_REG,
  1969. FH_RSCSR_CHNL0_RBDCB_BASE_REG,
  1970. FH_RSCSR_CHNL0_WPTR,
  1971. FH_MEM_RCSR_CHNL0_CONFIG_REG,
  1972. FH_MEM_RSSR_SHARED_CTRL_REG,
  1973. FH_MEM_RSSR_RX_STATUS_REG,
  1974. FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1975. FH_TSSR_TX_STATUS_REG,
  1976. FH_TSSR_TX_ERROR_REG
  1977. };
  1978. #ifdef CONFIG_IWLWIFI_DEBUG
  1979. if (display) {
  1980. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1981. *buf = kmalloc(bufsz, GFP_KERNEL);
  1982. if (!*buf)
  1983. return -ENOMEM;
  1984. pos += scnprintf(*buf + pos, bufsz - pos,
  1985. "FH register values:\n");
  1986. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1987. pos += scnprintf(*buf + pos, bufsz - pos,
  1988. " %34s: 0X%08x\n",
  1989. get_fh_string(fh_tbl[i]),
  1990. iwl_read_direct32(priv, fh_tbl[i]));
  1991. }
  1992. return pos;
  1993. }
  1994. #endif
  1995. IWL_ERR(priv, "FH register values:\n");
  1996. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1997. IWL_ERR(priv, " %34s: 0X%08x\n",
  1998. get_fh_string(fh_tbl[i]),
  1999. iwl_read_direct32(priv, fh_tbl[i]));
  2000. }
  2001. return 0;
  2002. }
  2003. /* notification wait support */
  2004. void iwlagn_init_notification_wait(struct iwl_priv *priv,
  2005. struct iwl_notification_wait *wait_entry,
  2006. u8 cmd,
  2007. void (*fn)(struct iwl_priv *priv,
  2008. struct iwl_rx_packet *pkt,
  2009. void *data),
  2010. void *fn_data)
  2011. {
  2012. wait_entry->fn = fn;
  2013. wait_entry->fn_data = fn_data;
  2014. wait_entry->cmd = cmd;
  2015. wait_entry->triggered = false;
  2016. wait_entry->aborted = false;
  2017. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2018. list_add(&wait_entry->list, &priv->_agn.notif_waits);
  2019. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2020. }
  2021. int iwlagn_wait_notification(struct iwl_priv *priv,
  2022. struct iwl_notification_wait *wait_entry,
  2023. unsigned long timeout)
  2024. {
  2025. int ret;
  2026. ret = wait_event_timeout(priv->_agn.notif_waitq,
  2027. wait_entry->triggered || wait_entry->aborted,
  2028. timeout);
  2029. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2030. list_del(&wait_entry->list);
  2031. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2032. if (wait_entry->aborted)
  2033. return -EIO;
  2034. /* return value is always >= 0 */
  2035. if (ret <= 0)
  2036. return -ETIMEDOUT;
  2037. return 0;
  2038. }
  2039. void iwlagn_remove_notification(struct iwl_priv *priv,
  2040. struct iwl_notification_wait *wait_entry)
  2041. {
  2042. spin_lock_bh(&priv->_agn.notif_wait_lock);
  2043. list_del(&wait_entry->list);
  2044. spin_unlock_bh(&priv->_agn.notif_wait_lock);
  2045. }
  2046. int iwlagn_start_device(struct iwl_priv *priv)
  2047. {
  2048. int ret;
  2049. if ((priv->cfg->sku & EEPROM_SKU_CAP_AMT_ENABLE) &&
  2050. iwl_prepare_card_hw(priv)) {
  2051. IWL_WARN(priv, "Exit HW not ready\n");
  2052. return -EIO;
  2053. }
  2054. /* If platform's RF_KILL switch is NOT set to KILL */
  2055. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2056. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2057. else
  2058. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2059. if (iwl_is_rfkill(priv)) {
  2060. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  2061. iwl_enable_interrupts(priv);
  2062. return -ERFKILL;
  2063. }
  2064. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2065. ret = iwlagn_hw_nic_init(priv);
  2066. if (ret) {
  2067. IWL_ERR(priv, "Unable to init nic\n");
  2068. return ret;
  2069. }
  2070. /* make sure rfkill handshake bits are cleared */
  2071. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2072. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2073. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2074. /* clear (again), then enable host interrupts */
  2075. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2076. iwl_enable_interrupts(priv);
  2077. /* really make sure rfkill handshake bits are cleared */
  2078. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2079. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2080. return 0;
  2081. }
  2082. void iwlagn_stop_device(struct iwl_priv *priv)
  2083. {
  2084. unsigned long flags;
  2085. /* stop and reset the on-board processor */
  2086. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2087. /* tell the device to stop sending interrupts */
  2088. spin_lock_irqsave(&priv->lock, flags);
  2089. iwl_disable_interrupts(priv);
  2090. spin_unlock_irqrestore(&priv->lock, flags);
  2091. iwl_synchronize_irq(priv);
  2092. /* device going down, Stop using ICT table */
  2093. iwl_disable_ict(priv);
  2094. /*
  2095. * If a HW restart happens during firmware loading,
  2096. * then the firmware loading might call this function
  2097. * and later it might be called again due to the
  2098. * restart. So don't process again if the device is
  2099. * already dead.
  2100. */
  2101. if (test_bit(STATUS_DEVICE_ENABLED, &priv->status)) {
  2102. iwlagn_txq_ctx_stop(priv);
  2103. iwlagn_rxq_stop(priv);
  2104. /* Power-down device's busmaster DMA clocks */
  2105. iwl_write_prph(priv, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  2106. udelay(5);
  2107. }
  2108. /* Make sure (redundant) we've released our request to stay awake */
  2109. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2110. /* Stop the device, and put it in low power state */
  2111. iwl_apm_stop(priv);
  2112. }