iwl-agn-tx.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/sched.h>
  33. #include "iwl-dev.h"
  34. #include "iwl-core.h"
  35. #include "iwl-sta.h"
  36. #include "iwl-io.h"
  37. #include "iwl-helpers.h"
  38. #include "iwl-agn-hw.h"
  39. #include "iwl-agn.h"
  40. /*
  41. * mac80211 queues, ACs, hardware queues, FIFOs.
  42. *
  43. * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
  44. *
  45. * Mac80211 uses the following numbers, which we get as from it
  46. * by way of skb_get_queue_mapping(skb):
  47. *
  48. * VO 0
  49. * VI 1
  50. * BE 2
  51. * BK 3
  52. *
  53. *
  54. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  55. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  56. * own queue per aggregation session (RA/TID combination), such queues are
  57. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  58. * order to map frames to the right queue, we also need an AC->hw queue
  59. * mapping. This is implemented here.
  60. *
  61. * Due to the way hw queues are set up (by the hw specific modules like
  62. * iwl-4965.c, iwl-5000.c etc.), the AC->hw queue mapping is the identity
  63. * mapping.
  64. */
  65. static const u8 tid_to_ac[] = {
  66. IEEE80211_AC_BE,
  67. IEEE80211_AC_BK,
  68. IEEE80211_AC_BK,
  69. IEEE80211_AC_BE,
  70. IEEE80211_AC_VI,
  71. IEEE80211_AC_VI,
  72. IEEE80211_AC_VO,
  73. IEEE80211_AC_VO
  74. };
  75. static inline int get_ac_from_tid(u16 tid)
  76. {
  77. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  78. return tid_to_ac[tid];
  79. /* no support for TIDs 8-15 yet */
  80. return -EINVAL;
  81. }
  82. static inline int get_fifo_from_tid(struct iwl_rxon_context *ctx, u16 tid)
  83. {
  84. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  85. return ctx->ac_to_fifo[tid_to_ac[tid]];
  86. /* no support for TIDs 8-15 yet */
  87. return -EINVAL;
  88. }
  89. /**
  90. * iwlagn_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  91. */
  92. void iwlagn_txq_update_byte_cnt_tbl(struct iwl_priv *priv,
  93. struct iwl_tx_queue *txq,
  94. u16 byte_cnt)
  95. {
  96. struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr;
  97. int write_ptr = txq->q.write_ptr;
  98. int txq_id = txq->q.id;
  99. u8 sec_ctl = 0;
  100. u8 sta_id = 0;
  101. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  102. __le16 bc_ent;
  103. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  104. if (txq_id != priv->cmd_queue) {
  105. sta_id = txq->cmd[txq->q.write_ptr]->cmd.tx.sta_id;
  106. sec_ctl = txq->cmd[txq->q.write_ptr]->cmd.tx.sec_ctl;
  107. switch (sec_ctl & TX_CMD_SEC_MSK) {
  108. case TX_CMD_SEC_CCM:
  109. len += CCMP_MIC_LEN;
  110. break;
  111. case TX_CMD_SEC_TKIP:
  112. len += TKIP_ICV_LEN;
  113. break;
  114. case TX_CMD_SEC_WEP:
  115. len += WEP_IV_LEN + WEP_ICV_LEN;
  116. break;
  117. }
  118. }
  119. bc_ent = cpu_to_le16((len & 0xFFF) | (sta_id << 12));
  120. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  121. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  122. scd_bc_tbl[txq_id].
  123. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  124. }
  125. void iwlagn_txq_inval_byte_cnt_tbl(struct iwl_priv *priv,
  126. struct iwl_tx_queue *txq)
  127. {
  128. struct iwlagn_scd_bc_tbl *scd_bc_tbl = priv->scd_bc_tbls.addr;
  129. int txq_id = txq->q.id;
  130. int read_ptr = txq->q.read_ptr;
  131. u8 sta_id = 0;
  132. __le16 bc_ent;
  133. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  134. if (txq_id != priv->cmd_queue)
  135. sta_id = txq->cmd[read_ptr]->cmd.tx.sta_id;
  136. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  137. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  138. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  139. scd_bc_tbl[txq_id].
  140. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  141. }
  142. static int iwlagn_tx_queue_set_q2ratid(struct iwl_priv *priv, u16 ra_tid,
  143. u16 txq_id)
  144. {
  145. u32 tbl_dw_addr;
  146. u32 tbl_dw;
  147. u16 scd_q2ratid;
  148. scd_q2ratid = ra_tid & IWL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  149. tbl_dw_addr = priv->scd_base_addr +
  150. IWLAGN_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  151. tbl_dw = iwl_read_targ_mem(priv, tbl_dw_addr);
  152. if (txq_id & 0x1)
  153. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  154. else
  155. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  156. iwl_write_targ_mem(priv, tbl_dw_addr, tbl_dw);
  157. return 0;
  158. }
  159. static void iwlagn_tx_queue_stop_scheduler(struct iwl_priv *priv, u16 txq_id)
  160. {
  161. /* Simply stop the queue, but don't change any configuration;
  162. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  163. iwl_write_prph(priv,
  164. IWLAGN_SCD_QUEUE_STATUS_BITS(txq_id),
  165. (0 << IWLAGN_SCD_QUEUE_STTS_REG_POS_ACTIVE)|
  166. (1 << IWLAGN_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  167. }
  168. void iwlagn_set_wr_ptrs(struct iwl_priv *priv,
  169. int txq_id, u32 index)
  170. {
  171. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  172. (index & 0xff) | (txq_id << 8));
  173. iwl_write_prph(priv, IWLAGN_SCD_QUEUE_RDPTR(txq_id), index);
  174. }
  175. void iwlagn_tx_queue_set_status(struct iwl_priv *priv,
  176. struct iwl_tx_queue *txq,
  177. int tx_fifo_id, int scd_retry)
  178. {
  179. int txq_id = txq->q.id;
  180. int active = test_bit(txq_id, &priv->txq_ctx_active_msk) ? 1 : 0;
  181. iwl_write_prph(priv, IWLAGN_SCD_QUEUE_STATUS_BITS(txq_id),
  182. (active << IWLAGN_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  183. (tx_fifo_id << IWLAGN_SCD_QUEUE_STTS_REG_POS_TXF) |
  184. (1 << IWLAGN_SCD_QUEUE_STTS_REG_POS_WSL) |
  185. IWLAGN_SCD_QUEUE_STTS_REG_MSK);
  186. txq->sched_retry = scd_retry;
  187. IWL_DEBUG_INFO(priv, "%s %s Queue %d on FIFO %d\n",
  188. active ? "Activate" : "Deactivate",
  189. scd_retry ? "BA" : "AC/CMD", txq_id, tx_fifo_id);
  190. }
  191. int iwlagn_txq_agg_enable(struct iwl_priv *priv, int txq_id,
  192. int tx_fifo, int sta_id, int tid, u16 ssn_idx)
  193. {
  194. unsigned long flags;
  195. u16 ra_tid;
  196. int ret;
  197. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  198. (IWLAGN_FIRST_AMPDU_QUEUE +
  199. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  200. IWL_WARN(priv,
  201. "queue number out of range: %d, must be %d to %d\n",
  202. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  203. IWLAGN_FIRST_AMPDU_QUEUE +
  204. priv->cfg->base_params->num_of_ampdu_queues - 1);
  205. return -EINVAL;
  206. }
  207. ra_tid = BUILD_RAxTID(sta_id, tid);
  208. /* Modify device's station table to Tx this TID */
  209. ret = iwl_sta_tx_modify_enable_tid(priv, sta_id, tid);
  210. if (ret)
  211. return ret;
  212. spin_lock_irqsave(&priv->lock, flags);
  213. /* Stop this Tx queue before configuring it */
  214. iwlagn_tx_queue_stop_scheduler(priv, txq_id);
  215. /* Map receiver-address / traffic-ID to this queue */
  216. iwlagn_tx_queue_set_q2ratid(priv, ra_tid, txq_id);
  217. /* Set this queue as a chain-building queue */
  218. iwl_set_bits_prph(priv, IWLAGN_SCD_QUEUECHAIN_SEL, (1<<txq_id));
  219. /* enable aggregations for the queue */
  220. iwl_set_bits_prph(priv, IWLAGN_SCD_AGGR_SEL, (1<<txq_id));
  221. /* Place first TFD at index corresponding to start sequence number.
  222. * Assumes that ssn_idx is valid (!= 0xFFF) */
  223. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  224. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  225. iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx);
  226. /* Set up Tx window size and frame limit for this queue */
  227. iwl_write_targ_mem(priv, priv->scd_base_addr +
  228. IWLAGN_SCD_CONTEXT_QUEUE_OFFSET(txq_id) +
  229. sizeof(u32),
  230. ((SCD_WIN_SIZE <<
  231. IWLAGN_SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  232. IWLAGN_SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  233. ((SCD_FRAME_LIMIT <<
  234. IWLAGN_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  235. IWLAGN_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  236. iwl_set_bits_prph(priv, IWLAGN_SCD_INTERRUPT_MASK, (1 << txq_id));
  237. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  238. iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 1);
  239. spin_unlock_irqrestore(&priv->lock, flags);
  240. return 0;
  241. }
  242. int iwlagn_txq_agg_disable(struct iwl_priv *priv, u16 txq_id,
  243. u16 ssn_idx, u8 tx_fifo)
  244. {
  245. if ((IWLAGN_FIRST_AMPDU_QUEUE > txq_id) ||
  246. (IWLAGN_FIRST_AMPDU_QUEUE +
  247. priv->cfg->base_params->num_of_ampdu_queues <= txq_id)) {
  248. IWL_ERR(priv,
  249. "queue number out of range: %d, must be %d to %d\n",
  250. txq_id, IWLAGN_FIRST_AMPDU_QUEUE,
  251. IWLAGN_FIRST_AMPDU_QUEUE +
  252. priv->cfg->base_params->num_of_ampdu_queues - 1);
  253. return -EINVAL;
  254. }
  255. iwlagn_tx_queue_stop_scheduler(priv, txq_id);
  256. iwl_clear_bits_prph(priv, IWLAGN_SCD_AGGR_SEL, (1 << txq_id));
  257. priv->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  258. priv->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  259. /* supposes that ssn_idx is valid (!= 0xFFF) */
  260. iwlagn_set_wr_ptrs(priv, txq_id, ssn_idx);
  261. iwl_clear_bits_prph(priv, IWLAGN_SCD_INTERRUPT_MASK, (1 << txq_id));
  262. iwl_txq_ctx_deactivate(priv, txq_id);
  263. iwlagn_tx_queue_set_status(priv, &priv->txq[txq_id], tx_fifo, 0);
  264. return 0;
  265. }
  266. /*
  267. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  268. * must be called under priv->lock and mac access
  269. */
  270. void iwlagn_txq_set_sched(struct iwl_priv *priv, u32 mask)
  271. {
  272. iwl_write_prph(priv, IWLAGN_SCD_TXFACT, mask);
  273. }
  274. /*
  275. * handle build REPLY_TX command notification.
  276. */
  277. static void iwlagn_tx_cmd_build_basic(struct iwl_priv *priv,
  278. struct sk_buff *skb,
  279. struct iwl_tx_cmd *tx_cmd,
  280. struct ieee80211_tx_info *info,
  281. struct ieee80211_hdr *hdr,
  282. u8 std_id)
  283. {
  284. __le16 fc = hdr->frame_control;
  285. __le32 tx_flags = tx_cmd->tx_flags;
  286. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  287. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  288. tx_flags |= TX_CMD_FLG_ACK_MSK;
  289. if (ieee80211_is_mgmt(fc))
  290. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  291. if (ieee80211_is_probe_resp(fc) &&
  292. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  293. tx_flags |= TX_CMD_FLG_TSF_MSK;
  294. } else {
  295. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  296. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  297. }
  298. if (ieee80211_is_back_req(fc))
  299. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  300. else if (info->band == IEEE80211_BAND_2GHZ &&
  301. priv->cfg->bt_params &&
  302. priv->cfg->bt_params->advanced_bt_coexist &&
  303. (ieee80211_is_auth(fc) || ieee80211_is_assoc_req(fc) ||
  304. ieee80211_is_reassoc_req(fc) ||
  305. skb->protocol == cpu_to_be16(ETH_P_PAE)))
  306. tx_flags |= TX_CMD_FLG_IGNORE_BT;
  307. tx_cmd->sta_id = std_id;
  308. if (ieee80211_has_morefrags(fc))
  309. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  310. if (ieee80211_is_data_qos(fc)) {
  311. u8 *qc = ieee80211_get_qos_ctl(hdr);
  312. tx_cmd->tid_tspec = qc[0] & 0xf;
  313. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  314. } else {
  315. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  316. }
  317. priv->cfg->ops->utils->tx_cmd_protection(priv, info, fc, &tx_flags);
  318. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  319. if (ieee80211_is_mgmt(fc)) {
  320. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  321. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  322. else
  323. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  324. } else {
  325. tx_cmd->timeout.pm_frame_timeout = 0;
  326. }
  327. tx_cmd->driver_txop = 0;
  328. tx_cmd->tx_flags = tx_flags;
  329. tx_cmd->next_frame_len = 0;
  330. }
  331. #define RTS_DFAULT_RETRY_LIMIT 60
  332. static void iwlagn_tx_cmd_build_rate(struct iwl_priv *priv,
  333. struct iwl_tx_cmd *tx_cmd,
  334. struct ieee80211_tx_info *info,
  335. __le16 fc)
  336. {
  337. u32 rate_flags;
  338. int rate_idx;
  339. u8 rts_retry_limit;
  340. u8 data_retry_limit;
  341. u8 rate_plcp;
  342. /* Set retry limit on DATA packets and Probe Responses*/
  343. if (ieee80211_is_probe_resp(fc))
  344. data_retry_limit = 3;
  345. else
  346. data_retry_limit = IWLAGN_DEFAULT_TX_RETRY;
  347. tx_cmd->data_retry_limit = data_retry_limit;
  348. /* Set retry limit on RTS packets */
  349. rts_retry_limit = RTS_DFAULT_RETRY_LIMIT;
  350. if (data_retry_limit < rts_retry_limit)
  351. rts_retry_limit = data_retry_limit;
  352. tx_cmd->rts_retry_limit = rts_retry_limit;
  353. /* DATA packets will use the uCode station table for rate/antenna
  354. * selection */
  355. if (ieee80211_is_data(fc)) {
  356. tx_cmd->initial_rate_index = 0;
  357. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  358. return;
  359. }
  360. /**
  361. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  362. * not really a TX rate. Thus, we use the lowest supported rate for
  363. * this band. Also use the lowest supported rate if the stored rate
  364. * index is invalid.
  365. */
  366. rate_idx = info->control.rates[0].idx;
  367. if (info->control.rates[0].flags & IEEE80211_TX_RC_MCS ||
  368. (rate_idx < 0) || (rate_idx > IWL_RATE_COUNT_LEGACY))
  369. rate_idx = rate_lowest_index(&priv->bands[info->band],
  370. info->control.sta);
  371. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  372. if (info->band == IEEE80211_BAND_5GHZ)
  373. rate_idx += IWL_FIRST_OFDM_RATE;
  374. /* Get PLCP rate for tx_cmd->rate_n_flags */
  375. rate_plcp = iwl_rates[rate_idx].plcp;
  376. /* Zero out flags for this packet */
  377. rate_flags = 0;
  378. /* Set CCK flag as needed */
  379. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  380. rate_flags |= RATE_MCS_CCK_MSK;
  381. /* Set up antennas */
  382. if (priv->cfg->bt_params &&
  383. priv->cfg->bt_params->advanced_bt_coexist &&
  384. priv->bt_full_concurrent) {
  385. /* operated as 1x1 in full concurrency mode */
  386. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  387. first_antenna(priv->hw_params.valid_tx_ant));
  388. } else
  389. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  390. priv->hw_params.valid_tx_ant);
  391. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  392. /* Set the rate in the TX cmd */
  393. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  394. }
  395. static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  396. struct ieee80211_tx_info *info,
  397. struct iwl_tx_cmd *tx_cmd,
  398. struct sk_buff *skb_frag,
  399. int sta_id)
  400. {
  401. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  402. switch (keyconf->cipher) {
  403. case WLAN_CIPHER_SUITE_CCMP:
  404. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  405. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  406. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  407. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  408. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  409. break;
  410. case WLAN_CIPHER_SUITE_TKIP:
  411. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  412. ieee80211_get_tkip_key(keyconf, skb_frag,
  413. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  414. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  415. break;
  416. case WLAN_CIPHER_SUITE_WEP104:
  417. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  418. /* fall through */
  419. case WLAN_CIPHER_SUITE_WEP40:
  420. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  421. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  422. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  423. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  424. "with key %d\n", keyconf->keyidx);
  425. break;
  426. default:
  427. IWL_ERR(priv, "Unknown encode cipher %x\n", keyconf->cipher);
  428. break;
  429. }
  430. }
  431. /*
  432. * start REPLY_TX command process
  433. */
  434. int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  435. {
  436. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  437. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  438. struct ieee80211_sta *sta = info->control.sta;
  439. struct iwl_station_priv *sta_priv = NULL;
  440. struct iwl_tx_queue *txq;
  441. struct iwl_queue *q;
  442. struct iwl_device_cmd *out_cmd;
  443. struct iwl_cmd_meta *out_meta;
  444. struct iwl_tx_cmd *tx_cmd;
  445. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  446. int txq_id;
  447. dma_addr_t phys_addr;
  448. dma_addr_t txcmd_phys;
  449. dma_addr_t scratch_phys;
  450. u16 len, firstlen, secondlen;
  451. u16 seq_number = 0;
  452. __le16 fc;
  453. u8 hdr_len;
  454. u8 sta_id;
  455. u8 wait_write_ptr = 0;
  456. u8 tid = 0;
  457. u8 *qc = NULL;
  458. unsigned long flags;
  459. bool is_agg = false;
  460. if (info->control.vif)
  461. ctx = iwl_rxon_ctx_from_vif(info->control.vif);
  462. spin_lock_irqsave(&priv->lock, flags);
  463. if (iwl_is_rfkill(priv)) {
  464. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  465. goto drop_unlock;
  466. }
  467. fc = hdr->frame_control;
  468. #ifdef CONFIG_IWLWIFI_DEBUG
  469. if (ieee80211_is_auth(fc))
  470. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  471. else if (ieee80211_is_assoc_req(fc))
  472. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  473. else if (ieee80211_is_reassoc_req(fc))
  474. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  475. #endif
  476. hdr_len = ieee80211_hdrlen(fc);
  477. /* Find index into station table for destination station */
  478. sta_id = iwl_sta_id_or_broadcast(priv, ctx, info->control.sta);
  479. if (sta_id == IWL_INVALID_STATION) {
  480. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  481. hdr->addr1);
  482. goto drop_unlock;
  483. }
  484. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  485. if (sta)
  486. sta_priv = (void *)sta->drv_priv;
  487. if (sta_priv && sta_priv->asleep &&
  488. (info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE)) {
  489. /*
  490. * This sends an asynchronous command to the device,
  491. * but we can rely on it being processed before the
  492. * next frame is processed -- and the next frame to
  493. * this station is the one that will consume this
  494. * counter.
  495. * For now set the counter to just 1 since we do not
  496. * support uAPSD yet.
  497. */
  498. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  499. }
  500. /*
  501. * Send this frame after DTIM -- there's a special queue
  502. * reserved for this for contexts that support AP mode.
  503. */
  504. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  505. txq_id = ctx->mcast_queue;
  506. /*
  507. * The microcode will clear the more data
  508. * bit in the last frame it transmits.
  509. */
  510. hdr->frame_control |=
  511. cpu_to_le16(IEEE80211_FCTL_MOREDATA);
  512. } else
  513. txq_id = ctx->ac_to_queue[skb_get_queue_mapping(skb)];
  514. /* irqs already disabled/saved above when locking priv->lock */
  515. spin_lock(&priv->sta_lock);
  516. if (ieee80211_is_data_qos(fc)) {
  517. qc = ieee80211_get_qos_ctl(hdr);
  518. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  519. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  520. spin_unlock(&priv->sta_lock);
  521. goto drop_unlock;
  522. }
  523. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  524. seq_number &= IEEE80211_SCTL_SEQ;
  525. hdr->seq_ctrl = hdr->seq_ctrl &
  526. cpu_to_le16(IEEE80211_SCTL_FRAG);
  527. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  528. seq_number += 0x10;
  529. /* aggregation is on for this <sta,tid> */
  530. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  531. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  532. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  533. is_agg = true;
  534. }
  535. }
  536. txq = &priv->txq[txq_id];
  537. q = &txq->q;
  538. if (unlikely(iwl_queue_space(q) < q->high_mark)) {
  539. spin_unlock(&priv->sta_lock);
  540. goto drop_unlock;
  541. }
  542. if (ieee80211_is_data_qos(fc)) {
  543. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  544. if (!ieee80211_has_morefrags(fc))
  545. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  546. }
  547. spin_unlock(&priv->sta_lock);
  548. /* Set up driver data for this TFD */
  549. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  550. txq->txb[q->write_ptr].skb = skb;
  551. txq->txb[q->write_ptr].ctx = ctx;
  552. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  553. out_cmd = txq->cmd[q->write_ptr];
  554. out_meta = &txq->meta[q->write_ptr];
  555. tx_cmd = &out_cmd->cmd.tx;
  556. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  557. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  558. /*
  559. * Set up the Tx-command (not MAC!) header.
  560. * Store the chosen Tx queue and TFD index within the sequence field;
  561. * after Tx, uCode's Tx response will return this value so driver can
  562. * locate the frame within the tx queue and do post-tx processing.
  563. */
  564. out_cmd->hdr.cmd = REPLY_TX;
  565. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  566. INDEX_TO_SEQ(q->write_ptr)));
  567. /* Copy MAC header from skb into command buffer */
  568. memcpy(tx_cmd->hdr, hdr, hdr_len);
  569. /* Total # bytes to be transmitted */
  570. len = (u16)skb->len;
  571. tx_cmd->len = cpu_to_le16(len);
  572. if (info->control.hw_key)
  573. iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  574. /* TODO need this for burst mode later on */
  575. iwlagn_tx_cmd_build_basic(priv, skb, tx_cmd, info, hdr, sta_id);
  576. iwl_dbg_log_tx_data_frame(priv, len, hdr);
  577. iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  578. iwl_update_stats(priv, true, fc, len);
  579. /*
  580. * Use the first empty entry in this queue's command buffer array
  581. * to contain the Tx command and MAC header concatenated together
  582. * (payload data will be in another buffer).
  583. * Size of this varies, due to varying MAC header length.
  584. * If end is not dword aligned, we'll have 2 extra bytes at the end
  585. * of the MAC header (device reads on dword boundaries).
  586. * We'll tell device about this padding later.
  587. */
  588. len = sizeof(struct iwl_tx_cmd) +
  589. sizeof(struct iwl_cmd_header) + hdr_len;
  590. firstlen = (len + 3) & ~3;
  591. /* Tell NIC about any 2-byte padding after MAC header */
  592. if (firstlen != len)
  593. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  594. /* Physical address of this Tx command's header (not MAC header!),
  595. * within command buffer array. */
  596. txcmd_phys = pci_map_single(priv->pci_dev,
  597. &out_cmd->hdr, firstlen,
  598. PCI_DMA_BIDIRECTIONAL);
  599. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  600. dma_unmap_len_set(out_meta, len, firstlen);
  601. /* Add buffer containing Tx command and MAC(!) header to TFD's
  602. * first entry */
  603. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  604. txcmd_phys, firstlen, 1, 0);
  605. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  606. txq->need_update = 1;
  607. } else {
  608. wait_write_ptr = 1;
  609. txq->need_update = 0;
  610. }
  611. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  612. * if any (802.11 null frames have no payload). */
  613. secondlen = skb->len - hdr_len;
  614. if (secondlen > 0) {
  615. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  616. secondlen, PCI_DMA_TODEVICE);
  617. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  618. phys_addr, secondlen,
  619. 0, 0);
  620. }
  621. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  622. offsetof(struct iwl_tx_cmd, scratch);
  623. /* take back ownership of DMA buffer to enable update */
  624. pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys,
  625. firstlen, PCI_DMA_BIDIRECTIONAL);
  626. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  627. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  628. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  629. le16_to_cpu(out_cmd->hdr.sequence));
  630. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  631. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  632. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  633. /* Set up entry for this TFD in Tx byte-count array */
  634. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  635. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq,
  636. le16_to_cpu(tx_cmd->len));
  637. pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys,
  638. firstlen, PCI_DMA_BIDIRECTIONAL);
  639. trace_iwlwifi_dev_tx(priv,
  640. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  641. sizeof(struct iwl_tfd),
  642. &out_cmd->hdr, firstlen,
  643. skb->data + hdr_len, secondlen);
  644. /* Tell device the write index *just past* this latest filled TFD */
  645. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  646. iwl_txq_update_write_ptr(priv, txq);
  647. spin_unlock_irqrestore(&priv->lock, flags);
  648. /*
  649. * At this point the frame is "transmitted" successfully
  650. * and we will get a TX status notification eventually,
  651. * regardless of the value of ret. "ret" only indicates
  652. * whether or not we should update the write pointer.
  653. */
  654. /*
  655. * Avoid atomic ops if it isn't an associated client.
  656. * Also, if this is a packet for aggregation, don't
  657. * increase the counter because the ucode will stop
  658. * aggregation queues when their respective station
  659. * goes to sleep.
  660. */
  661. if (sta_priv && sta_priv->client && !is_agg)
  662. atomic_inc(&sta_priv->pending_frames);
  663. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  664. if (wait_write_ptr) {
  665. spin_lock_irqsave(&priv->lock, flags);
  666. txq->need_update = 1;
  667. iwl_txq_update_write_ptr(priv, txq);
  668. spin_unlock_irqrestore(&priv->lock, flags);
  669. } else {
  670. iwl_stop_queue(priv, txq);
  671. }
  672. }
  673. return 0;
  674. drop_unlock:
  675. spin_unlock_irqrestore(&priv->lock, flags);
  676. return -1;
  677. }
  678. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  679. struct iwl_dma_ptr *ptr, size_t size)
  680. {
  681. ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma,
  682. GFP_KERNEL);
  683. if (!ptr->addr)
  684. return -ENOMEM;
  685. ptr->size = size;
  686. return 0;
  687. }
  688. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  689. struct iwl_dma_ptr *ptr)
  690. {
  691. if (unlikely(!ptr->addr))
  692. return;
  693. dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  694. memset(ptr, 0, sizeof(*ptr));
  695. }
  696. /**
  697. * iwlagn_hw_txq_ctx_free - Free TXQ Context
  698. *
  699. * Destroy all TX DMA queues and structures
  700. */
  701. void iwlagn_hw_txq_ctx_free(struct iwl_priv *priv)
  702. {
  703. int txq_id;
  704. /* Tx queues */
  705. if (priv->txq) {
  706. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  707. if (txq_id == priv->cmd_queue)
  708. iwl_cmd_queue_free(priv);
  709. else
  710. iwl_tx_queue_free(priv, txq_id);
  711. }
  712. iwlagn_free_dma_ptr(priv, &priv->kw);
  713. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  714. /* free tx queue structure */
  715. iwl_free_txq_mem(priv);
  716. }
  717. /**
  718. * iwlagn_txq_ctx_alloc - allocate TX queue context
  719. * Allocate all Tx DMA structures and initialize them
  720. *
  721. * @param priv
  722. * @return error code
  723. */
  724. int iwlagn_txq_ctx_alloc(struct iwl_priv *priv)
  725. {
  726. int ret;
  727. int txq_id, slots_num;
  728. unsigned long flags;
  729. /* Free all tx/cmd queues and keep-warm buffer */
  730. iwlagn_hw_txq_ctx_free(priv);
  731. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  732. priv->hw_params.scd_bc_tbls_size);
  733. if (ret) {
  734. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  735. goto error_bc_tbls;
  736. }
  737. /* Alloc keep-warm buffer */
  738. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  739. if (ret) {
  740. IWL_ERR(priv, "Keep Warm allocation failed\n");
  741. goto error_kw;
  742. }
  743. /* allocate tx queue structure */
  744. ret = iwl_alloc_txq_mem(priv);
  745. if (ret)
  746. goto error;
  747. spin_lock_irqsave(&priv->lock, flags);
  748. /* Turn off all Tx DMA fifos */
  749. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  750. /* Tell NIC where to find the "keep warm" buffer */
  751. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  752. spin_unlock_irqrestore(&priv->lock, flags);
  753. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  754. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  755. slots_num = (txq_id == priv->cmd_queue) ?
  756. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  757. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  758. txq_id);
  759. if (ret) {
  760. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  761. goto error;
  762. }
  763. }
  764. return ret;
  765. error:
  766. iwlagn_hw_txq_ctx_free(priv);
  767. iwlagn_free_dma_ptr(priv, &priv->kw);
  768. error_kw:
  769. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  770. error_bc_tbls:
  771. return ret;
  772. }
  773. void iwlagn_txq_ctx_reset(struct iwl_priv *priv)
  774. {
  775. int txq_id, slots_num;
  776. unsigned long flags;
  777. spin_lock_irqsave(&priv->lock, flags);
  778. /* Turn off all Tx DMA fifos */
  779. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  780. /* Tell NIC where to find the "keep warm" buffer */
  781. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  782. spin_unlock_irqrestore(&priv->lock, flags);
  783. /* Alloc and init all Tx queues, including the command queue (#4) */
  784. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  785. slots_num = txq_id == priv->cmd_queue ?
  786. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  787. iwl_tx_queue_reset(priv, &priv->txq[txq_id], slots_num, txq_id);
  788. }
  789. }
  790. /**
  791. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  792. */
  793. void iwlagn_txq_ctx_stop(struct iwl_priv *priv)
  794. {
  795. int ch;
  796. unsigned long flags;
  797. /* Turn off all Tx DMA fifos */
  798. spin_lock_irqsave(&priv->lock, flags);
  799. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  800. /* Stop each Tx DMA channel, and wait for it to be idle */
  801. for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) {
  802. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  803. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  804. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  805. 1000))
  806. IWL_ERR(priv, "Failing on timeout while stopping"
  807. " DMA channel %d [0x%08x]", ch,
  808. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  809. }
  810. spin_unlock_irqrestore(&priv->lock, flags);
  811. }
  812. /*
  813. * Find first available (lowest unused) Tx Queue, mark it "active".
  814. * Called only when finding queue for aggregation.
  815. * Should never return anything < 7, because they should already
  816. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  817. */
  818. static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv)
  819. {
  820. int txq_id;
  821. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  822. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  823. return txq_id;
  824. return -1;
  825. }
  826. int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  827. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  828. {
  829. int sta_id;
  830. int tx_fifo;
  831. int txq_id;
  832. int ret;
  833. unsigned long flags;
  834. struct iwl_tid_data *tid_data;
  835. tx_fifo = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  836. if (unlikely(tx_fifo < 0))
  837. return tx_fifo;
  838. IWL_WARN(priv, "%s on ra = %pM tid = %d\n",
  839. __func__, sta->addr, tid);
  840. sta_id = iwl_sta_id(sta);
  841. if (sta_id == IWL_INVALID_STATION) {
  842. IWL_ERR(priv, "Start AGG on invalid station\n");
  843. return -ENXIO;
  844. }
  845. if (unlikely(tid >= MAX_TID_COUNT))
  846. return -EINVAL;
  847. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  848. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  849. return -ENXIO;
  850. }
  851. txq_id = iwlagn_txq_ctx_activate_free(priv);
  852. if (txq_id == -1) {
  853. IWL_ERR(priv, "No free aggregation queue available\n");
  854. return -ENXIO;
  855. }
  856. spin_lock_irqsave(&priv->sta_lock, flags);
  857. tid_data = &priv->stations[sta_id].tid[tid];
  858. *ssn = SEQ_TO_SN(tid_data->seq_number);
  859. tid_data->agg.txq_id = txq_id;
  860. iwl_set_swq_id(&priv->txq[txq_id], get_ac_from_tid(tid), txq_id);
  861. spin_unlock_irqrestore(&priv->sta_lock, flags);
  862. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  863. sta_id, tid, *ssn);
  864. if (ret)
  865. return ret;
  866. spin_lock_irqsave(&priv->sta_lock, flags);
  867. tid_data = &priv->stations[sta_id].tid[tid];
  868. if (tid_data->tfds_in_queue == 0) {
  869. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  870. tid_data->agg.state = IWL_AGG_ON;
  871. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  872. } else {
  873. IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
  874. tid_data->tfds_in_queue);
  875. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  876. }
  877. spin_unlock_irqrestore(&priv->sta_lock, flags);
  878. return ret;
  879. }
  880. int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  881. struct ieee80211_sta *sta, u16 tid)
  882. {
  883. int tx_fifo_id, txq_id, sta_id, ssn;
  884. struct iwl_tid_data *tid_data;
  885. int write_ptr, read_ptr;
  886. unsigned long flags;
  887. tx_fifo_id = get_fifo_from_tid(iwl_rxon_ctx_from_vif(vif), tid);
  888. if (unlikely(tx_fifo_id < 0))
  889. return tx_fifo_id;
  890. sta_id = iwl_sta_id(sta);
  891. if (sta_id == IWL_INVALID_STATION) {
  892. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  893. return -ENXIO;
  894. }
  895. spin_lock_irqsave(&priv->sta_lock, flags);
  896. tid_data = &priv->stations[sta_id].tid[tid];
  897. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  898. txq_id = tid_data->agg.txq_id;
  899. switch (priv->stations[sta_id].tid[tid].agg.state) {
  900. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  901. /*
  902. * This can happen if the peer stops aggregation
  903. * again before we've had a chance to drain the
  904. * queue we selected previously, i.e. before the
  905. * session was really started completely.
  906. */
  907. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  908. goto turn_off;
  909. case IWL_AGG_ON:
  910. break;
  911. default:
  912. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  913. }
  914. write_ptr = priv->txq[txq_id].q.write_ptr;
  915. read_ptr = priv->txq[txq_id].q.read_ptr;
  916. /* The queue is not empty */
  917. if (write_ptr != read_ptr) {
  918. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  919. priv->stations[sta_id].tid[tid].agg.state =
  920. IWL_EMPTYING_HW_QUEUE_DELBA;
  921. spin_unlock_irqrestore(&priv->sta_lock, flags);
  922. return 0;
  923. }
  924. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  925. turn_off:
  926. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  927. /* do not restore/save irqs */
  928. spin_unlock(&priv->sta_lock);
  929. spin_lock(&priv->lock);
  930. /*
  931. * the only reason this call can fail is queue number out of range,
  932. * which can happen if uCode is reloaded and all the station
  933. * information are lost. if it is outside the range, there is no need
  934. * to deactivate the uCode queue, just return "success" to allow
  935. * mac80211 to clean up it own data.
  936. */
  937. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  938. tx_fifo_id);
  939. spin_unlock_irqrestore(&priv->lock, flags);
  940. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  941. return 0;
  942. }
  943. int iwlagn_txq_check_empty(struct iwl_priv *priv,
  944. int sta_id, u8 tid, int txq_id)
  945. {
  946. struct iwl_queue *q = &priv->txq[txq_id].q;
  947. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  948. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  949. struct iwl_rxon_context *ctx;
  950. ctx = &priv->contexts[priv->stations[sta_id].ctxid];
  951. lockdep_assert_held(&priv->sta_lock);
  952. switch (priv->stations[sta_id].tid[tid].agg.state) {
  953. case IWL_EMPTYING_HW_QUEUE_DELBA:
  954. /* We are reclaiming the last packet of the */
  955. /* aggregated HW queue */
  956. if ((txq_id == tid_data->agg.txq_id) &&
  957. (q->read_ptr == q->write_ptr)) {
  958. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  959. int tx_fifo = get_fifo_from_tid(ctx, tid);
  960. IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
  961. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  962. ssn, tx_fifo);
  963. tid_data->agg.state = IWL_AGG_OFF;
  964. ieee80211_stop_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  965. }
  966. break;
  967. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  968. /* We are reclaiming the last packet of the queue */
  969. if (tid_data->tfds_in_queue == 0) {
  970. IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
  971. tid_data->agg.state = IWL_AGG_ON;
  972. ieee80211_start_tx_ba_cb_irqsafe(ctx->vif, addr, tid);
  973. }
  974. break;
  975. }
  976. return 0;
  977. }
  978. static void iwlagn_non_agg_tx_status(struct iwl_priv *priv,
  979. struct iwl_rxon_context *ctx,
  980. const u8 *addr1)
  981. {
  982. struct ieee80211_sta *sta;
  983. struct iwl_station_priv *sta_priv;
  984. rcu_read_lock();
  985. sta = ieee80211_find_sta(ctx->vif, addr1);
  986. if (sta) {
  987. sta_priv = (void *)sta->drv_priv;
  988. /* avoid atomic ops if this isn't a client */
  989. if (sta_priv->client &&
  990. atomic_dec_return(&sta_priv->pending_frames) == 0)
  991. ieee80211_sta_block_awake(priv->hw, sta, false);
  992. }
  993. rcu_read_unlock();
  994. }
  995. static void iwlagn_tx_status(struct iwl_priv *priv, struct iwl_tx_info *tx_info,
  996. bool is_agg)
  997. {
  998. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) tx_info->skb->data;
  999. if (!is_agg)
  1000. iwlagn_non_agg_tx_status(priv, tx_info->ctx, hdr->addr1);
  1001. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb);
  1002. }
  1003. int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  1004. {
  1005. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  1006. struct iwl_queue *q = &txq->q;
  1007. struct iwl_tx_info *tx_info;
  1008. int nfreed = 0;
  1009. struct ieee80211_hdr *hdr;
  1010. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  1011. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  1012. "is out of range [0-%d] %d %d.\n", txq_id,
  1013. index, q->n_bd, q->write_ptr, q->read_ptr);
  1014. return 0;
  1015. }
  1016. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  1017. q->read_ptr != index;
  1018. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  1019. tx_info = &txq->txb[txq->q.read_ptr];
  1020. iwlagn_tx_status(priv, tx_info,
  1021. txq_id >= IWLAGN_FIRST_AMPDU_QUEUE);
  1022. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  1023. if (hdr && ieee80211_is_data_qos(hdr->frame_control))
  1024. nfreed++;
  1025. tx_info->skb = NULL;
  1026. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  1027. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  1028. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  1029. }
  1030. return nfreed;
  1031. }
  1032. /**
  1033. * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack
  1034. *
  1035. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  1036. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  1037. */
  1038. static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  1039. struct iwl_ht_agg *agg,
  1040. struct iwl_compressed_ba_resp *ba_resp)
  1041. {
  1042. int i, sh, ack;
  1043. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1044. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1045. u64 bitmap, sent_bitmap;
  1046. int successes = 0;
  1047. struct ieee80211_tx_info *info;
  1048. if (unlikely(!agg->wait_for_ba)) {
  1049. if (unlikely(ba_resp->bitmap))
  1050. IWL_ERR(priv, "Received BA when not expected\n");
  1051. return -EINVAL;
  1052. }
  1053. /* Mark that the expected block-ack response arrived */
  1054. agg->wait_for_ba = 0;
  1055. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  1056. /* Calculate shift to align block-ack bits with our Tx window bits */
  1057. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  1058. if (sh < 0) /* tbw something is wrong with indices */
  1059. sh += 0x100;
  1060. if (agg->frame_count > (64 - sh)) {
  1061. IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size");
  1062. return -1;
  1063. }
  1064. if (!priv->cfg->base_params->no_agg_framecnt_info && ba_resp->txed) {
  1065. /*
  1066. * sent and ack information provided by uCode
  1067. * use it instead of figure out ourself
  1068. */
  1069. if (ba_resp->txed_2_done > ba_resp->txed) {
  1070. IWL_DEBUG_TX_REPLY(priv,
  1071. "bogus sent(%d) and ack(%d) count\n",
  1072. ba_resp->txed, ba_resp->txed_2_done);
  1073. /*
  1074. * set txed_2_done = txed,
  1075. * so it won't impact rate scale
  1076. */
  1077. ba_resp->txed = ba_resp->txed_2_done;
  1078. }
  1079. IWL_DEBUG_HT(priv, "agg frames sent:%d, acked:%d\n",
  1080. ba_resp->txed, ba_resp->txed_2_done);
  1081. } else {
  1082. /* don't use 64-bit values for now */
  1083. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1084. /* check for success or failure according to the
  1085. * transmitted bitmap and block-ack bitmap */
  1086. sent_bitmap = bitmap & agg->bitmap;
  1087. /* For each frame attempted in aggregation,
  1088. * update driver's record of tx frame's status. */
  1089. i = 0;
  1090. while (sent_bitmap) {
  1091. ack = sent_bitmap & 1ULL;
  1092. successes += ack;
  1093. IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n",
  1094. ack ? "ACK" : "NACK", i,
  1095. (agg->start_idx + i) & 0xff,
  1096. agg->start_idx + i);
  1097. sent_bitmap >>= 1;
  1098. ++i;
  1099. }
  1100. }
  1101. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
  1102. memset(&info->status, 0, sizeof(info->status));
  1103. info->flags |= IEEE80211_TX_STAT_ACK;
  1104. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1105. if (!priv->cfg->base_params->no_agg_framecnt_info && ba_resp->txed) {
  1106. info->status.ampdu_ack_len = ba_resp->txed_2_done;
  1107. info->status.ampdu_len = ba_resp->txed;
  1108. } else {
  1109. info->status.ampdu_ack_len = successes;
  1110. info->status.ampdu_len = agg->frame_count;
  1111. }
  1112. iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1113. IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n", (unsigned long long)bitmap);
  1114. return 0;
  1115. }
  1116. /**
  1117. * translate ucode response to mac80211 tx status control values
  1118. */
  1119. void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  1120. struct ieee80211_tx_info *info)
  1121. {
  1122. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1123. info->antenna_sel_tx =
  1124. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1125. if (rate_n_flags & RATE_MCS_HT_MSK)
  1126. r->flags |= IEEE80211_TX_RC_MCS;
  1127. if (rate_n_flags & RATE_MCS_GF_MSK)
  1128. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1129. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1130. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1131. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1132. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1133. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1134. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1135. r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1136. }
  1137. /**
  1138. * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1139. *
  1140. * Handles block-acknowledge notification from device, which reports success
  1141. * of frames sent via aggregation.
  1142. */
  1143. void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
  1144. struct iwl_rx_mem_buffer *rxb)
  1145. {
  1146. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1147. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1148. struct iwl_tx_queue *txq = NULL;
  1149. struct iwl_ht_agg *agg;
  1150. int index;
  1151. int sta_id;
  1152. int tid;
  1153. unsigned long flags;
  1154. /* "flow" corresponds to Tx queue */
  1155. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1156. /* "ssn" is start of block-ack Tx window, corresponds to index
  1157. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1158. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1159. if (scd_flow >= priv->hw_params.max_txq_num) {
  1160. IWL_ERR(priv,
  1161. "BUG_ON scd_flow is bigger than number of queues\n");
  1162. return;
  1163. }
  1164. txq = &priv->txq[scd_flow];
  1165. sta_id = ba_resp->sta_id;
  1166. tid = ba_resp->tid;
  1167. agg = &priv->stations[sta_id].tid[tid].agg;
  1168. if (unlikely(agg->txq_id != scd_flow)) {
  1169. /*
  1170. * FIXME: this is a uCode bug which need to be addressed,
  1171. * log the information and return for now!
  1172. * since it is possible happen very often and in order
  1173. * not to fill the syslog, don't enable the logging by default
  1174. */
  1175. IWL_DEBUG_TX_REPLY(priv,
  1176. "BA scd_flow %d does not match txq_id %d\n",
  1177. scd_flow, agg->txq_id);
  1178. return;
  1179. }
  1180. /* Find index just before block-ack window */
  1181. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1182. spin_lock_irqsave(&priv->sta_lock, flags);
  1183. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1184. "sta_id = %d\n",
  1185. agg->wait_for_ba,
  1186. (u8 *) &ba_resp->sta_addr_lo32,
  1187. ba_resp->sta_id);
  1188. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  1189. "%d, scd_ssn = %d\n",
  1190. ba_resp->tid,
  1191. ba_resp->seq_ctl,
  1192. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1193. ba_resp->scd_flow,
  1194. ba_resp->scd_ssn);
  1195. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
  1196. agg->start_idx,
  1197. (unsigned long long)agg->bitmap);
  1198. /* Update driver's record of ACK vs. not for each frame in window */
  1199. iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1200. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1201. * block-ack window (we assume that they've been successfully
  1202. * transmitted ... if not, it's too late anyway). */
  1203. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1204. /* calculate mac80211 ampdu sw queue to wake */
  1205. int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index);
  1206. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  1207. if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  1208. priv->mac80211_registered &&
  1209. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  1210. iwl_wake_queue(priv, txq);
  1211. iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow);
  1212. }
  1213. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1214. }
  1215. #ifdef CONFIG_IWLWIFI_DEBUG
  1216. const char *iwl_get_tx_fail_reason(u32 status)
  1217. {
  1218. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  1219. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  1220. switch (status & TX_STATUS_MSK) {
  1221. case TX_STATUS_SUCCESS:
  1222. return "SUCCESS";
  1223. TX_STATUS_POSTPONE(DELAY);
  1224. TX_STATUS_POSTPONE(FEW_BYTES);
  1225. TX_STATUS_POSTPONE(BT_PRIO);
  1226. TX_STATUS_POSTPONE(QUIET_PERIOD);
  1227. TX_STATUS_POSTPONE(CALC_TTAK);
  1228. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  1229. TX_STATUS_FAIL(SHORT_LIMIT);
  1230. TX_STATUS_FAIL(LONG_LIMIT);
  1231. TX_STATUS_FAIL(FIFO_UNDERRUN);
  1232. TX_STATUS_FAIL(DRAIN_FLOW);
  1233. TX_STATUS_FAIL(RFKILL_FLUSH);
  1234. TX_STATUS_FAIL(LIFE_EXPIRE);
  1235. TX_STATUS_FAIL(DEST_PS);
  1236. TX_STATUS_FAIL(HOST_ABORTED);
  1237. TX_STATUS_FAIL(BT_RETRY);
  1238. TX_STATUS_FAIL(STA_INVALID);
  1239. TX_STATUS_FAIL(FRAG_DROPPED);
  1240. TX_STATUS_FAIL(TID_DISABLE);
  1241. TX_STATUS_FAIL(FIFO_FLUSHED);
  1242. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  1243. TX_STATUS_FAIL(PASSIVE_NO_RX);
  1244. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  1245. }
  1246. return "UNKNOWN";
  1247. #undef TX_STATUS_FAIL
  1248. #undef TX_STATUS_POSTPONE
  1249. }
  1250. #endif /* CONFIG_IWLWIFI_DEBUG */