iwl-agn-tx.c 40 KB

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