iwl-agn-lib.c 69 KB

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