iwl-agn-lib.c 70 KB

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