iwl-agn-tx.c 43 KB

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