iwl-agn-lib.c 70 KB

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