iwl-agn-tx.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369
  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->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. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  383. priv->hw_params.valid_tx_ant);
  384. rate_flags |= iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  385. /* Set the rate in the TX cmd */
  386. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  387. }
  388. static void iwlagn_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  389. struct ieee80211_tx_info *info,
  390. struct iwl_tx_cmd *tx_cmd,
  391. struct sk_buff *skb_frag,
  392. int sta_id)
  393. {
  394. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  395. switch (keyconf->alg) {
  396. case ALG_CCMP:
  397. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  398. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  399. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  400. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  401. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  402. break;
  403. case ALG_TKIP:
  404. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  405. ieee80211_get_tkip_key(keyconf, skb_frag,
  406. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  407. IWL_DEBUG_TX(priv, "tx_cmd with tkip hwcrypto\n");
  408. break;
  409. case ALG_WEP:
  410. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  411. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  412. if (keyconf->keylen == WEP_KEY_LEN_128)
  413. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  414. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  415. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  416. "with key %d\n", keyconf->keyidx);
  417. break;
  418. default:
  419. IWL_ERR(priv, "Unknown encode alg %d\n", keyconf->alg);
  420. break;
  421. }
  422. }
  423. /*
  424. * start REPLY_TX command process
  425. */
  426. int iwlagn_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  427. {
  428. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  429. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  430. struct ieee80211_sta *sta = info->control.sta;
  431. struct iwl_station_priv *sta_priv = NULL;
  432. struct iwl_tx_queue *txq;
  433. struct iwl_queue *q;
  434. struct iwl_device_cmd *out_cmd;
  435. struct iwl_cmd_meta *out_meta;
  436. struct iwl_tx_cmd *tx_cmd;
  437. int swq_id, txq_id;
  438. dma_addr_t phys_addr;
  439. dma_addr_t txcmd_phys;
  440. dma_addr_t scratch_phys;
  441. u16 len, len_org, firstlen, secondlen;
  442. u16 seq_number = 0;
  443. __le16 fc;
  444. u8 hdr_len;
  445. u8 sta_id;
  446. u8 wait_write_ptr = 0;
  447. u8 tid = 0;
  448. u8 *qc = NULL;
  449. unsigned long flags;
  450. spin_lock_irqsave(&priv->lock, flags);
  451. if (iwl_is_rfkill(priv)) {
  452. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  453. goto drop_unlock;
  454. }
  455. fc = hdr->frame_control;
  456. #ifdef CONFIG_IWLWIFI_DEBUG
  457. if (ieee80211_is_auth(fc))
  458. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  459. else if (ieee80211_is_assoc_req(fc))
  460. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  461. else if (ieee80211_is_reassoc_req(fc))
  462. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  463. #endif
  464. hdr_len = ieee80211_hdrlen(fc);
  465. /* Find index into station table for destination station */
  466. sta_id = iwl_sta_id_or_broadcast(priv, info->control.sta);
  467. if (sta_id == IWL_INVALID_STATION) {
  468. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  469. hdr->addr1);
  470. goto drop_unlock;
  471. }
  472. IWL_DEBUG_TX(priv, "station Id %d\n", sta_id);
  473. if (sta)
  474. sta_priv = (void *)sta->drv_priv;
  475. if (sta_priv && sta_id != priv->hw_params.bcast_sta_id &&
  476. sta_priv->asleep) {
  477. WARN_ON(!(info->flags & IEEE80211_TX_CTL_PSPOLL_RESPONSE));
  478. /*
  479. * This sends an asynchronous command to the device,
  480. * but we can rely on it being processed before the
  481. * next frame is processed -- and the next frame to
  482. * this station is the one that will consume this
  483. * counter.
  484. * For now set the counter to just 1 since we do not
  485. * support uAPSD yet.
  486. */
  487. iwl_sta_modify_sleep_tx_count(priv, sta_id, 1);
  488. }
  489. txq_id = get_queue_from_ac(skb_get_queue_mapping(skb));
  490. /* irqs already disabled/saved above when locking priv->lock */
  491. spin_lock(&priv->sta_lock);
  492. if (ieee80211_is_data_qos(fc)) {
  493. qc = ieee80211_get_qos_ctl(hdr);
  494. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  495. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  496. spin_unlock(&priv->sta_lock);
  497. goto drop_unlock;
  498. }
  499. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  500. seq_number &= IEEE80211_SCTL_SEQ;
  501. hdr->seq_ctrl = hdr->seq_ctrl &
  502. cpu_to_le16(IEEE80211_SCTL_FRAG);
  503. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  504. seq_number += 0x10;
  505. /* aggregation is on for this <sta,tid> */
  506. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  507. priv->stations[sta_id].tid[tid].agg.state == IWL_AGG_ON) {
  508. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  509. }
  510. }
  511. txq = &priv->txq[txq_id];
  512. swq_id = txq->swq_id;
  513. q = &txq->q;
  514. if (unlikely(iwl_queue_space(q) < q->high_mark)) {
  515. spin_unlock(&priv->sta_lock);
  516. goto drop_unlock;
  517. }
  518. if (ieee80211_is_data_qos(fc)) {
  519. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  520. if (!ieee80211_has_morefrags(fc))
  521. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  522. }
  523. spin_unlock(&priv->sta_lock);
  524. /* Set up driver data for this TFD */
  525. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  526. txq->txb[q->write_ptr].skb = skb;
  527. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  528. out_cmd = txq->cmd[q->write_ptr];
  529. out_meta = &txq->meta[q->write_ptr];
  530. tx_cmd = &out_cmd->cmd.tx;
  531. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  532. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  533. /*
  534. * Set up the Tx-command (not MAC!) header.
  535. * Store the chosen Tx queue and TFD index within the sequence field;
  536. * after Tx, uCode's Tx response will return this value so driver can
  537. * locate the frame within the tx queue and do post-tx processing.
  538. */
  539. out_cmd->hdr.cmd = REPLY_TX;
  540. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  541. INDEX_TO_SEQ(q->write_ptr)));
  542. /* Copy MAC header from skb into command buffer */
  543. memcpy(tx_cmd->hdr, hdr, hdr_len);
  544. /* Total # bytes to be transmitted */
  545. len = (u16)skb->len;
  546. tx_cmd->len = cpu_to_le16(len);
  547. if (info->control.hw_key)
  548. iwlagn_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  549. /* TODO need this for burst mode later on */
  550. iwlagn_tx_cmd_build_basic(priv, tx_cmd, info, hdr, sta_id);
  551. iwl_dbg_log_tx_data_frame(priv, len, hdr);
  552. iwlagn_tx_cmd_build_rate(priv, tx_cmd, info, fc);
  553. iwl_update_stats(priv, true, fc, len);
  554. /*
  555. * Use the first empty entry in this queue's command buffer array
  556. * to contain the Tx command and MAC header concatenated together
  557. * (payload data will be in another buffer).
  558. * Size of this varies, due to varying MAC header length.
  559. * If end is not dword aligned, we'll have 2 extra bytes at the end
  560. * of the MAC header (device reads on dword boundaries).
  561. * We'll tell device about this padding later.
  562. */
  563. len = sizeof(struct iwl_tx_cmd) +
  564. sizeof(struct iwl_cmd_header) + hdr_len;
  565. len_org = len;
  566. firstlen = len = (len + 3) & ~3;
  567. if (len_org != len)
  568. len_org = 1;
  569. else
  570. len_org = 0;
  571. /* Tell NIC about any 2-byte padding after MAC header */
  572. if (len_org)
  573. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  574. /* Physical address of this Tx command's header (not MAC header!),
  575. * within command buffer array. */
  576. txcmd_phys = pci_map_single(priv->pci_dev,
  577. &out_cmd->hdr, len,
  578. PCI_DMA_BIDIRECTIONAL);
  579. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  580. dma_unmap_len_set(out_meta, len, len);
  581. /* Add buffer containing Tx command and MAC(!) header to TFD's
  582. * first entry */
  583. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  584. txcmd_phys, len, 1, 0);
  585. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  586. txq->need_update = 1;
  587. } else {
  588. wait_write_ptr = 1;
  589. txq->need_update = 0;
  590. }
  591. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  592. * if any (802.11 null frames have no payload). */
  593. secondlen = len = skb->len - hdr_len;
  594. if (len) {
  595. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  596. len, PCI_DMA_TODEVICE);
  597. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  598. phys_addr, len,
  599. 0, 0);
  600. }
  601. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  602. offsetof(struct iwl_tx_cmd, scratch);
  603. len = sizeof(struct iwl_tx_cmd) +
  604. sizeof(struct iwl_cmd_header) + hdr_len;
  605. /* take back ownership of DMA buffer to enable update */
  606. pci_dma_sync_single_for_cpu(priv->pci_dev, txcmd_phys,
  607. len, PCI_DMA_BIDIRECTIONAL);
  608. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  609. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  610. IWL_DEBUG_TX(priv, "sequence nr = 0X%x\n",
  611. le16_to_cpu(out_cmd->hdr.sequence));
  612. IWL_DEBUG_TX(priv, "tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  613. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  614. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  615. /* Set up entry for this TFD in Tx byte-count array */
  616. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  617. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq,
  618. le16_to_cpu(tx_cmd->len));
  619. pci_dma_sync_single_for_device(priv->pci_dev, txcmd_phys,
  620. len, PCI_DMA_BIDIRECTIONAL);
  621. trace_iwlwifi_dev_tx(priv,
  622. &((struct iwl_tfd *)txq->tfds)[txq->q.write_ptr],
  623. sizeof(struct iwl_tfd),
  624. &out_cmd->hdr, firstlen,
  625. skb->data + hdr_len, secondlen);
  626. /* Tell device the write index *just past* this latest filled TFD */
  627. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  628. iwl_txq_update_write_ptr(priv, txq);
  629. spin_unlock_irqrestore(&priv->lock, flags);
  630. /*
  631. * At this point the frame is "transmitted" successfully
  632. * and we will get a TX status notification eventually,
  633. * regardless of the value of ret. "ret" only indicates
  634. * whether or not we should update the write pointer.
  635. */
  636. /* avoid atomic ops if it isn't an associated client */
  637. if (sta_priv && sta_priv->client)
  638. atomic_inc(&sta_priv->pending_frames);
  639. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  640. if (wait_write_ptr) {
  641. spin_lock_irqsave(&priv->lock, flags);
  642. txq->need_update = 1;
  643. iwl_txq_update_write_ptr(priv, txq);
  644. spin_unlock_irqrestore(&priv->lock, flags);
  645. } else {
  646. iwl_stop_queue(priv, txq->swq_id);
  647. }
  648. }
  649. return 0;
  650. drop_unlock:
  651. spin_unlock_irqrestore(&priv->lock, flags);
  652. return -1;
  653. }
  654. static inline int iwlagn_alloc_dma_ptr(struct iwl_priv *priv,
  655. struct iwl_dma_ptr *ptr, size_t size)
  656. {
  657. ptr->addr = dma_alloc_coherent(&priv->pci_dev->dev, size, &ptr->dma,
  658. GFP_KERNEL);
  659. if (!ptr->addr)
  660. return -ENOMEM;
  661. ptr->size = size;
  662. return 0;
  663. }
  664. static inline void iwlagn_free_dma_ptr(struct iwl_priv *priv,
  665. struct iwl_dma_ptr *ptr)
  666. {
  667. if (unlikely(!ptr->addr))
  668. return;
  669. dma_free_coherent(&priv->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  670. memset(ptr, 0, sizeof(*ptr));
  671. }
  672. /**
  673. * iwlagn_hw_txq_ctx_free - Free TXQ Context
  674. *
  675. * Destroy all TX DMA queues and structures
  676. */
  677. void iwlagn_hw_txq_ctx_free(struct iwl_priv *priv)
  678. {
  679. int txq_id;
  680. /* Tx queues */
  681. if (priv->txq) {
  682. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  683. if (txq_id == IWL_CMD_QUEUE_NUM)
  684. iwl_cmd_queue_free(priv);
  685. else
  686. iwl_tx_queue_free(priv, txq_id);
  687. }
  688. iwlagn_free_dma_ptr(priv, &priv->kw);
  689. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  690. /* free tx queue structure */
  691. iwl_free_txq_mem(priv);
  692. }
  693. /**
  694. * iwlagn_txq_ctx_alloc - allocate TX queue context
  695. * Allocate all Tx DMA structures and initialize them
  696. *
  697. * @param priv
  698. * @return error code
  699. */
  700. int iwlagn_txq_ctx_alloc(struct iwl_priv *priv)
  701. {
  702. int ret;
  703. int txq_id, slots_num;
  704. unsigned long flags;
  705. /* Free all tx/cmd queues and keep-warm buffer */
  706. iwlagn_hw_txq_ctx_free(priv);
  707. ret = iwlagn_alloc_dma_ptr(priv, &priv->scd_bc_tbls,
  708. priv->hw_params.scd_bc_tbls_size);
  709. if (ret) {
  710. IWL_ERR(priv, "Scheduler BC Table allocation failed\n");
  711. goto error_bc_tbls;
  712. }
  713. /* Alloc keep-warm buffer */
  714. ret = iwlagn_alloc_dma_ptr(priv, &priv->kw, IWL_KW_SIZE);
  715. if (ret) {
  716. IWL_ERR(priv, "Keep Warm allocation failed\n");
  717. goto error_kw;
  718. }
  719. /* allocate tx queue structure */
  720. ret = iwl_alloc_txq_mem(priv);
  721. if (ret)
  722. goto error;
  723. spin_lock_irqsave(&priv->lock, flags);
  724. /* Turn off all Tx DMA fifos */
  725. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  726. /* Tell NIC where to find the "keep warm" buffer */
  727. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  728. spin_unlock_irqrestore(&priv->lock, flags);
  729. /* Alloc and init all Tx queues, including the command queue (#4) */
  730. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  731. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  732. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  733. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  734. txq_id);
  735. if (ret) {
  736. IWL_ERR(priv, "Tx %d queue init failed\n", txq_id);
  737. goto error;
  738. }
  739. }
  740. return ret;
  741. error:
  742. iwlagn_hw_txq_ctx_free(priv);
  743. iwlagn_free_dma_ptr(priv, &priv->kw);
  744. error_kw:
  745. iwlagn_free_dma_ptr(priv, &priv->scd_bc_tbls);
  746. error_bc_tbls:
  747. return ret;
  748. }
  749. void iwlagn_txq_ctx_reset(struct iwl_priv *priv)
  750. {
  751. int txq_id, slots_num;
  752. unsigned long flags;
  753. spin_lock_irqsave(&priv->lock, flags);
  754. /* Turn off all Tx DMA fifos */
  755. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  756. /* Tell NIC where to find the "keep warm" buffer */
  757. iwl_write_direct32(priv, FH_KW_MEM_ADDR_REG, priv->kw.dma >> 4);
  758. spin_unlock_irqrestore(&priv->lock, flags);
  759. /* Alloc and init all Tx queues, including the command queue (#4) */
  760. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  761. slots_num = txq_id == IWL_CMD_QUEUE_NUM ?
  762. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  763. iwl_tx_queue_reset(priv, &priv->txq[txq_id], slots_num, txq_id);
  764. }
  765. }
  766. /**
  767. * iwlagn_txq_ctx_stop - Stop all Tx DMA channels
  768. */
  769. void iwlagn_txq_ctx_stop(struct iwl_priv *priv)
  770. {
  771. int ch;
  772. unsigned long flags;
  773. /* Turn off all Tx DMA fifos */
  774. spin_lock_irqsave(&priv->lock, flags);
  775. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  776. /* Stop each Tx DMA channel, and wait for it to be idle */
  777. for (ch = 0; ch < priv->hw_params.dma_chnl_num; ch++) {
  778. iwl_write_direct32(priv, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  779. if (iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  780. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  781. 1000))
  782. IWL_ERR(priv, "Failing on timeout while stopping"
  783. " DMA channel %d [0x%08x]", ch,
  784. iwl_read_direct32(priv, FH_TSSR_TX_STATUS_REG));
  785. }
  786. spin_unlock_irqrestore(&priv->lock, flags);
  787. }
  788. /*
  789. * Find first available (lowest unused) Tx Queue, mark it "active".
  790. * Called only when finding queue for aggregation.
  791. * Should never return anything < 7, because they should already
  792. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  793. */
  794. static int iwlagn_txq_ctx_activate_free(struct iwl_priv *priv)
  795. {
  796. int txq_id;
  797. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  798. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  799. return txq_id;
  800. return -1;
  801. }
  802. int iwlagn_tx_agg_start(struct iwl_priv *priv, struct ieee80211_vif *vif,
  803. struct ieee80211_sta *sta, u16 tid, u16 *ssn)
  804. {
  805. int sta_id;
  806. int tx_fifo;
  807. int txq_id;
  808. int ret;
  809. unsigned long flags;
  810. struct iwl_tid_data *tid_data;
  811. tx_fifo = get_fifo_from_tid(tid);
  812. if (unlikely(tx_fifo < 0))
  813. return tx_fifo;
  814. IWL_WARN(priv, "%s on ra = %pM tid = %d\n",
  815. __func__, sta->addr, tid);
  816. sta_id = iwl_sta_id(sta);
  817. if (sta_id == IWL_INVALID_STATION) {
  818. IWL_ERR(priv, "Start AGG on invalid station\n");
  819. return -ENXIO;
  820. }
  821. if (unlikely(tid >= MAX_TID_COUNT))
  822. return -EINVAL;
  823. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  824. IWL_ERR(priv, "Start AGG when state is not IWL_AGG_OFF !\n");
  825. return -ENXIO;
  826. }
  827. txq_id = iwlagn_txq_ctx_activate_free(priv);
  828. if (txq_id == -1) {
  829. IWL_ERR(priv, "No free aggregation queue available\n");
  830. return -ENXIO;
  831. }
  832. spin_lock_irqsave(&priv->sta_lock, flags);
  833. tid_data = &priv->stations[sta_id].tid[tid];
  834. *ssn = SEQ_TO_SN(tid_data->seq_number);
  835. tid_data->agg.txq_id = txq_id;
  836. priv->txq[txq_id].swq_id = iwl_virtual_agg_queue_num(get_ac_from_tid(tid), txq_id);
  837. spin_unlock_irqrestore(&priv->sta_lock, flags);
  838. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  839. sta_id, tid, *ssn);
  840. if (ret)
  841. return ret;
  842. spin_lock_irqsave(&priv->sta_lock, flags);
  843. tid_data = &priv->stations[sta_id].tid[tid];
  844. if (tid_data->tfds_in_queue == 0) {
  845. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  846. tid_data->agg.state = IWL_AGG_ON;
  847. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  848. } else {
  849. IWL_DEBUG_HT(priv, "HW queue is NOT empty: %d packets in HW queue\n",
  850. tid_data->tfds_in_queue);
  851. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  852. }
  853. spin_unlock_irqrestore(&priv->sta_lock, flags);
  854. return ret;
  855. }
  856. int iwlagn_tx_agg_stop(struct iwl_priv *priv, struct ieee80211_vif *vif,
  857. struct ieee80211_sta *sta, u16 tid)
  858. {
  859. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  860. struct iwl_tid_data *tid_data;
  861. int write_ptr, read_ptr;
  862. unsigned long flags;
  863. tx_fifo_id = get_fifo_from_tid(tid);
  864. if (unlikely(tx_fifo_id < 0))
  865. return tx_fifo_id;
  866. sta_id = iwl_sta_id(sta);
  867. if (sta_id == IWL_INVALID_STATION) {
  868. IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
  869. return -ENXIO;
  870. }
  871. spin_lock_irqsave(&priv->sta_lock, flags);
  872. if (priv->stations[sta_id].tid[tid].agg.state ==
  873. IWL_EMPTYING_HW_QUEUE_ADDBA) {
  874. IWL_DEBUG_HT(priv, "AGG stop before setup done\n");
  875. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  876. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  877. spin_unlock_irqrestore(&priv->sta_lock, flags);
  878. return 0;
  879. }
  880. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
  881. IWL_WARN(priv, "Stopping AGG while state not ON or starting\n");
  882. tid_data = &priv->stations[sta_id].tid[tid];
  883. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  884. txq_id = tid_data->agg.txq_id;
  885. write_ptr = priv->txq[txq_id].q.write_ptr;
  886. read_ptr = priv->txq[txq_id].q.read_ptr;
  887. /* The queue is not empty */
  888. if (write_ptr != read_ptr) {
  889. IWL_DEBUG_HT(priv, "Stopping a non empty AGG HW QUEUE\n");
  890. priv->stations[sta_id].tid[tid].agg.state =
  891. IWL_EMPTYING_HW_QUEUE_DELBA;
  892. spin_unlock_irqrestore(&priv->sta_lock, flags);
  893. return 0;
  894. }
  895. IWL_DEBUG_HT(priv, "HW queue is empty\n");
  896. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  897. /* do not restore/save irqs */
  898. spin_unlock(&priv->sta_lock);
  899. spin_lock(&priv->lock);
  900. /*
  901. * the only reason this call can fail is queue number out of range,
  902. * which can happen if uCode is reloaded and all the station
  903. * information are lost. if it is outside the range, there is no need
  904. * to deactivate the uCode queue, just return "success" to allow
  905. * mac80211 to clean up it own data.
  906. */
  907. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  908. tx_fifo_id);
  909. spin_unlock_irqrestore(&priv->lock, flags);
  910. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  911. return 0;
  912. }
  913. int iwlagn_txq_check_empty(struct iwl_priv *priv,
  914. int sta_id, u8 tid, int txq_id)
  915. {
  916. struct iwl_queue *q = &priv->txq[txq_id].q;
  917. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  918. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  919. lockdep_assert_held(&priv->sta_lock);
  920. switch (priv->stations[sta_id].tid[tid].agg.state) {
  921. case IWL_EMPTYING_HW_QUEUE_DELBA:
  922. /* We are reclaiming the last packet of the */
  923. /* aggregated HW queue */
  924. if ((txq_id == tid_data->agg.txq_id) &&
  925. (q->read_ptr == q->write_ptr)) {
  926. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  927. int tx_fifo = get_fifo_from_tid(tid);
  928. IWL_DEBUG_HT(priv, "HW queue empty: continue DELBA flow\n");
  929. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  930. ssn, tx_fifo);
  931. tid_data->agg.state = IWL_AGG_OFF;
  932. ieee80211_stop_tx_ba_cb_irqsafe(priv->vif, addr, tid);
  933. }
  934. break;
  935. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  936. /* We are reclaiming the last packet of the queue */
  937. if (tid_data->tfds_in_queue == 0) {
  938. IWL_DEBUG_HT(priv, "HW queue empty: continue ADDBA flow\n");
  939. tid_data->agg.state = IWL_AGG_ON;
  940. ieee80211_start_tx_ba_cb_irqsafe(priv->vif, addr, tid);
  941. }
  942. break;
  943. }
  944. return 0;
  945. }
  946. static void iwlagn_tx_status(struct iwl_priv *priv, struct sk_buff *skb)
  947. {
  948. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  949. struct ieee80211_sta *sta;
  950. struct iwl_station_priv *sta_priv;
  951. rcu_read_lock();
  952. sta = ieee80211_find_sta(priv->vif, hdr->addr1);
  953. if (sta) {
  954. sta_priv = (void *)sta->drv_priv;
  955. /* avoid atomic ops if this isn't a client */
  956. if (sta_priv->client &&
  957. atomic_dec_return(&sta_priv->pending_frames) == 0)
  958. ieee80211_sta_block_awake(priv->hw, sta, false);
  959. }
  960. rcu_read_unlock();
  961. ieee80211_tx_status_irqsafe(priv->hw, skb);
  962. }
  963. int iwlagn_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  964. {
  965. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  966. struct iwl_queue *q = &txq->q;
  967. struct iwl_tx_info *tx_info;
  968. int nfreed = 0;
  969. struct ieee80211_hdr *hdr;
  970. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  971. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  972. "is out of range [0-%d] %d %d.\n", txq_id,
  973. index, q->n_bd, q->write_ptr, q->read_ptr);
  974. return 0;
  975. }
  976. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  977. q->read_ptr != index;
  978. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  979. tx_info = &txq->txb[txq->q.read_ptr];
  980. iwlagn_tx_status(priv, tx_info->skb);
  981. hdr = (struct ieee80211_hdr *)tx_info->skb->data;
  982. if (hdr && ieee80211_is_data_qos(hdr->frame_control))
  983. nfreed++;
  984. tx_info->skb = NULL;
  985. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  986. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  987. priv->cfg->ops->lib->txq_free_tfd(priv, txq);
  988. }
  989. return nfreed;
  990. }
  991. /**
  992. * iwlagn_tx_status_reply_compressed_ba - Update tx status from block-ack
  993. *
  994. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  995. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  996. */
  997. static int iwlagn_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  998. struct iwl_ht_agg *agg,
  999. struct iwl_compressed_ba_resp *ba_resp)
  1000. {
  1001. int i, sh, ack;
  1002. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1003. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1004. u64 bitmap, sent_bitmap;
  1005. int successes = 0;
  1006. struct ieee80211_tx_info *info;
  1007. if (unlikely(!agg->wait_for_ba)) {
  1008. IWL_ERR(priv, "Received BA when not expected\n");
  1009. return -EINVAL;
  1010. }
  1011. /* Mark that the expected block-ack response arrived */
  1012. agg->wait_for_ba = 0;
  1013. IWL_DEBUG_TX_REPLY(priv, "BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  1014. /* Calculate shift to align block-ack bits with our Tx window bits */
  1015. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl >> 4);
  1016. if (sh < 0) /* tbw something is wrong with indices */
  1017. sh += 0x100;
  1018. /* don't use 64-bit values for now */
  1019. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1020. if (agg->frame_count > (64 - sh)) {
  1021. IWL_DEBUG_TX_REPLY(priv, "more frames than bitmap size");
  1022. return -1;
  1023. }
  1024. /* check for success or failure according to the
  1025. * transmitted bitmap and block-ack bitmap */
  1026. sent_bitmap = bitmap & agg->bitmap;
  1027. /* For each frame attempted in aggregation,
  1028. * update driver's record of tx frame's status. */
  1029. i = 0;
  1030. while (sent_bitmap) {
  1031. ack = sent_bitmap & 1ULL;
  1032. successes += ack;
  1033. IWL_DEBUG_TX_REPLY(priv, "%s ON i=%d idx=%d raw=%d\n",
  1034. ack ? "ACK" : "NACK", i, (agg->start_idx + i) & 0xff,
  1035. agg->start_idx + i);
  1036. sent_bitmap >>= 1;
  1037. ++i;
  1038. }
  1039. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb);
  1040. memset(&info->status, 0, sizeof(info->status));
  1041. info->flags |= IEEE80211_TX_STAT_ACK;
  1042. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1043. info->status.ampdu_ack_len = successes;
  1044. info->status.ampdu_len = agg->frame_count;
  1045. iwlagn_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1046. IWL_DEBUG_TX_REPLY(priv, "Bitmap %llx\n", (unsigned long long)bitmap);
  1047. return 0;
  1048. }
  1049. /**
  1050. * translate ucode response to mac80211 tx status control values
  1051. */
  1052. void iwlagn_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
  1053. struct ieee80211_tx_info *info)
  1054. {
  1055. struct ieee80211_tx_rate *r = &info->control.rates[0];
  1056. info->antenna_sel_tx =
  1057. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  1058. if (rate_n_flags & RATE_MCS_HT_MSK)
  1059. r->flags |= IEEE80211_TX_RC_MCS;
  1060. if (rate_n_flags & RATE_MCS_GF_MSK)
  1061. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1062. if (rate_n_flags & RATE_MCS_HT40_MSK)
  1063. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1064. if (rate_n_flags & RATE_MCS_DUP_MSK)
  1065. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  1066. if (rate_n_flags & RATE_MCS_SGI_MSK)
  1067. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  1068. r->idx = iwlagn_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  1069. }
  1070. /**
  1071. * iwlagn_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1072. *
  1073. * Handles block-acknowledge notification from device, which reports success
  1074. * of frames sent via aggregation.
  1075. */
  1076. void iwlagn_rx_reply_compressed_ba(struct iwl_priv *priv,
  1077. struct iwl_rx_mem_buffer *rxb)
  1078. {
  1079. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1080. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1081. struct iwl_tx_queue *txq = NULL;
  1082. struct iwl_ht_agg *agg;
  1083. int index;
  1084. int sta_id;
  1085. int tid;
  1086. unsigned long flags;
  1087. /* "flow" corresponds to Tx queue */
  1088. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1089. /* "ssn" is start of block-ack Tx window, corresponds to index
  1090. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1091. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1092. if (scd_flow >= priv->hw_params.max_txq_num) {
  1093. IWL_ERR(priv,
  1094. "BUG_ON scd_flow is bigger than number of queues\n");
  1095. return;
  1096. }
  1097. txq = &priv->txq[scd_flow];
  1098. sta_id = ba_resp->sta_id;
  1099. tid = ba_resp->tid;
  1100. agg = &priv->stations[sta_id].tid[tid].agg;
  1101. if (unlikely(agg->txq_id != scd_flow)) {
  1102. /*
  1103. * FIXME: this is a uCode bug which need to be addressed,
  1104. * log the information and return for now!
  1105. * since it is possible happen very often and in order
  1106. * not to fill the syslog, don't enable the logging by default
  1107. */
  1108. IWL_DEBUG_TX_REPLY(priv,
  1109. "BA scd_flow %d does not match txq_id %d\n",
  1110. scd_flow, agg->txq_id);
  1111. return;
  1112. }
  1113. /* Find index just before block-ack window */
  1114. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1115. spin_lock_irqsave(&priv->sta_lock, flags);
  1116. IWL_DEBUG_TX_REPLY(priv, "REPLY_COMPRESSED_BA [%d] Received from %pM, "
  1117. "sta_id = %d\n",
  1118. agg->wait_for_ba,
  1119. (u8 *) &ba_resp->sta_addr_lo32,
  1120. ba_resp->sta_id);
  1121. IWL_DEBUG_TX_REPLY(priv, "TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  1122. "%d, scd_ssn = %d\n",
  1123. ba_resp->tid,
  1124. ba_resp->seq_ctl,
  1125. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1126. ba_resp->scd_flow,
  1127. ba_resp->scd_ssn);
  1128. IWL_DEBUG_TX_REPLY(priv, "DAT start_idx = %d, bitmap = 0x%llx\n",
  1129. agg->start_idx,
  1130. (unsigned long long)agg->bitmap);
  1131. /* Update driver's record of ACK vs. not for each frame in window */
  1132. iwlagn_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1133. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1134. * block-ack window (we assume that they've been successfully
  1135. * transmitted ... if not, it's too late anyway). */
  1136. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1137. /* calculate mac80211 ampdu sw queue to wake */
  1138. int freed = iwlagn_tx_queue_reclaim(priv, scd_flow, index);
  1139. iwl_free_tfds_in_queue(priv, sta_id, tid, freed);
  1140. if ((iwl_queue_space(&txq->q) > txq->q.low_mark) &&
  1141. priv->mac80211_registered &&
  1142. (agg->state != IWL_EMPTYING_HW_QUEUE_DELBA))
  1143. iwl_wake_queue(priv, txq->swq_id);
  1144. iwlagn_txq_check_empty(priv, sta_id, tid, scd_flow);
  1145. }
  1146. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1147. }